Search in sources :

Example 1 with ModifyAckDeadline

use of com.google.cloud.pubsub.spi.v1.FakeSubscriberServiceImpl.ModifyAckDeadline in project google-cloud-java by GoogleCloudPlatform.

the class SubscriberTest method testModifyAckDeadline_defaultMaxExtensionPeriod.

@Test
public void testModifyAckDeadline_defaultMaxExtensionPeriod() throws Exception {
    Subscriber subscriber = startSubscriber(getTestSubscriberBuilder(testReceiver).setAckExpirationPadding(Duration.ofSeconds(1)));
    // Send messages to be acked
    List<String> testAckIdsBatch = ImmutableList.of("A", "B", "C");
    testReceiver.setExplicitAck(true);
    // A modify ack deadline should be schedule for the next 9s
    fakeExecutor.setupScheduleExpectation(Duration.ofSeconds(9));
    sendMessages(testAckIdsBatch);
    // To ensure the first modify ack deadlines got scheduled
    fakeExecutor.waitForExpectedWork();
    // Next modify ack deadline should be schedule in the next 1s
    fakeExecutor.advanceTime(Duration.ofSeconds(9));
    assertEquivalentWithTransformation(testAckIdsBatch, fakeSubscriberServiceImpl.waitAndConsumeModifyAckDeadlines(3), new Function<String, ModifyAckDeadline>() {

        @Override
        public ModifyAckDeadline apply(String ack) {
            return new ModifyAckDeadline(ack, INITIAL_ACK_DEADLINE_EXTENSION_SECS);
        }
    });
    fakeExecutor.advanceTime(Duration.ofSeconds(1));
    // Second time increment
    int timeIncrementSecs = INITIAL_ACK_DEADLINE_EXTENSION_SECS;
    // Check ack deadline extensions while the current time has not reached 60 minutes
    while (fakeExecutor.getClock().millisTime() + timeIncrementSecs - 1 < 1000 * 60 * 60) {
        timeIncrementSecs *= 2;
        final int expectedIncrementSecs = Math.min(600, timeIncrementSecs);
        assertEquivalentWithTransformation(testAckIdsBatch, fakeSubscriberServiceImpl.waitAndConsumeModifyAckDeadlines(3), new Function<String, ModifyAckDeadline>() {

            @Override
            public ModifyAckDeadline apply(String ack) {
                return new ModifyAckDeadline(ack, expectedIncrementSecs);
            }
        });
        fakeExecutor.advanceTime(Duration.ofSeconds(timeIncrementSecs - 1));
    }
    // No more modify ack deadline extension should be triggered at this point
    fakeExecutor.advanceTime(Duration.ofSeconds(20));
    assertTrue(fakeSubscriberServiceImpl.getModifyAckDeadlines().isEmpty());
    testReceiver.replyAllOutstandingMessage();
    subscriber.stopAsync().awaitTerminated();
}
Also used : ModifyAckDeadline(com.google.cloud.pubsub.spi.v1.FakeSubscriberServiceImpl.ModifyAckDeadline) Test(org.junit.Test)

Example 2 with ModifyAckDeadline

use of com.google.cloud.pubsub.spi.v1.FakeSubscriberServiceImpl.ModifyAckDeadline in project google-cloud-java by GoogleCloudPlatform.

the class SubscriberTest method testModifyAckDeadline.

@Test
public void testModifyAckDeadline() throws Exception {
    Subscriber subscriber = startSubscriber(getTestSubscriberBuilder(testReceiver).setAckExpirationPadding(Duration.ofSeconds(1)).setMaxAckExtensionPeriod(Duration.ofSeconds(13)));
    // Send messages to be acked
    List<String> testAckIdsBatch = ImmutableList.of("A", "B", "C");
    testReceiver.setExplicitAck(true);
    // A modify ack deadline should be scheduled for the next 9s
    fakeExecutor.setupScheduleExpectation(Duration.ofSeconds(9));
    sendMessages(testAckIdsBatch);
    // To ensure first modify ack deadline got scheduled
    fakeExecutor.waitForExpectedWork();
    fakeExecutor.advanceTime(Duration.ofSeconds(9));
    assertEquivalentWithTransformation(testAckIdsBatch, fakeSubscriberServiceImpl.waitAndConsumeModifyAckDeadlines(3), new Function<String, ModifyAckDeadline>() {

        @Override
        public ModifyAckDeadline apply(String ack) {
            return new ModifyAckDeadline(ack, INITIAL_ACK_DEADLINE_EXTENSION_SECS);
        }
    });
    fakeExecutor.advanceTime(Duration.ofSeconds(1));
    assertEquivalentWithTransformation(testAckIdsBatch, fakeSubscriberServiceImpl.waitAndConsumeModifyAckDeadlines(3), new Function<String, ModifyAckDeadline>() {

        @Override
        public ModifyAckDeadline apply(String ack) {
            // It is expected that the deadline is renewed
            return new ModifyAckDeadline(ack, 3);
        // only three more seconds to not pass the max
        // ack deadline ext.
        }
    });
    // No more modify ack deadline extension should be triggered at this point
    fakeExecutor.advanceTime(Duration.ofSeconds(20));
    assertTrue(fakeSubscriberServiceImpl.getModifyAckDeadlines().isEmpty());
    testReceiver.replyAllOutstandingMessage();
    subscriber.stopAsync().awaitTerminated();
}
Also used : ModifyAckDeadline(com.google.cloud.pubsub.spi.v1.FakeSubscriberServiceImpl.ModifyAckDeadline) Test(org.junit.Test)

Example 3 with ModifyAckDeadline

use of com.google.cloud.pubsub.spi.v1.FakeSubscriberServiceImpl.ModifyAckDeadline in project google-cloud-java by GoogleCloudPlatform.

the class SubscriberTest method testReceiverError_NacksMessage.

@Test
public void testReceiverError_NacksMessage() throws Exception {
    testReceiver.setErrorReply(new RuntimeException("Can't process message"));
    Subscriber subscriber = startSubscriber(getTestSubscriberBuilder(testReceiver));
    sendMessages(ImmutableList.of("A"));
    // Trigger nack sending
    subscriber.stopAsync().awaitTerminated();
    assertEquivalent(ImmutableList.of(new ModifyAckDeadline("A", 0)), fakeSubscriberServiceImpl.waitAndConsumeModifyAckDeadlines(1));
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) ModifyAckDeadline(com.google.cloud.pubsub.spi.v1.FakeSubscriberServiceImpl.ModifyAckDeadline) Test(org.junit.Test)

Aggregations

ModifyAckDeadline (com.google.cloud.pubsub.spi.v1.FakeSubscriberServiceImpl.ModifyAckDeadline)3 Test (org.junit.Test)3 StatusRuntimeException (io.grpc.StatusRuntimeException)1