Class PubsubTestClient
java.lang.Object
org.apache.beam.sdk.io.gcp.pubsub.PubsubClient
org.apache.beam.sdk.io.gcp.pubsub.PubsubTestClient
- All Implemented Interfaces:
Closeable,Serializable,AutoCloseable
A (partial) implementation of
PubsubClient for use by unit tests. Only suitable for
testing publish(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath, java.util.List<org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage>), pull(long, org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath, int, boolean), acknowledge(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath, java.util.List<java.lang.String>) and modifyAckDeadline(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath, java.util.List<java.lang.String>, int)
methods. Relies on statics to mimic the Pubsub service, though we try to hide that.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceClosing the factory will validate all expected messages were processed.Nested classes/interfaces inherited from class org.apache.beam.sdk.io.gcp.pubsub.PubsubClient
PubsubClient.IncomingMessage, PubsubClient.OutgoingMessage, PubsubClient.ProjectPath, PubsubClient.PubsubClientFactory, PubsubClient.SchemaPath, PubsubClient.SubscriptionPath, PubsubClient.TopicPath -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintackDeadlineSeconds(PubsubClient.SubscriptionPath subscription) Return the ack deadline, in seconds, forsubscription.voidacknowledge(PubsubClient.SubscriptionPath subscription, List<String> ackIds) Acknowldege messages fromsubscriptionwithackIds.voidadvance()For subscription mode only: Track progression of time according to theClockpassed .voidclose()createFactoryForGetSchema(PubsubClient.TopicPath expectedTopic, @Nullable PubsubClient.SchemaPath expectedSchemaPath, @Nullable Schema expectedSchema) createFactoryForPublish(@Nullable PubsubClient.TopicPath expectedTopic, Iterable<PubsubClient.OutgoingMessage> expectedOutgoingMessages, Iterable<PubsubClient.OutgoingMessage> failingOutgoingMessages) Return a factory for testing publishers.createFactoryForPull(com.google.api.client.util.Clock clock, PubsubClient.SubscriptionPath expectedSubscription, int ackTimeoutSec, Iterable<PubsubClient.IncomingMessage> expectedIncomingMessages) Return a factory for testing subscribers.createFactoryForPullAndPublish(PubsubClient.SubscriptionPath pullSubscription, PubsubClient.TopicPath publishTopicPath, com.google.api.client.util.Clock pullClock, int pullAckTimeoutSec, Iterable<PubsubClient.IncomingMessage> expectedIncomingMessages, Iterable<PubsubClient.OutgoingMessage> expectedOutgoingMessages, Iterable<PubsubClient.OutgoingMessage> failingOutgoingMessages) Returns a factory for a test that is expected to both publish and pull messages over the course of the test.voidcreateSchema(PubsubClient.SchemaPath schemaPath, String schemaContent, com.google.pubsub.v1.Schema.Type type) CreateSchemafrom Schema definition content.voidcreateSubscription(PubsubClient.TopicPath topic, PubsubClient.SubscriptionPath subscription, int ackDeadlineSeconds) Createsubscriptiontotopic.voidCreatetopic.voidcreateTopic(PubsubClient.TopicPath topic, PubsubClient.SchemaPath schema) Create {link TopicPath} withPubsubClient.SchemaPath.voiddeleteSchema(PubsubClient.SchemaPath schemaPath) DeletePubsubClient.SchemaPath.voiddeleteSubscription(PubsubClient.SubscriptionPath subscription) Deletesubscription.voidgetSchema(PubsubClient.SchemaPath schemaPath) Return a BeamSchemafrom the Pub/Sub schema resource, if exists.getSchemaPath(PubsubClient.TopicPath topicPath) ReturnPubsubClient.SchemaPathfromPubsubClient.TopicPathif exists.booleanisEOF()Return true ifPubsubClient.pull(long, org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath, int, boolean)will always return empty list.booleanReturn true iftopicexists.listSubscriptions(PubsubClient.ProjectPath project, PubsubClient.TopicPath topic) Return a list of subscriptions fortopicinproject.listTopics(PubsubClient.ProjectPath project) Return a list of topics forproject.voidmodifyAckDeadline(PubsubClient.SubscriptionPath subscription, List<String> ackIds, int deadlineSeconds) Modify the ack deadline for messages fromsubscriptionwithackIdsto bedeadlineSecondsfrom now.intpublish(PubsubClient.TopicPath topic, List<PubsubClient.OutgoingMessage> outgoingMessages) PublishoutgoingMessagesto Pubsubtopic.pull(long requestTimeMsSinceEpoch, PubsubClient.SubscriptionPath subscription, int batchSize, boolean returnImmediately) Request the next batch of up tobatchSizemessages fromsubscription.Methods inherited from class org.apache.beam.sdk.io.gcp.pubsub.PubsubClient
createRandomSubscription, extractTimestampAttribute, parseTimestampAsMsSinceEpoch, projectPathFromId, projectPathFromPath, schemaPathFromId, schemaPathFromPath, subscriptionPathFromName, subscriptionPathFromPath, topicPathFromName, topicPathFromPath
-
Constructor Details
-
PubsubTestClient
public PubsubTestClient()
-
-
Method Details
-
createFactoryForPublish
public static PubsubTestClient.PubsubTestClientFactory createFactoryForPublish(@Nullable PubsubClient.TopicPath expectedTopic, Iterable<PubsubClient.OutgoingMessage> expectedOutgoingMessages, Iterable<PubsubClient.OutgoingMessage> failingOutgoingMessages) Return a factory for testing publishers. Only one factory may be in-flight at a time. The factory must be closed when the test is complete, at which point final validation will occur. -
createFactoryForPull
public static PubsubTestClient.PubsubTestClientFactory createFactoryForPull(com.google.api.client.util.Clock clock, PubsubClient.SubscriptionPath expectedSubscription, int ackTimeoutSec, Iterable<PubsubClient.IncomingMessage> expectedIncomingMessages) Return a factory for testing subscribers. Only one factory may be in-flight at a time. The factory must be closed when the test in complete -
createFactoryForPullAndPublish
public static PubsubTestClient.PubsubTestClientFactory createFactoryForPullAndPublish(PubsubClient.SubscriptionPath pullSubscription, PubsubClient.TopicPath publishTopicPath, com.google.api.client.util.Clock pullClock, int pullAckTimeoutSec, Iterable<PubsubClient.IncomingMessage> expectedIncomingMessages, Iterable<PubsubClient.OutgoingMessage> expectedOutgoingMessages, Iterable<PubsubClient.OutgoingMessage> failingOutgoingMessages) Returns a factory for a test that is expected to both publish and pull messages over the course of the test. -
createFactoryForGetSchema
public static PubsubTestClient.PubsubTestClientFactory createFactoryForGetSchema(PubsubClient.TopicPath expectedTopic, @Nullable PubsubClient.SchemaPath expectedSchemaPath, @Nullable Schema expectedSchema) -
createFactoryForCreateSubscription
-
advance
public void advance()For subscription mode only: Track progression of time according to theClockpassed . This will simulate Pubsub expiring outstanding ACKs. -
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
publish
public int publish(PubsubClient.TopicPath topic, List<PubsubClient.OutgoingMessage> outgoingMessages) throws IOException Description copied from class:PubsubClientPublishoutgoingMessagesto Pubsubtopic. Return number of messages published.- Specified by:
publishin classPubsubClient- Throws:
IOException
-
pull
public List<PubsubClient.IncomingMessage> pull(long requestTimeMsSinceEpoch, PubsubClient.SubscriptionPath subscription, int batchSize, boolean returnImmediately) throws IOException Description copied from class:PubsubClientRequest the next batch of up tobatchSizemessages fromsubscription. Return the received messages, or empty collection if none were available. Does not wait for messages to arrive ifreturnImmediatelyis true. Returned messages will record their request time asrequestTimeMsSinceEpoch.- Specified by:
pullin classPubsubClient- Throws:
IOException
-
acknowledge
public void acknowledge(PubsubClient.SubscriptionPath subscription, List<String> ackIds) throws IOException Description copied from class:PubsubClientAcknowldege messages fromsubscriptionwithackIds.- Specified by:
acknowledgein classPubsubClient- Throws:
IOException
-
modifyAckDeadline
public void modifyAckDeadline(PubsubClient.SubscriptionPath subscription, List<String> ackIds, int deadlineSeconds) throws IOException Description copied from class:PubsubClientModify the ack deadline for messages fromsubscriptionwithackIdsto bedeadlineSecondsfrom now.- Specified by:
modifyAckDeadlinein classPubsubClient- Throws:
IOException
-
createTopic
Description copied from class:PubsubClientCreatetopic.- Specified by:
createTopicin classPubsubClient- Throws:
IOException
-
createTopic
public void createTopic(PubsubClient.TopicPath topic, PubsubClient.SchemaPath schema) throws IOException Description copied from class:PubsubClientCreate {link TopicPath} withPubsubClient.SchemaPath.- Specified by:
createTopicin classPubsubClient- Throws:
IOException
-
deleteTopic
- Specified by:
deleteTopicin classPubsubClient- Throws:
IOException
-
listTopics
Description copied from class:PubsubClientReturn a list of topics forproject.- Specified by:
listTopicsin classPubsubClient- Throws:
IOException
-
isTopicExists
Description copied from class:PubsubClientReturn true iftopicexists.- Specified by:
isTopicExistsin classPubsubClient- Throws:
IOException
-
createSubscription
public void createSubscription(PubsubClient.TopicPath topic, PubsubClient.SubscriptionPath subscription, int ackDeadlineSeconds) throws IOException Description copied from class:PubsubClientCreatesubscriptiontotopic.- Specified by:
createSubscriptionin classPubsubClient- Throws:
IOException
-
deleteSubscription
Description copied from class:PubsubClientDeletesubscription.- Specified by:
deleteSubscriptionin classPubsubClient- Throws:
IOException
-
listSubscriptions
public List<PubsubClient.SubscriptionPath> listSubscriptions(PubsubClient.ProjectPath project, PubsubClient.TopicPath topic) throws IOException Description copied from class:PubsubClientReturn a list of subscriptions fortopicinproject.- Specified by:
listSubscriptionsin classPubsubClient- Throws:
IOException
-
ackDeadlineSeconds
Description copied from class:PubsubClientReturn the ack deadline, in seconds, forsubscription.- Specified by:
ackDeadlineSecondsin classPubsubClient- Throws:
IOException
-
isEOF
public boolean isEOF()Description copied from class:PubsubClientReturn true ifPubsubClient.pull(long, org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath, int, boolean)will always return empty list. Actual clients will return false. Test clients may return true to signal that all expected messages have been pulled and the test may complete.- Specified by:
isEOFin classPubsubClient
-
createSchema
public void createSchema(PubsubClient.SchemaPath schemaPath, String schemaContent, com.google.pubsub.v1.Schema.Type type) throws IOException Description copied from class:PubsubClientCreateSchemafrom Schema definition content.- Specified by:
createSchemain classPubsubClient- Throws:
IOException
-
deleteSchema
DeletePubsubClient.SchemaPath.- Specified by:
deleteSchemain classPubsubClient- Throws:
IOException
-
getSchemaPath
Description copied from class:PubsubClientReturnPubsubClient.SchemaPathfromPubsubClient.TopicPathif exists.- Specified by:
getSchemaPathin classPubsubClient- Throws:
IOException
-
getSchema
Description copied from class:PubsubClientReturn a BeamSchemafrom the Pub/Sub schema resource, if exists.- Specified by:
getSchemain classPubsubClient- Throws:
IOException
-