use of io.zeebe.test.util.TestUtil.waitUntil in project zeebe by zeebe-io.
the class TaskSubscriptionTest method shouldNotLoseCreditsOnFailureToReportTaskFailure.
/**
* i.e. if signalling task failure itself fails
*/
@Test
public void shouldNotLoseCreditsOnFailureToReportTaskFailure() throws InterruptedException {
// given
broker.stubTaskSubscriptionApi(123L);
failTaskFailure();
final int subscriptionCapacity = 8;
final AtomicInteger failedTasks = new AtomicInteger(0);
final TaskHandler taskHandler = (c, t) -> {
failedTasks.incrementAndGet();
throw new RuntimeException("foo");
};
clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(taskHandler).lockOwner("owner").lockTime(1000L).taskFetchSize(subscriptionCapacity).taskType("foo").open();
final RemoteAddress clientAddress = broker.getReceivedControlMessageRequests().get(0).getSource();
for (int i = 0; i < subscriptionCapacity; i++) {
broker.pushLockedTask(clientAddress, 123L, i, i, "owner", "foo");
}
// when
TestUtil.waitUntil(() -> failedTasks.get() == 8);
// give the client a bit of time to submit credits; this is not coupled to any defined event, so we just sleep for a bit
Thread.sleep(500L);
// then
final List<ControlMessageRequest> creditRequests = getCreditRequests().collect(Collectors.toList());
assertThat(creditRequests).isNotEmpty();
final int numSubmittedCredits = creditRequests.stream().mapToInt((r) -> (int) r.getData().get("credits")).sum();
assertThat(numSubmittedCredits).isGreaterThan(0);
}
use of io.zeebe.test.util.TestUtil.waitUntil in project zeebe by zeebe-io.
the class TaskSubscriptionTest method shouldCloseSubscriptionOnChannelClose.
@Test
public void shouldCloseSubscriptionOnChannelClose() throws InterruptedException {
// given
broker.stubTaskSubscriptionApi(123L);
final TaskSubscription subscription = clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler((c, t) -> c.complete(t).withoutPayload().execute()).lockOwner("owner").lockTime(1000L).taskFetchSize(5).taskType("foo").open();
// when
broker.closeTransport();
// let subscriber attempt reopening
Thread.sleep(500L);
// make request time out immediately
clientRule.getClock().addTime(Duration.ofSeconds(60));
// then
TestUtil.waitUntil(() -> subscription.isClosed());
assertThat(subscription.isClosed()).isTrue();
}
use of io.zeebe.test.util.TestUtil.waitUntil in project zeebe by zeebe-io.
the class TaskSubscriptionTest method shouldRetryWithMoreTasksThanSubscriptionCapacity.
/**
* This tests a case that should not occur under normal circumstances, but might occur
* in case of inconsistencies between broker and client state (e.g. due to bugs in either of them)
*/
@Test
public void shouldRetryWithMoreTasksThanSubscriptionCapacity() throws InterruptedException {
// given
broker.stubTaskSubscriptionApi(123L);
broker.onExecuteCommandRequest(EventType.TASK_EVENT, "COMPLETE").respondWith().key((r) -> r.key()).event().allOf((r) -> r.getCommand()).put("state", "COMPLETED").done().register();
final WaitingTaskHandler handler = new WaitingTaskHandler();
final Properties clientProperties = ((ZeebeClientImpl) client).getInitializationProperties();
final int numExecutionThreads = Integer.parseInt(clientProperties.getProperty(ClientProperties.CLIENT_SUBSCRIPTION_EXECUTION_THREADS));
final int taskCapacity = 4;
clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(handler).lockOwner("owner").lockTime(1000L).taskFetchSize(taskCapacity).taskType("foo").open();
final RemoteAddress clientAddress = broker.getReceivedControlMessageRequests().get(0).getSource();
for (int i = 0; i < taskCapacity + numExecutionThreads; i++) {
broker.pushLockedTask(clientAddress, 123L, i, i, "owner", "foo");
}
TestUtil.waitUntil(() -> handler.numWaitingThreads.get() > 0);
// pushing one more event, exceeding client capacity
broker.pushLockedTask(clientAddress, 123L, Integer.MAX_VALUE, Integer.MAX_VALUE, "owner", "foo");
// waiting for the client to receive all pending tasks
Thread.sleep(500L);
// when
handler.shouldWait = false;
continueTaskHandlingThreads();
// then the additional event is handled nevertheless (i.e. client applies backpressure)
TestUtil.waitUntil(() -> handler.numHandledEvents.get() == taskCapacity + numExecutionThreads + 1);
}
use of io.zeebe.test.util.TestUtil.waitUntil in project zeebe by zeebe-io.
the class TopicSubscriptionTest method shouldContinueEventHandlingAfterSuccessfulRetry.
@Test
public void shouldContinueEventHandlingAfterSuccessfulRetry() {
// given
broker.stubTopicSubscriptionApi(123L);
final AtomicInteger counter = new AtomicInteger(3);
final FailingHandler handler = new FailingHandler(e -> e.getMetadata().getPosition() == 1L && counter.decrementAndGet() > 0);
clientRule.topics().newSubscription(clientRule.getDefaultTopicName()).startAtHeadOfTopic().handler(handler).name(SUBSCRIPTION_NAME).open();
final RemoteAddress clientAddress = broker.getReceivedCommandRequests().get(0).getSource();
broker.pushTopicEvent(clientAddress, 123L, 1L, 1L);
// when
broker.pushTopicEvent(clientAddress, 123L, 1L, 2L);
// then
TestUtil.waitUntil(() -> handler.getRecordedEvents().stream().anyMatch(re -> re.getMetadata().getPosition() == 2L));
final List<Long> handledEventPositions = handler.getRecordedEvents().stream().map((re) -> re.getMetadata().getPosition()).collect(Collectors.toList());
assertThat(handledEventPositions).containsExactly(1L, 1L, 1L, 2L);
}
use of io.zeebe.test.util.TestUtil.waitUntil in project zeebe by zeebe-io.
the class TopicSubscriptionTest method shouldOnlyAcknowledgeEventAndCloseSubscriptionAfterLastEventHasBeenHandled.
@Test
public void shouldOnlyAcknowledgeEventAndCloseSubscriptionAfterLastEventHasBeenHandled() throws InterruptedException, ExecutionException, TimeoutException {
// given
broker.stubTopicSubscriptionApi(123L);
final ControllableHandler handler = new ControllableHandler();
final TopicSubscriberGroup subscription = (TopicSubscriberGroup) clientRule.topics().newSubscription(clientRule.getDefaultTopicName()).startAtHeadOfTopic().handler(handler).name(SUBSCRIPTION_NAME).open();
final RemoteAddress clientAddress = broker.getReceivedCommandRequests().get(0).getSource();
broker.pushTopicEvent(clientAddress, 123L, 1L, 1L);
TestUtil.waitUntil(() -> handler.isWaiting());
// when
final ActorFuture<?> closeFuture = subscription.closeAsync();
// then
Thread.sleep(1000L);
assertThat(closeFuture).isNotDone();
boolean hasSentAck = broker.getReceivedCommandRequests().stream().filter((r) -> r.eventType() == EventType.SUBSCRIPTION_EVENT).findAny().isPresent();
assertThat(hasSentAck).isFalse();
// and when
handler.signal();
// then
closeFuture.get(1L, TimeUnit.SECONDS);
// and
hasSentAck = broker.getReceivedCommandRequests().stream().filter((r) -> r.eventType() == EventType.SUBSCRIPTION_EVENT).findAny().isPresent();
assertThat(hasSentAck).isTrue();
}
Aggregations