Search in sources :

Example 1 with TestUtil.waitUntil

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);
}
Also used : TasksClient(io.zeebe.client.TasksClient) TaskEvent(io.zeebe.client.event.TaskEvent) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) HashMap(java.util.HashMap) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) ErrorCode(io.zeebe.protocol.clientapi.ErrorCode) RemoteAddress(io.zeebe.transport.RemoteAddress) ClientRule(io.zeebe.client.util.ClientRule) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) ExpectedException(org.junit.rules.ExpectedException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Before(org.junit.Before) MsgPackHelper(io.zeebe.test.broker.protocol.MsgPackHelper) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Properties(java.util.Properties) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Test(org.junit.Test) TaskSubscription(io.zeebe.client.task.TaskSubscription) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) Collectors(java.util.stream.Collectors) TasksClientImpl(io.zeebe.client.impl.TasksClientImpl) SubscriptionType(io.zeebe.protocol.clientapi.SubscriptionType) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) EventType(io.zeebe.protocol.clientapi.EventType) ClientProperties(io.zeebe.client.ClientProperties) TaskHandler(io.zeebe.client.task.TaskHandler) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Subscriber(io.zeebe.client.task.impl.subscription.Subscriber) TestUtil(io.zeebe.test.util.TestUtil) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) RemoteAddress(io.zeebe.transport.RemoteAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TaskHandler(io.zeebe.client.task.TaskHandler) Test(org.junit.Test)

Example 2 with TestUtil.waitUntil

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();
}
Also used : TasksClient(io.zeebe.client.TasksClient) TaskEvent(io.zeebe.client.event.TaskEvent) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) HashMap(java.util.HashMap) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) ErrorCode(io.zeebe.protocol.clientapi.ErrorCode) RemoteAddress(io.zeebe.transport.RemoteAddress) ClientRule(io.zeebe.client.util.ClientRule) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) ExpectedException(org.junit.rules.ExpectedException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Before(org.junit.Before) MsgPackHelper(io.zeebe.test.broker.protocol.MsgPackHelper) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Properties(java.util.Properties) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Test(org.junit.Test) TaskSubscription(io.zeebe.client.task.TaskSubscription) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) Collectors(java.util.stream.Collectors) TasksClientImpl(io.zeebe.client.impl.TasksClientImpl) SubscriptionType(io.zeebe.protocol.clientapi.SubscriptionType) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) EventType(io.zeebe.protocol.clientapi.EventType) ClientProperties(io.zeebe.client.ClientProperties) TaskHandler(io.zeebe.client.task.TaskHandler) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Subscriber(io.zeebe.client.task.impl.subscription.Subscriber) TestUtil(io.zeebe.test.util.TestUtil) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) TaskSubscription(io.zeebe.client.task.TaskSubscription) Test(org.junit.Test)

Example 3 with TestUtil.waitUntil

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);
}
Also used : TasksClient(io.zeebe.client.TasksClient) TaskEvent(io.zeebe.client.event.TaskEvent) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) HashMap(java.util.HashMap) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) ErrorCode(io.zeebe.protocol.clientapi.ErrorCode) RemoteAddress(io.zeebe.transport.RemoteAddress) ClientRule(io.zeebe.client.util.ClientRule) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) ExpectedException(org.junit.rules.ExpectedException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Before(org.junit.Before) MsgPackHelper(io.zeebe.test.broker.protocol.MsgPackHelper) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Properties(java.util.Properties) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Test(org.junit.Test) TaskSubscription(io.zeebe.client.task.TaskSubscription) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) Collectors(java.util.stream.Collectors) TasksClientImpl(io.zeebe.client.impl.TasksClientImpl) SubscriptionType(io.zeebe.protocol.clientapi.SubscriptionType) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) EventType(io.zeebe.protocol.clientapi.EventType) ClientProperties(io.zeebe.client.ClientProperties) TaskHandler(io.zeebe.client.task.TaskHandler) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Subscriber(io.zeebe.client.task.impl.subscription.Subscriber) TestUtil(io.zeebe.test.util.TestUtil) RemoteAddress(io.zeebe.transport.RemoteAddress) Properties(java.util.Properties) ClientProperties(io.zeebe.client.ClientProperties) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) Test(org.junit.Test)

Example 4 with TestUtil.waitUntil

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);
}
Also used : SubscriptionManager(io.zeebe.client.task.impl.subscription.SubscriptionManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) TimeoutException(java.util.concurrent.TimeoutException) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) Future(java.util.concurrent.Future) ErrorCode(io.zeebe.protocol.clientapi.ErrorCode) RemoteAddress(io.zeebe.transport.RemoteAddress) ClientRule(io.zeebe.client.util.ClientRule) TopicSubscriberGroup(io.zeebe.client.event.impl.TopicSubscriberGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) TopicSubscriptionBuilderImpl(io.zeebe.client.event.impl.TopicSubscriptionBuilderImpl) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ResponseController(io.zeebe.test.broker.protocol.brokerapi.ResponseController) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) ActorFuture(io.zeebe.util.sched.future.ActorFuture) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) EventType(io.zeebe.protocol.clientapi.EventType) Conditions(io.zeebe.test.util.Conditions) Optional(java.util.Optional) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) TestUtil(io.zeebe.test.util.TestUtil) RemoteAddress(io.zeebe.transport.RemoteAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 5 with TestUtil.waitUntil

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();
}
Also used : SubscriptionManager(io.zeebe.client.task.impl.subscription.SubscriptionManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) TimeoutException(java.util.concurrent.TimeoutException) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) Future(java.util.concurrent.Future) ErrorCode(io.zeebe.protocol.clientapi.ErrorCode) RemoteAddress(io.zeebe.transport.RemoteAddress) ClientRule(io.zeebe.client.util.ClientRule) TopicSubscriberGroup(io.zeebe.client.event.impl.TopicSubscriberGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) TopicSubscriptionBuilderImpl(io.zeebe.client.event.impl.TopicSubscriptionBuilderImpl) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ResponseController(io.zeebe.test.broker.protocol.brokerapi.ResponseController) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) ActorFuture(io.zeebe.util.sched.future.ActorFuture) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) EventType(io.zeebe.protocol.clientapi.EventType) Conditions(io.zeebe.test.util.Conditions) Optional(java.util.Optional) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) TestUtil(io.zeebe.test.util.TestUtil) RemoteAddress(io.zeebe.transport.RemoteAddress) TopicSubscriberGroup(io.zeebe.client.event.impl.TopicSubscriberGroup) Test(org.junit.Test)

Aggregations

ZeebeClient (io.zeebe.client.ZeebeClient)6 ZeebeClientImpl (io.zeebe.client.impl.ZeebeClientImpl)6 ClientRule (io.zeebe.client.util.ClientRule)6 ControlMessageType (io.zeebe.protocol.clientapi.ControlMessageType)6 ErrorCode (io.zeebe.protocol.clientapi.ErrorCode)6 EventType (io.zeebe.protocol.clientapi.EventType)6 ControlMessageRequest (io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest)6 ExecuteCommandRequest (io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest)6 StubBrokerRule (io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule)6 TestUtil (io.zeebe.test.util.TestUtil)6 TestUtil.waitUntil (io.zeebe.test.util.TestUtil.waitUntil)6 RemoteAddress (io.zeebe.transport.RemoteAddress)6 Duration (java.time.Duration)6 List (java.util.List)6 TimeUnit (java.util.concurrent.TimeUnit)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Collectors (java.util.stream.Collectors)6 Stream (java.util.stream.Stream)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3