Class TestPubsub

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    public class TestPubsub
    extends java.lang.Object
    implements org.junit.rules.TestRule
    Test rule which creates a new topic and subscription with randomized names and exposes the APIs to work with them.

    Deletes topic and subscription on shutdown.

    • Method Detail

      • create

        public static TestPubsub create()
        Creates an instance of this rule using options provided by TestPipeline.testingPipelineOptions().

        Loads GCP configuration from TestPipelineOptions.

      • fromOptions

        public static TestPubsub fromOptions​(org.apache.beam.sdk.options.PipelineOptions options)
        Creates an instance of this rule using provided options.

        Loads GCP configuration from TestPipelineOptions.

      • apply

        public org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement base,
                                                       org.junit.runner.Description description)
        Specified by:
        apply in interface org.junit.rules.TestRule
      • waitForNMessages

        public java.util.List<PubsubMessage> waitForNMessages​(int n,
                                                              org.joda.time.Duration timeoutDuration)
                                                       throws java.io.IOException,
                                                              java.lang.InterruptedException
        Repeatedly pull messages from subscriptionPath(), returns after receiving n messages or after waiting for timeoutDuration.
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • assertThatTopicEventuallyReceives

        public TestPubsub.PollingAssertion assertThatTopicEventuallyReceives​(org.hamcrest.Matcher<PubsubMessage>... matchers)
        Repeatedly pull messages from subscriptionPath() until receiving one for each matcher (or timeout is reached), then assert that the received messages match the expectations.

        Example usage:

        {@code
         testTopic
           .assertThatTopicEventuallyReceives(
             hasProperty("payload", equalTo("hello".getBytes(StandardCharsets.US_ASCII))),
             hasProperty("payload", equalTo("world".getBytes(StandardCharsets.US_ASCII))))
           .waitForUpTo(Duration.standardSeconds(20));
         
      • checkIfAnySubscriptionExists

        @Deprecated
        public void checkIfAnySubscriptionExists​(java.lang.String project,
                                                 org.joda.time.Duration timeoutDuration)
                                          throws java.lang.InterruptedException,
                                                 java.lang.IllegalArgumentException,
                                                 java.io.IOException,
                                                 java.util.concurrent.TimeoutException
        Check if topics exist.
        Parameters:
        project - GCP project identifier.
        timeoutDuration - Joda duration that sets a period of time before checking times out.
        Throws:
        java.lang.InterruptedException
        java.lang.IllegalArgumentException
        java.io.IOException
        java.util.concurrent.TimeoutException
      • assertSubscriptionEventuallyCreated

        public void assertSubscriptionEventuallyCreated​(java.lang.String project,
                                                        org.joda.time.Duration timeoutDuration)
                                                 throws java.lang.InterruptedException,
                                                        java.lang.IllegalArgumentException,
                                                        java.io.IOException
        Block until a subscription is created for this test topic in the specified project. Throws AssertionError if timeoutDuration is reached before a subscription is created.
        Parameters:
        project - GCP project identifier.
        timeoutDuration - Joda duration before timeout occurs.
        Throws:
        java.lang.InterruptedException
        java.lang.IllegalArgumentException
        java.io.IOException