com.google.api.client.util.Clock

Here are the examples of the java api class com.google.api.client.util.Clock taken from open source projects.

1. PubsubUnboundedSourceTest#setupOneMessage()

View license
private void setupOneMessage(Iterable<IncomingMessage> incoming) {
    now = new AtomicLong(REQ_TIME);
    clock = new Clock() {

        @Override
        public long currentTimeMillis() {
            return now.get();
        }
    };
    factory = PubsubTestClient.createFactoryForPull(clock, SUBSCRIPTION, ACK_TIMEOUT_S, incoming);
    PubsubUnboundedSource<String> source = new PubsubUnboundedSource<>(clock, factory, null, null, SUBSCRIPTION, StringUtf8Coder.of(), TIMESTAMP_LABEL, ID_LABEL);
    primSource = new PubsubSource<>(source);
}

2. GoogleIdTokenVerifierTest#testBuilder()

View license
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()));
}

3. IdTokenVerifierTest#testBuilder()

View license
public void testBuilder() throws Exception {
    IdTokenVerifier.Builder builder = new IdTokenVerifier.Builder().setIssuer(ISSUER).setAudience(TRUSTED_CLIENT_IDS);
    assertEquals(Clock.SYSTEM, builder.getClock());
    assertEquals(ISSUER, builder.getIssuer());
    assertEquals(Collections.singleton(ISSUER), builder.getIssuers());
    assertTrue(TRUSTED_CLIENT_IDS.equals(builder.getAudience()));
    Clock clock = new MyClock();
    builder.setClock(clock);
    assertEquals(clock, builder.getClock());
    IdTokenVerifier verifier = builder.build();
    assertEquals(clock, verifier.getClock());
    assertEquals(ISSUER, verifier.getIssuer());
    assertEquals(Collections.singleton(ISSUER), builder.getIssuers());
    assertEquals(TRUSTED_CLIENT_IDS, Lists.newArrayList(verifier.getAudience()));
}

4. PubsubUnboundedSourceTest#setupOneMessage()

View license
private void setupOneMessage(Iterable<IncomingMessage> incoming) {
    now = new AtomicLong(REQ_TIME);
    clock = new Clock() {

        @Override
        public long currentTimeMillis() {
            return now.get();
        }
    };
    factory = PubsubTestClient.createFactoryForPull(clock, SUBSCRIPTION, ACK_TIMEOUT_S, incoming);
    PubsubUnboundedSource<String> source = new PubsubUnboundedSource<>(clock, factory, null, null, SUBSCRIPTION, StringUtf8Coder.of(), TIMESTAMP_LABEL, ID_LABEL);
    primSource = new PubsubSource<>(source);
}

5. PubsubTestClientTest#pullOneMessage()

View license
@Test
public void pullOneMessage() throws IOException {
    final AtomicLong now = new AtomicLong();
    Clock clock = new Clock() {

        @Override
        public long currentTimeMillis() {
            return now.get();
        }
    };
    IncomingMessage expectedIncomingMessage = new IncomingMessage(DATA.getBytes(), 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(expectedIncomingMessage.withRequestTime(now.get()), 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(expectedIncomingMessage.withRequestTime(now.get()), 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));
        }
    }
}

6. PubsubTestClientTest#pullOneMessage()

View license
@Test
public void pullOneMessage() throws IOException {
    final AtomicLong now = new AtomicLong();
    Clock clock = new Clock() {

        @Override
        public long currentTimeMillis() {
            return now.get();
        }
    };
    IncomingMessage expectedIncomingMessage = new IncomingMessage(DATA.getBytes(), 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(expectedIncomingMessage.withRequestTime(now.get()), 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(expectedIncomingMessage.withRequestTime(now.get()), 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));
        }
    }
}