Search in sources :

Example 1 with Clock

use of com.google.api.client.util.Clock in project google-api-java-client by google.

the class GoogleIdTokenVerifierTest method testBuilder.

public void testBuilder() throws Exception {
    GoogleIdTokenVerifier.Builder builder = new GoogleIdTokenVerifier.Builder(new GooglePublicKeysManagerTest.PublicCertsMockHttpTransport(), new JacksonFactory()).setIssuer(ISSUER).setAudience(TRUSTED_CLIENT_IDS);
    assertEquals(Clock.SYSTEM, builder.getClock());
    assertEquals(ISSUER, builder.getIssuer());
    assertTrue(TRUSTED_CLIENT_IDS.equals(builder.getAudience()));
    Clock clock = new FixedClock(4);
    builder.setClock(clock);
    assertEquals(clock, builder.getClock());
    IdTokenVerifier verifier = builder.build();
    assertEquals(clock, verifier.getClock());
    assertEquals(ISSUER, verifier.getIssuer());
    assertEquals(TRUSTED_CLIENT_IDS, Lists.newArrayList(verifier.getAudience()));
}
Also used : FixedClock(com.google.api.client.testing.http.FixedClock) Clock(com.google.api.client.util.Clock) FixedClock(com.google.api.client.testing.http.FixedClock) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) IdTokenVerifier(com.google.api.client.auth.openidconnect.IdTokenVerifier)

Example 2 with Clock

use of com.google.api.client.util.Clock in project beam by apache.

the class PubsubTestClientTest method pullOneMessage.

@Test
public void pullOneMessage() throws IOException {
    final AtomicLong now = new AtomicLong();
    Clock clock = now::get;
    IncomingMessage expectedIncomingMessage = IncomingMessage.of(PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(DATA)).build(), MESSAGE_TIME, REQ_TIME, ACK_ID, MESSAGE_ID);
    try (PubsubTestClientFactory factory = PubsubTestClient.createFactoryForPull(clock, SUBSCRIPTION, ACK_TIMEOUT_S, Lists.newArrayList(expectedIncomingMessage))) {
        try (PubsubTestClient client = (PubsubTestClient) factory.newClient(null, null, null)) {
            now.set(REQ_TIME);
            client.advance();
            List<IncomingMessage> incomingMessages = client.pull(now.get(), SUBSCRIPTION, 1, true);
            assertEquals(1, incomingMessages.size());
            assertEquals(expectedIncomingMessage, incomingMessages.get(0));
            // Timeout on ACK.
            now.addAndGet((ACK_TIMEOUT_S + 10) * 1000);
            client.advance();
            incomingMessages = client.pull(now.get(), SUBSCRIPTION, 1, true);
            assertEquals(1, incomingMessages.size());
            assertEquals(IncomingMessage.of(expectedIncomingMessage.message(), expectedIncomingMessage.timestampMsSinceEpoch(), now.get(), expectedIncomingMessage.ackId(), expectedIncomingMessage.recordId()), incomingMessages.get(0));
            now.addAndGet(10 * 1000);
            client.advance();
            // Extend ack
            client.modifyAckDeadline(SUBSCRIPTION, ImmutableList.of(ACK_ID), 20);
            // Timeout on extended ACK
            now.addAndGet(30 * 1000);
            client.advance();
            incomingMessages = client.pull(now.get(), SUBSCRIPTION, 1, true);
            assertEquals(1, incomingMessages.size());
            assertEquals(IncomingMessage.of(expectedIncomingMessage.message(), expectedIncomingMessage.timestampMsSinceEpoch(), now.get(), expectedIncomingMessage.ackId(), expectedIncomingMessage.recordId()), incomingMessages.get(0));
            // Extend ack
            client.modifyAckDeadline(SUBSCRIPTION, ImmutableList.of(ACK_ID), 20);
            // Ack
            now.addAndGet(15 * 1000);
            client.advance();
            client.acknowledge(SUBSCRIPTION, ImmutableList.of(ACK_ID));
        }
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) IncomingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.IncomingMessage) Clock(com.google.api.client.util.Clock) PubsubTestClientFactory(org.apache.beam.sdk.io.gcp.pubsub.PubsubTestClient.PubsubTestClientFactory) Test(org.junit.Test)

Example 3 with Clock

use of com.google.api.client.util.Clock in project beam by apache.

the class PubsubTestClientTest method testPullThenPublish.

@Test
public void testPullThenPublish() throws IOException {
    AtomicLong now = new AtomicLong();
    Clock clock = now::get;
    PubsubMessage message = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(DATA)).build();
    IncomingMessage expectedIncomingMessage = IncomingMessage.of(message, MESSAGE_TIME, REQ_TIME, ACK_ID, MESSAGE_ID);
    OutgoingMessage expectedOutgoingMessage = OutgoingMessage.of(message, MESSAGE_TIME, MESSAGE_ID);
    try (PubsubTestClientFactory factory = PubsubTestClient.createFactoryForPullAndPublish(SUBSCRIPTION, TOPIC, clock, ACK_TIMEOUT_S, ImmutableList.of(expectedIncomingMessage), ImmutableList.of(expectedOutgoingMessage), ImmutableList.of())) {
        try (PubsubTestClient client = (PubsubTestClient) factory.newClient(null, null, null)) {
            // Pull
            now.set(REQ_TIME);
            client.advance();
            List<IncomingMessage> actualIncomingMessages = client.pull(now.get(), SUBSCRIPTION, 1, true);
            now.addAndGet(ACK_TIMEOUT_S - 10);
            client.advance();
            client.acknowledge(SUBSCRIPTION, ImmutableList.of(ACK_ID));
            assertEquals(1, actualIncomingMessages.size());
            assertEquals(expectedIncomingMessage, actualIncomingMessages.get(0));
            // Publish
            IncomingMessage incomingMessage = actualIncomingMessages.get(0);
            OutgoingMessage actualOutgoingMessage = OutgoingMessage.of(incomingMessage.message(), incomingMessage.timestampMsSinceEpoch(), incomingMessage.recordId());
            client.publish(TOPIC, ImmutableList.of(actualOutgoingMessage));
        }
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) OutgoingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage) IncomingMessage(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.IncomingMessage) Clock(com.google.api.client.util.Clock) PubsubTestClientFactory(org.apache.beam.sdk.io.gcp.pubsub.PubsubTestClient.PubsubTestClientFactory) PubsubMessage(com.google.pubsub.v1.PubsubMessage) Test(org.junit.Test)

Aggregations

Clock (com.google.api.client.util.Clock)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 IncomingMessage (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.IncomingMessage)2 PubsubTestClientFactory (org.apache.beam.sdk.io.gcp.pubsub.PubsubTestClient.PubsubTestClientFactory)2 Test (org.junit.Test)2 IdTokenVerifier (com.google.api.client.auth.openidconnect.IdTokenVerifier)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 FixedClock (com.google.api.client.testing.http.FixedClock)1 PubsubMessage (com.google.pubsub.v1.PubsubMessage)1 OutgoingMessage (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.OutgoingMessage)1