public class TestPubsub
extends java.lang.Object
implements org.junit.rules.TestRule
Deletes topic and subscription on shutdown.
| Modifier and Type | Class and Description |
|---|---|
static interface |
TestPubsub.PollingAssertion |
| Modifier and Type | Method and Description |
|---|---|
org.junit.runners.model.Statement |
apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description) |
void |
assertSubscriptionEventuallyCreated(java.lang.String project,
org.joda.time.Duration timeoutDuration)
Block until a subscription is created for this test topic in the specified project.
|
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. |
void |
checkIfAnySubscriptionExists(java.lang.String project,
org.joda.time.Duration timeoutDuration)
|
static TestPubsub |
create()
Creates an instance of this rule using options provided by
TestPipeline.testingPipelineOptions(). |
static TestPubsub |
fromOptions(org.apache.beam.sdk.options.PipelineOptions options)
Creates an instance of this rule using provided options.
|
void |
publish(java.util.List<PubsubMessage> messages)
Publish messages to
topicPath(). |
PubsubClient.SubscriptionPath |
subscriptionPath()
Subscription path used to listen for messages on
topicPath(). |
PubsubClient.TopicPath |
topicPath()
Topic path where events will be published to.
|
java.util.List<PubsubMessage> |
waitForNMessages(int n,
org.joda.time.Duration timeoutDuration)
Repeatedly pull messages from
subscriptionPath(), returns after receiving n
messages or after waiting for timeoutDuration. |
public static TestPubsub create()
TestPipeline.testingPipelineOptions().
Loads GCP configuration from TestPipelineOptions.
public static TestPubsub fromOptions(org.apache.beam.sdk.options.PipelineOptions options)
Loads GCP configuration from TestPipelineOptions.
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base,
org.junit.runner.Description description)
apply in interface org.junit.rules.TestRulepublic PubsubClient.TopicPath topicPath()
public PubsubClient.SubscriptionPath subscriptionPath()
topicPath().public void publish(java.util.List<PubsubMessage> messages)
topicPath().public java.util.List<PubsubMessage> waitForNMessages(int n, org.joda.time.Duration timeoutDuration) throws java.io.IOException, java.lang.InterruptedException
subscriptionPath(), returns after receiving n
messages or after waiting for timeoutDuration.java.io.IOExceptionjava.lang.InterruptedExceptionpublic TestPubsub.PollingAssertion assertThatTopicEventuallyReceives(org.hamcrest.Matcher<PubsubMessage>... matchers)
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));
@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
project - GCP project identifier.timeoutDuration - Joda duration that sets a period of time before checking times out.java.lang.InterruptedExceptionjava.lang.IllegalArgumentExceptionjava.io.IOExceptionjava.util.concurrent.TimeoutExceptionpublic void assertSubscriptionEventuallyCreated(java.lang.String project,
org.joda.time.Duration timeoutDuration)
throws java.lang.InterruptedException,
java.lang.IllegalArgumentException,
java.io.IOException
AssertionError if timeoutDuration is reached before a subscription is created.project - GCP project identifier.timeoutDuration - Joda duration before timeout occurs.java.lang.InterruptedExceptionjava.lang.IllegalArgumentExceptionjava.io.IOException