Search in sources :

Example 36 with TaskEvent

use of io.zeebe.client.event.TaskEvent in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldExpireTaskLock.

@Test
public void shouldExpireTaskLock() {
    // given
    eventRecorder.startRecordingEvents();
    final TaskEvent task = clientRule.tasks().create(clientRule.getDefaultTopic(), "foo").execute();
    final RecordingTaskHandler taskHandler = new RecordingTaskHandler((c, t) -> {
    // don't complete the task - just wait for lock expiration
    });
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopic()).handler(taskHandler).taskType("foo").lockTime(Duration.ofMinutes(5)).lockOwner("test").open();
    waitUntil(() -> taskHandler.getHandledTasks().size() == 1);
    // then
    doRepeatedly(() -> brokerRule.getClock().addTime(Duration.ofMinutes(5))).until((v) -> taskHandler.getHandledTasks().size() == 2);
    final long taskKey = task.getMetadata().getKey();
    assertThat(taskHandler.getHandledTasks()).hasSize(2).extracting("metadata.key").containsExactly(taskKey, taskKey);
    assertThat(eventRecorder.hasTaskEvent(taskEvent("LOCK_EXPIRED"))).isTrue();
}
Also used : RecordingTaskHandler(io.zeebe.broker.it.util.RecordingTaskHandler) TaskEvent(io.zeebe.client.event.TaskEvent) Test(org.junit.Test)

Example 37 with TaskEvent

use of io.zeebe.client.event.TaskEvent in project zeebe by zeebe-io.

the class YamlWorkflowTest method shouldGetTaskWithHeaders.

@Test
public void shouldGetTaskWithHeaders() {
    // given
    workflowClient.deploy(clientRule.getDefaultTopic()).addResourceFromClasspath("workflows/workflow-with-headers.yaml").execute();
    workflowClient.create(clientRule.getDefaultTopic()).bpmnProcessId("workflow-headers").execute();
    // when
    final RecordingTaskHandler recordingTaskHandler = new RecordingTaskHandler();
    taskClient.newTaskSubscription(clientRule.getDefaultTopic()).taskType("foo").lockOwner("owner").lockTime(Duration.ofMinutes(5)).handler(recordingTaskHandler).open();
    // then
    waitUntil(() -> recordingTaskHandler.getHandledTasks().size() >= 1);
    final TaskEvent taskLockedEvent = recordingTaskHandler.getHandledTasks().get(0);
    assertThat(taskLockedEvent.getCustomHeaders()).containsEntry("foo", "f").containsEntry("bar", "b");
}
Also used : RecordingTaskHandler(io.zeebe.broker.it.util.RecordingTaskHandler) TaskEvent(io.zeebe.client.event.TaskEvent)

Example 38 with TaskEvent

use of io.zeebe.client.event.TaskEvent in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldInvokeTaskHandler.

