Class PubsubTestClient
- java.lang.Object
-
- org.apache.beam.sdk.io.gcp.pubsub.PubsubClient
-
- org.apache.beam.sdk.io.gcp.pubsub.PubsubTestClient
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Serializable,java.lang.AutoCloseable
public class PubsubTestClient extends PubsubClient implements java.io.Serializable
A (partial) implementation ofPubsubClientfor use by unit tests. Only suitable for testingpublish(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>)andmodifyAckDeadline(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:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfacePubsubTestClient.PubsubTestClientFactoryClosing 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 Constructor Description PubsubTestClient()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intackDeadlineSeconds(PubsubClient.SubscriptionPath subscription)Return the ack deadline, in seconds, forsubscription.voidacknowledge(PubsubClient.SubscriptionPath subscription, java.util.List<java.lang.String> ackIds)Acknowldege messages fromsubscriptionwithackIds.voidadvance()For subscription mode only: Track progression of time according to theClockpassed .voidclose()static PubsubTestClient.PubsubTestClientFactorycreateFactoryForCreateSubscription()static PubsubTestClient.PubsubTestClientFactorycreateFactoryForGetSchema(PubsubClient.TopicPath expectedTopic, @Nullable PubsubClient.SchemaPath expectedSchemaPath, @Nullable org.apache.beam.sdk.schemas.Schema expectedSchema)static PubsubTestClient.PubsubTestClientFactorycreateFactoryForPublish(@Nullable PubsubClient.TopicPath expectedTopic, java.lang.Iterable<PubsubClient.OutgoingMessage> expectedOutgoingMessages, java.lang.Iterable<PubsubClient.OutgoingMessage> failingOutgoingMessages)Return a factory for testing publishers.static PubsubTestClient.PubsubTestClientFactorycreateFactoryForPull(com.google.api.client.util.Clock clock, PubsubClient.SubscriptionPath expectedSubscription, int ackTimeoutSec, java.lang.Iterable<PubsubClient.IncomingMessage> expectedIncomingMessages)Return a factory for testing subscribers.static PubsubTestClient.PubsubTestClientFactorycreateFactoryForPullAndPublish(PubsubClient.SubscriptionPath pullSubscription, PubsubClient.TopicPath publishTopicPath, com.google.api.client.util.Clock pullClock, int pullAckTimeoutSec, java.lang.Iterable<PubsubClient.IncomingMessage> expectedIncomingMessages, java.lang.Iterable<PubsubClient.OutgoingMessage> expectedOutgoingMessages, java.lang.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, java.lang.String schemaContent, com.google.pubsub.v1.Schema.Type type)CreateSchemafrom Schema definition content.voidcreateSubscription(PubsubClient.TopicPath topic, PubsubClient.SubscriptionPath subscription, int ackDeadlineSeconds)Createsubscriptiontotopic.voidcreateTopic(PubsubClient.TopicPath topic)Createtopic.voidcreateTopic(PubsubClient.TopicPath topic, PubsubClient.SchemaPath schema)Create {link TopicPath} withPubsubClient.SchemaPath.voiddeleteSchema(PubsubClient.SchemaPath schemaPath)DeletePubsubClient.SchemaPath.voiddeleteSubscription(PubsubClient.SubscriptionPath subscription)Deletesubscription.voiddeleteTopic(PubsubClient.TopicPath topic)org.apache.beam.sdk.schemas.SchemagetSchema(PubsubClient.SchemaPath schemaPath)Return a BeamSchemafrom the Pub/Sub schema resource, if exists.PubsubClient.SchemaPathgetSchemaPath(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.booleanisTopicExists(PubsubClient.TopicPath topic)Return true iftopicexists.java.util.List<PubsubClient.SubscriptionPath>listSubscriptions(PubsubClient.ProjectPath project, PubsubClient.TopicPath topic)Return a list of subscriptions fortopicinproject.java.util.List<PubsubClient.TopicPath>listTopics(PubsubClient.ProjectPath project)Return a list of topics forproject.voidmodifyAckDeadline(PubsubClient.SubscriptionPath subscription, java.util.List<java.lang.String> ackIds, int deadlineSeconds)Modify the ack deadline for messages fromsubscriptionwithackIdsto bedeadlineSecondsfrom now.intpublish(PubsubClient.TopicPath topic, java.util.List<PubsubClient.OutgoingMessage> outgoingMessages)PublishoutgoingMessagesto Pubsubtopic.java.util.List<PubsubClient.IncomingMessage>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
-
-
-
-
Method Detail
-
createFactoryForPublish
public static PubsubTestClient.PubsubTestClientFactory createFactoryForPublish(@Nullable PubsubClient.TopicPath expectedTopic, java.lang.Iterable<PubsubClient.OutgoingMessage> expectedOutgoingMessages, java.lang.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, java.lang.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, java.lang.Iterable<PubsubClient.IncomingMessage> expectedIncomingMessages, java.lang.Iterable<PubsubClient.OutgoingMessage> expectedOutgoingMessages, java.lang.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 org.apache.beam.sdk.schemas.Schema expectedSchema)
-
createFactoryForCreateSubscription
public static PubsubTestClient.PubsubTestClientFactory 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 interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
publish
public int publish(PubsubClient.TopicPath topic, java.util.List<PubsubClient.OutgoingMessage> outgoingMessages) throws java.io.IOException
Description copied from class:PubsubClientPublishoutgoingMessagesto Pubsubtopic. Return number of messages published.- Specified by:
publishin classPubsubClient- Throws:
java.io.IOException
-
pull
public java.util.List<PubsubClient.IncomingMessage> pull(long requestTimeMsSinceEpoch, PubsubClient.SubscriptionPath subscription, int batchSize, boolean returnImmediately) throws java.io.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:
java.io.IOException
-
acknowledge
public void acknowledge(PubsubClient.SubscriptionPath subscription, java.util.List<java.lang.String> ackIds) throws java.io.IOException
Description copied from class:PubsubClientAcknowldege messages fromsubscriptionwithackIds.- Specified by:
acknowledgein classPubsubClient- Throws:
java.io.IOException
-
modifyAckDeadline
public void modifyAckDeadline(PubsubClient.SubscriptionPath subscription, java.util.List<java.lang.String> ackIds, int deadlineSeconds) throws java.io.IOException
Description copied from class:PubsubClientModify the ack deadline for messages fromsubscriptionwithackIdsto bedeadlineSecondsfrom now.- Specified by:
modifyAckDeadlinein classPubsubClient- Throws:
java.io.IOException
-
createTopic
public void createTopic(PubsubClient.TopicPath topic) throws java.io.IOException
Description copied from class:PubsubClientCreatetopic.- Specified by:
createTopicin classPubsubClient- Throws:
java.io.IOException
-
createTopic
public void createTopic(PubsubClient.TopicPath topic, PubsubClient.SchemaPath schema) throws java.io.IOException
Description copied from class:PubsubClientCreate {link TopicPath} withPubsubClient.SchemaPath.- Specified by:
createTopicin classPubsubClient- Throws:
java.io.IOException
-
deleteTopic
public void deleteTopic(PubsubClient.TopicPath topic) throws java.io.IOException
- Specified by:
deleteTopicin classPubsubClient- Throws:
java.io.IOException
-
listTopics
public java.util.List<PubsubClient.TopicPath> listTopics(PubsubClient.ProjectPath project) throws java.io.IOException
Description copied from class:PubsubClientReturn a list of topics forproject.- Specified by:
listTopicsin classPubsubClient- Throws:
java.io.IOException
-
isTopicExists
public boolean isTopicExists(PubsubClient.TopicPath topic) throws java.io.IOException
Description copied from class:PubsubClientReturn true iftopicexists.- Specified by:
isTopicExistsin classPubsubClient- Throws:
java.io.IOException
-
createSubscription
public void createSubscription(PubsubClient.TopicPath topic, PubsubClient.SubscriptionPath subscription, int ackDeadlineSeconds) throws java.io.IOException
Description copied from class:PubsubClientCreatesubscriptiontotopic.- Specified by:
createSubscriptionin classPubsubClient- Throws:
java.io.IOException
-
deleteSubscription
public void deleteSubscription(PubsubClient.SubscriptionPath subscription) throws java.io.IOException
Description copied from class:PubsubClientDeletesubscription.- Specified by:
deleteSubscriptionin classPubsubClient- Throws:
java.io.IOException
-
listSubscriptions
public java.util.List<PubsubClient.SubscriptionPath> listSubscriptions(PubsubClient.ProjectPath project, PubsubClient.TopicPath topic) throws java.io.IOException
Description copied from class:PubsubClientReturn a list of subscriptions fortopicinproject.- Specified by:
listSubscriptionsin classPubsubClient- Throws:
java.io.IOException
-
ackDeadlineSeconds
public int ackDeadlineSeconds(PubsubClient.SubscriptionPath subscription) throws java.io.IOException
Description copied from class:PubsubClientReturn the ack deadline, in seconds, forsubscription.- Specified by:
ackDeadlineSecondsin classPubsubClient- Throws:
java.io.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, java.lang.String schemaContent, com.google.pubsub.v1.Schema.Type type) throws java.io.IOException
Description copied from class:PubsubClientCreateSchemafrom Schema definition content.- Specified by:
createSchemain classPubsubClient- Throws:
java.io.IOException
-
deleteSchema
public void deleteSchema(PubsubClient.SchemaPath schemaPath) throws java.io.IOException
DeletePubsubClient.SchemaPath.- Specified by:
deleteSchemain classPubsubClient- Throws:
java.io.IOException
-
getSchemaPath
public PubsubClient.SchemaPath getSchemaPath(PubsubClient.TopicPath topicPath) throws java.io.IOException
Description copied from class:PubsubClientReturnPubsubClient.SchemaPathfromPubsubClient.TopicPathif exists.- Specified by:
getSchemaPathin classPubsubClient- Throws:
java.io.IOException
-
getSchema
public org.apache.beam.sdk.schemas.Schema getSchema(PubsubClient.SchemaPath schemaPath) throws java.io.IOException
Description copied from class:PubsubClientReturn a BeamSchemafrom the Pub/Sub schema resource, if exists.- Specified by:
getSchemain classPubsubClient- Throws:
java.io.IOException
-
-