Class TestPubsub
java.lang.Object
org.apache.beam.sdk.io.gcp.pubsub.TestPubsub
- All Implemented Interfaces:
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.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionapply
(Statement base, Description description) void
assertSubscriptionEventuallyCreated
(String project, Duration timeoutDuration) Block until a subscription is created for this test topic in the specified project.assertThatTopicEventuallyReceives
(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.void
checkIfAnySubscriptionExists
(String project, Duration timeoutDuration) Deprecated.static TestPubsub
create()
Creates an instance of this rule using options provided byTestPipeline.testingPipelineOptions()
.static TestPubsub
fromOptions
(PipelineOptions options) Creates an instance of this rule using provided options.void
publish
(List<PubsubMessage> messages) Publish messages totopicPath()
.Subscription path used to listen for messages ontopicPath()
.Topic path where events will be published to.waitForNMessages
(int n, Duration timeoutDuration) Repeatedly pull messages fromsubscriptionPath()
, returns after receivingn
messages or after waiting fortimeoutDuration
.
-
Method Details
-
create
Creates an instance of this rule using options provided byTestPipeline.testingPipelineOptions()
.Loads GCP configuration from
TestPipelineOptions
. -
fromOptions
Creates an instance of this rule using provided options.Loads GCP configuration from
TestPipelineOptions
. -
apply
-
topicPath
Topic path where events will be published to. -
subscriptionPath
Subscription path used to listen for messages ontopicPath()
. -
publish
Publish messages totopicPath()
. -
waitForNMessages
public List<PubsubMessage> waitForNMessages(int n, Duration timeoutDuration) throws IOException, InterruptedException Repeatedly pull messages fromsubscriptionPath()
, returns after receivingn
messages or after waiting fortimeoutDuration
.- Throws:
IOException
InterruptedException
-
assertThatTopicEventuallyReceives
public TestPubsub.PollingAssertion assertThatTopicEventuallyReceives(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:
invalid input: '{@code testTopic .assertThatTopicEventuallyReceives( hasProperty("payload", equalTo("hello".getBytes(StandardCharsets.US_ASCII))), hasProperty("payload", equalTo("world".getBytes(StandardCharsets.US_ASCII)))) .waitForUpTo(Duration.standardSeconds(20)); </pre>'
-
checkIfAnySubscriptionExists
@Deprecated public void checkIfAnySubscriptionExists(String project, Duration timeoutDuration) throws InterruptedException, IllegalArgumentException, IOException, TimeoutException Check if topics exist.- Parameters:
project
- GCP project identifier.timeoutDuration
- Joda duration that sets a period of time before checking times out.- Throws:
InterruptedException
IllegalArgumentException
IOException
TimeoutException
-
assertSubscriptionEventuallyCreated
public void assertSubscriptionEventuallyCreated(String project, Duration timeoutDuration) throws InterruptedException, IllegalArgumentException, IOException Block until a subscription is created for this test topic in the specified project. ThrowsAssertionError
iftimeoutDuration
is reached before a subscription is created.- Parameters:
project
- GCP project identifier.timeoutDuration
- Joda duration before timeout occurs.- Throws:
InterruptedException
IllegalArgumentException
IOException
-
assertSubscriptionEventuallyCreated(java.lang.String, org.joda.time.Duration)
.