Class PubSubTestClients

java.lang.Object
io.github.flink.gcp.connector.testutils.pubsub.PubSubTestClients
All Implemented Interfaces:
AutoCloseable

@Internal public final class PubSubTestClients extends Object implements AutoCloseable
The Pub/Sub admin, publish and pull machinery every integration-test harness needs, parameterised over the transport: a plaintext channel with no credentials against the emulator, or application-default credentials against the real service. The harnesses in the connector modules keep their connector-typed conveniences and delegate the client work here; everything on this class is stock com.google.* types, which is what lets the SQL module's smoke test drive the emulator with these clients while the connector under test uses its relocated copies.

Subscriptions are addressed by their full resource path ( projects/<p>/subscriptions/<s>) so no connector destination type crosses the module boundary.

  • Method Details

    • forEmulator

      public static PubSubTestClients forEmulator(String endpoint) throws IOException
      Clients over one plaintext channel to the emulator, with no credentials. The returned instance owns the channel and shuts it down on close().
      Throws:
      IOException
    • withApplicationDefaultCredentials

      public static PubSubTestClients withApplicationDefaultCredentials() throws IOException
      Clients on the SDK's defaults: application-default credentials, real service. The poll interval is looser than the emulator transport's because every pull is a network round trip.
      Throws:
      IOException
    • topicAdmin

      public com.google.cloud.pubsub.v1.TopicAdminClient topicAdmin()
    • subscriptionAdmin

      public com.google.cloud.pubsub.v1.SubscriptionAdminClient subscriptionAdmin()
    • publishOrdered

      public void publishOrdered(com.google.pubsub.v1.TopicName topic, String orderingKey, String publishEndpoint, String... payloads) throws IOException, InterruptedException, ExecutionException
      Publishes the payloads, optionally under one ordering key, and waits for the acknowledgements so the messages are durable before a source starts.
      Parameters:
      orderingKey - the ordering key, or null for unordered publishing
      publishEndpoint - overrides the publisher's endpoint, or null for the transport's default — the real-service ordering tests pass their regional endpoint here, because ordered publishing is only guaranteed through one
      Throws:
      IOException
      InterruptedException
      ExecutionException
    • pullAndAckUntil

      public Set<String> pullAndAckUntil(String subscriptionPath, int expected, Duration timeout) throws InterruptedException
      Pulls and acknowledges until expected distinct payloads have arrived or the deadline passes, returning what did arrive.

      A single pull is not guaranteed to return everything outstanding even when more is available, so an exact-count assertion on one pull would be flaky. Each pending pull has a 30-second RPC bound; a pull started before the observation deadline may finish after it. A nonresponsive pull fails rather than establishing that a subscription is empty.

      Throws:
      InterruptedException
    • pullMessagesUntil

      public List<com.google.pubsub.v1.PubsubMessage> pullMessagesUntil(String subscriptionPath, int expected, Duration timeout) throws InterruptedException
      Pulls and acknowledges until expected distinct messages have arrived or the deadline passes, returning them whole — attributes, publish time and ordering key included, which pullAndAckUntil(java.lang.String, int, java.time.Duration) discards.

      Distinct by message id, for the same reason pullAndAckUntil(java.lang.String, int, java.time.Duration) collects into a set: an acknowledgement is not necessarily applied before the next pull is served, and the sinks are at-least-once, so the same message can come back. Counting redeliveries would make every exact-count assertion a coin flip. Each pending pull has the same 30-second RPC bound as pullAndAckUntil(java.lang.String, int, java.time.Duration); the observation deadline is checked between completed pulls.

      Throws:
      InterruptedException
    • pullMessagesAndAck

      public List<com.google.pubsub.v1.PubsubMessage> pullMessagesAndAck(String subscriptionPath, int maxMessages)
      Pulls up to maxMessages and acknowledges them, returning the messages. One pull only — use pullMessagesUntil(java.lang.String, int, java.time.Duration) to assert on a known count.
    • pullMessages

      public List<com.google.pubsub.v1.PubsubMessage> pullMessages(String subscriptionPath, int maxMessages)
      Pulls up to maxMessages without acknowledging, returning the messages — for tests that must leave the subscription's state untouched, at the cost of the next pull waiting out the acknowledgement deadline before a redelivery.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable