Class TestPubsub
- java.lang.Object
-
- org.apache.beam.sdk.io.gcp.pubsub.TestPubsub
-
- All Implemented Interfaces:
org.junit.rules.TestRule
public class TestPubsub extends java.lang.Object implements org.junit.rules.TestRuleTest 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceTestPubsub.PollingAssertion
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description org.junit.runners.model.Statementapply(org.junit.runners.model.Statement base, org.junit.runner.Description description)voidassertSubscriptionEventuallyCreated(java.lang.String project, org.joda.time.Duration timeoutDuration)Block until a subscription is created for this test topic in the specified project.TestPubsub.PollingAssertionassertThatTopicEventuallyReceives(org.hamcrest.Matcher<PubsubMessage>... matchers)Repeatedly pull messages fromsubscriptionPath()until receiving one for each matcher (or timeout is reached), then assert that the received messages match the expectations.voidcheckIfAnySubscriptionExists(java.lang.String project, org.joda.time.Duration timeoutDuration)static TestPubsubcreate()Creates an instance of this rule using options provided byTestPipeline.testingPipelineOptions().static TestPubsubfromOptions(org.apache.beam.sdk.options.PipelineOptions options)Creates an instance of this rule using provided options.voidpublish(java.util.List<PubsubMessage> messages)Publish messages totopicPath().PubsubClient.SubscriptionPathsubscriptionPath()Subscription path used to listen for messages ontopicPath().PubsubClient.TopicPathtopicPath()Topic path where events will be published to.java.util.List<PubsubMessage>waitForNMessages(int n, org.joda.time.Duration timeoutDuration)Repeatedly pull messages fromsubscriptionPath(), returns after receivingnmessages or after waiting fortimeoutDuration.
-
-
-
Method Detail
-
create
public static TestPubsub create()
Creates an instance of this rule using options provided byTestPipeline.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:
applyin interfaceorg.junit.rules.TestRule
-
topicPath
public PubsubClient.TopicPath topicPath()
Topic path where events will be published to.
-
subscriptionPath
public PubsubClient.SubscriptionPath subscriptionPath()
Subscription path used to listen for messages ontopicPath().
-
publish
public void publish(java.util.List<PubsubMessage> messages)
Publish messages totopicPath().
-
waitForNMessages
public java.util.List<PubsubMessage> waitForNMessages(int n, org.joda.time.Duration timeoutDuration) throws java.io.IOException, java.lang.InterruptedException
Repeatedly pull messages fromsubscriptionPath(), returns after receivingnmessages or after waiting fortimeoutDuration.- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
assertThatTopicEventuallyReceives
public TestPubsub.PollingAssertion assertThatTopicEventuallyReceives(org.hamcrest.Matcher<PubsubMessage>... matchers)
Repeatedly pull messages fromsubscriptionPath()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.TimeoutExceptionCheck if topics exist.- Parameters:
project- GCP project identifier.timeoutDuration- Joda duration that sets a period of time before checking times out.- Throws:
java.lang.InterruptedExceptionjava.lang.IllegalArgumentExceptionjava.io.IOExceptionjava.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.IOExceptionBlock until a subscription is created for this test topic in the specified project. ThrowsAssertionErroriftimeoutDurationis reached before a subscription is created.- Parameters:
project- GCP project identifier.timeoutDuration- Joda duration before timeout occurs.- Throws:
java.lang.InterruptedExceptionjava.lang.IllegalArgumentExceptionjava.io.IOException
-
-