Search in sources :

Example 21 with RemoteAddress

use of io.zeebe.transport.RemoteAddress in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldCompleteTaskWithoutPayload.

@Test
public void shouldCompleteTaskWithoutPayload() {
    // given
    broker.stubTaskSubscriptionApi(123L);
    stubTaskCompleteRequest();
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler((c, t) -> c.complete(t).withoutPayload().execute()).lockOwner("foo").lockTime(10000L).taskType("bar").open();
    final RemoteAddress eventSource = getSubscribeRequests().findFirst().get().getSource();
    // when
    broker.pushLockedTask(eventSource, 123L, 4L, 5L, "foo", "bar");
    // then
    final ExecuteCommandRequest taskRequest = TestUtil.doRepeatedly(() -> broker.getReceivedCommandRequests().stream().filter(r -> r.eventType() == EventType.TASK_EVENT).findFirst()).until(r -> r.isPresent()).get();
    assertThat(taskRequest.partitionId()).isEqualTo(clientRule.getDefaultPartitionId());
    assertThat(taskRequest.key()).isEqualTo(4L);
    assertThat(taskRequest.getCommand()).containsEntry("state", "COMPLETE").containsEntry("type", "bar").containsEntry("lockOwner", "foo").doesNotContainKey("payload");
}
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) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) Test(org.junit.Test)

Example 22 with RemoteAddress

use of io.zeebe.transport.RemoteAddress in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldNotAttemptReplenishmentForZeroCredits.

@Test
public void shouldNotAttemptReplenishmentForZeroCredits() throws InterruptedException {
    // given
    final int subscriptionCapacity = 16;
    final int replenishmentThreshold = (int) (Math.ceil(subscriptionCapacity * Subscriber.REPLENISHMENT_THRESHOLD));
    final int tasksToHandleBeforeReplenishment = subscriptionCapacity - replenishmentThreshold;
    broker.stubTaskSubscriptionApi(123L);
    final WaitingTaskHandler handler = new WaitingTaskHandler();
    handler.shouldWait = false;
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(handler).lockOwner("owner").lockTime(10000L).taskFetchSize(subscriptionCapacity).taskType("type").open();
    final RemoteAddress clientAddress = getSubscribeRequests().findFirst().get().getSource();
    // handling these tasks should not yet trigger replenishment; the next handled task would
    for (int i = 0; i < tasksToHandleBeforeReplenishment; i++) {
        broker.pushLockedTask(clientAddress, 123L, 4L + i, 5L + i, "foo", "type");
    }
    waitUntil(() -> handler.numHandledEvents.get() == tasksToHandleBeforeReplenishment);
    handler.shouldWait = true;
    for (int i = 0; i < NUM_EXECUTION_THREADS; i++) {
        broker.pushLockedTask(clientAddress, 123L, 4L + i, 5L + i, "foo", "type");
    }
    waitUntil(() -> handler.numWaitingThreads.get() == NUM_EXECUTION_THREADS);
    // when all task handling threads trigger credit replenishment
    continueTaskHandlingThreads();
    // then
    waitUntil(() -> getCreditRequests().count() >= 1);
    // waiting for potentially more credit requests
    Thread.sleep(500L);
    final List<ControlMessageRequest> creditRequests = getCreditRequests().collect(Collectors.toList());
    assertThat(creditRequests.size()).isGreaterThanOrEqualTo(1);
    int totalReplenishedCredits = 0;
    for (ControlMessageRequest request : creditRequests) {
        final int replenishedCredits = (int) request.getData().get("credits");
        assertThat(replenishedCredits).isGreaterThan(0);
        totalReplenishedCredits += replenishedCredits;
    }
    assertThat(totalReplenishedCredits).isGreaterThanOrEqualTo(tasksToHandleBeforeReplenishment + 1);
}
Also used : ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) RemoteAddress(io.zeebe.transport.RemoteAddress) Test(org.junit.Test)

Example 23 with RemoteAddress

use of io.zeebe.transport.RemoteAddress in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldInvokeTaskHandlerWithTwoSubscriptions.