@Test
public void shouldInvokeTaskHandler() throws JsonParseException, JsonMappingException, IOException {
    // given
    broker.stubTaskSubscriptionApi(123L);
    stubTaskCompleteRequest();
    final RecordingTaskHandler handler = new RecordingTaskHandler();
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(handler).lockOwner("owner").lockTime(10000L).taskType("type").open();
    final RemoteAddress clientAddress = getSubscribeRequests().findFirst().get().getSource();
    final MsgPackHelper msgPackHelper = new MsgPackHelper();
    final Map<String, Object> taskPayload = new HashMap<>();
    taskPayload.put("payloadKey", "payloadValue");
    final Map<String, Object> taskHeaders = new HashMap<>();
    taskPayload.put("headerKey", "headerValue");
    final long lockTime = System.currentTimeMillis();
    // when
    broker.newSubscribedEvent().partitionId(StubBrokerRule.TEST_PARTITION_ID).key(4L).position(5L).eventType(EventType.TASK_EVENT).subscriberKey(123L).subscriptionType(SubscriptionType.TASK_SUBSCRIPTION).event().put("type", "type").put("lockTime", lockTime).put("retries", 3).put("payload", msgPackHelper.encodeAsMsgPack(taskPayload)).put("headers", taskHeaders).done().push(clientAddress);
    // then
    TestUtil.waitUntil(() -> !handler.getHandledTasks().isEmpty());
    assertThat(handler.getHandledTasks()).hasSize(1);
    final TaskEvent task = handler.getHandledTasks().get(0);
    assertThat(task.getMetadata().getKey()).isEqualTo(4L);
    assertThat(task.getType()).isEqualTo("type");
    assertThat(task.getHeaders()).isEqualTo(taskHeaders);
    assertThat(task.getLockExpirationTime()).isEqualTo(Instant.ofEpochMilli(lockTime));
    final ObjectMapper objectMapper = new ObjectMapper();
    @SuppressWarnings("unchecked") final Map<String, Object> receivedPayload = objectMapper.readValue(task.getPayload(), Map.class);
    assertThat(receivedPayload).isEqualTo(taskPayload);
}
Also used : RemoteAddress(io.zeebe.transport.RemoteAddress) MsgPackHelper(io.zeebe.test.broker.protocol.MsgPackHelper) HashMap(java.util.HashMap) TaskEvent(io.zeebe.client.event.TaskEvent) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 39 with TaskEvent

use of io.zeebe.client.event.TaskEvent 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 40 with TaskEvent

use of io.zeebe.client.event.TaskEvent in project zeebe by zeebe-io.

the class ZeebeClientTest method shouldReleaseRequestsOnTimeout.

@Test
public void shouldReleaseRequestsOnTimeout() {
    // given
    final TaskEventImpl baseEvent = Events.exampleTask();
    broker.onExecuteCommandRequest(EventType.TASK_EVENT, "COMPLETE").doNotRespond();
    // given
    final List<Future<TaskEvent>> futures = new ArrayList<>();
    for (int i = 0; i < clientMaxRequests; i++) {
        futures.add(client.tasks().complete(baseEvent).executeAsync());
    }
    // when
    for (Future<TaskEvent> future : futures) {
        try {
            future.get();
            fail("exception expected");
        } catch (Exception e) {
        // expected
        }
    }
    // then
    for (int i = 0; i < clientMaxRequests; i++) {
        futures.add(client.tasks().complete(baseEvent).executeAsync());
    }
}
Also used : TaskEvent(io.zeebe.client.event.TaskEvent) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) ClientCommandRejectedException(io.zeebe.client.cmd.ClientCommandRejectedException) ExpectedException(org.junit.rules.ExpectedException) ClientException(io.zeebe.client.cmd.ClientException) Test(org.junit.Test)

Aggregations

TaskEvent (io.zeebe.client.event.TaskEvent)47 Test (org.junit.Test)43 RecordingTaskHandler (io.zeebe.broker.it.util.RecordingTaskHandler)20 ExpectedException (org.junit.rules.ExpectedException)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 Rule (org.junit.Rule)9 ZeebeClient (io.zeebe.client.ZeebeClient)7 ClientCommandRejectedException (io.zeebe.client.cmd.ClientCommandRejectedException)7 TaskEventImpl (io.zeebe.client.event.impl.TaskEventImpl)7 EventType (io.zeebe.protocol.clientapi.EventType)7 StubBrokerRule (io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule)7 RuleChain (org.junit.rules.RuleChain)7 TopologyBroker (io.zeebe.client.clustering.impl.TopologyBroker)6 ClientRule (io.zeebe.client.util.ClientRule)5 Events (io.zeebe.client.util.Events)5 ExecuteCommandRequest (io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest)5 SocketAddress (io.zeebe.transport.SocketAddress)5 StandardCharsets (java.nio.charset.StandardCharsets)5 Assertions.entry (org.assertj.core.api.Assertions.entry)5 WorkflowInstanceEvent (io.zeebe.client.event.WorkflowInstanceEvent)4