@Test
public void shouldInvokeTaskHandlerWithTwoSubscriptions() {
    // given
    broker.stubTaskSubscriptionApi(123L);
    stubTaskCompleteRequest();
    final RecordingTaskHandler handler1 = new RecordingTaskHandler();
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(handler1).lockOwner("foo").lockTime(10000L).taskType("type1").open();
    final RecordingTaskHandler handler2 = new RecordingTaskHandler();
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(handler2).lockOwner("bar").lockTime(10000L).taskType("type2").open();
    final RemoteAddress clientAddress = getSubscribeRequests().findFirst().get().getSource();
    // when
    broker.pushLockedTask(clientAddress, 123L, 4L, 5L, "foo", "type1");
    broker.pushLockedTask(clientAddress, 124L, 5L, 6L, "bar", "type2");
    // then
    TestUtil.waitUntil(() -> !handler1.getHandledTasks().isEmpty());
    TestUtil.waitUntil(() -> !handler2.getHandledTasks().isEmpty());
    assertThat(handler1.getHandledTasks()).hasSize(1);
    assertThat(handler2.getHandledTasks()).hasSize(1);
    final TaskEvent task1 = handler1.getHandledTasks().get(0);
    assertThat(task1.getMetadata().getKey()).isEqualTo(4L);
    assertThat(task1.getType()).isEqualTo("type1");
    final TaskEvent task2 = handler2.getHandledTasks().get(0);
    assertThat(task2.getMetadata().getKey()).isEqualTo(5L);
    assertThat(task2.getType()).isEqualTo("type2");
}
Also used : RemoteAddress(io.zeebe.transport.RemoteAddress) TaskEvent(io.zeebe.client.event.TaskEvent) Test(org.junit.Test)

Example 24 with RemoteAddress

use of io.zeebe.transport.RemoteAddress in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldCompleteTaskWithPayload.

@Test
public void shouldCompleteTaskWithPayload() {
    // given
    broker.stubTaskSubscriptionApi(123L);
    stubTaskCompleteRequest();
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler((c, t) -> c.complete(t).payload("{\"a\": 1}").execute()).lockOwner("foo").lockTime(10000L).taskType("bar").open();
    final RemoteAddress eventSource = getSubscribeRequests().findFirst().get().getSource();
    // when
    broker.pushLockedTask(eventSource, 123L, 4L, 5L, "foo", "bar");
    // then
    final ExecuteCommandRequest taskRequest = TestUtil.doRepeatedly(() -> broker.getReceivedCommandRequests().stream().filter(r -> r.eventType() == EventType.TASK_EVENT).findFirst()).until(r -> r.isPresent()).get();
    assertThat(taskRequest.partitionId()).isEqualTo(clientRule.getDefaultPartitionId());
    assertThat(taskRequest.key()).isEqualTo(4L);
    assertThat(taskRequest.getCommand()).containsEntry("state", "COMPLETE").containsEntry("type", "bar").containsEntry("lockOwner", "foo").containsEntry("payload", msgPackConverter.convertToMsgPack("{\"a\": 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) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) Test(org.junit.Test)

Example 25 with RemoteAddress

use of io.zeebe.transport.RemoteAddress in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldNotAutocompleteTask.

@Test
public void shouldNotAutocompleteTask() throws InterruptedException {
    // given
    broker.stubTaskSubscriptionApi(123L);
    stubTaskCompleteRequest();
    final RecordingTaskHandler handler = new RecordingTaskHandler();
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(handler).lockOwner("foo").lockTime(10000L).taskType("bar").open();
    final RemoteAddress clientAddress = getSubscribeRequests().findFirst().get().getSource();
    // when
    broker.pushLockedTask(clientAddress, 123L, 4L, 5L, "foo", "bar");
    // then
    Thread.sleep(1000L);
    assertThat(broker.getReceivedCommandRequests().stream().filter(r -> r.eventType() == EventType.TASK_EVENT).count()).isEqualTo(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) RemoteAddress(io.zeebe.transport.RemoteAddress) Test(org.junit.Test)

Aggregations

RemoteAddress (io.zeebe.transport.RemoteAddress)35 Test (org.junit.Test)34 ZeebeClientImpl (io.zeebe.client.impl.ZeebeClientImpl)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 ControlMessageRequest (io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest)12 ExecuteCommandRequest (io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest)12 ZeebeClient (io.zeebe.client.ZeebeClient)11 ClientRule (io.zeebe.client.util.ClientRule)11 ControlMessageType (io.zeebe.protocol.clientapi.ControlMessageType)11 ErrorCode (io.zeebe.protocol.clientapi.ErrorCode)11 EventType (io.zeebe.protocol.clientapi.EventType)11 StubBrokerRule (io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule)11 TestUtil (io.zeebe.test.util.TestUtil)11 TestUtil.waitUntil (io.zeebe.test.util.TestUtil.waitUntil)11 Duration (java.time.Duration)11 List (java.util.List)11 TimeUnit (java.util.concurrent.TimeUnit)11 Collectors (java.util.stream.Collectors)11 Stream (java.util.stream.Stream)11 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)11