Search in sources :

Example 6 with TaskEvent

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

the class TaskSubscriptionTest method shouldPollTasks.

@Test
public void shouldPollTasks() {
    // given
    eventRecorder.startRecordingEvents();
    final PollableTaskSubscription subscription = clientRule.tasks().newPollableTaskSubscription(clientRule.getDefaultTopic()).taskType("foo").lockTime(Duration.ofMinutes(5)).lockOwner("test").open();
    final TaskEvent task = clientRule.tasks().create(clientRule.getDefaultTopic(), "foo").payload("{\"a\":1}").addCustomHeader("b", "2").execute();
    // when
    final RecordingTaskHandler taskHandler = new RecordingTaskHandler((c, t) -> c.complete(t).withoutPayload().execute());
    doRepeatedly(() -> subscription.poll(taskHandler)).until((workCount) -> workCount == 1);
    assertThat(taskHandler.getHandledTasks()).hasSize(1);
    final TaskEvent subscribedTasks = taskHandler.getHandledTasks().get(0);
    assertThat(subscribedTasks.getMetadata().getKey()).isEqualTo(task.getMetadata().getKey());
    assertThat(subscribedTasks.getType()).isEqualTo("foo");
    assertThat(subscribedTasks.getLockExpirationTime()).isAfter(Instant.now());
    assertThat(subscribedTasks.getPayload()).isEqualTo("{\"a\":1}");
    assertThat(subscribedTasks.getCustomHeaders()).containsEntry("b", "2");
    waitUntil(() -> eventRecorder.hasTaskEvent(taskEvent("COMPLETED")));
}
Also used : RecordingTaskHandler(io.zeebe.broker.it.util.RecordingTaskHandler) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) TaskEvent(io.zeebe.client.event.TaskEvent) Test(org.junit.Test)

Example 7 with TaskEvent

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

the class TaskSubscriptionTest method shouldOpenSubscription.

@Test
public void shouldOpenSubscription() throws InterruptedException {
    // given
    final TaskEvent task = clientRule.tasks().create(clientRule.getDefaultTopic(), "foo").execute();
    // when
    final RecordingTaskHandler taskHandler = new RecordingTaskHandler();
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopic()).handler(taskHandler).taskType("foo").lockTime(Duration.ofMinutes(5)).lockOwner("test").open();
    // then
    waitUntil(() -> !taskHandler.getHandledTasks().isEmpty());
    final List<TaskEvent> tasks = taskHandler.getHandledTasks();
    assertThat(tasks).hasSize(1);
    assertThat(tasks.get(0).getMetadata().getKey()).isEqualTo(task.getMetadata().getKey());
}
Also used : RecordingTaskHandler(io.zeebe.broker.it.util.RecordingTaskHandler) TaskEvent(io.zeebe.client.event.TaskEvent) Test(org.junit.Test)

Example 8 with TaskEvent

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

the class TaskSubscriptionTest method shouldMarkTaskAsFailedAndRetryIfHandlerThrowsException.

@Test
public void shouldMarkTaskAsFailedAndRetryIfHandlerThrowsException() {
    // given
    eventRecorder.startRecordingEvents();
    final TaskEvent task = clientRule.tasks().create(clientRule.getDefaultTopic(), "foo").execute();
    final RecordingTaskHandler taskHandler = new RecordingTaskHandler((c, t) -> {
        throw new RuntimeException("expected failure");
    }, (c, t) -> c.complete(t).withoutPayload().execute());
    // when
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopic()).handler(taskHandler).taskType("foo").lockTime(Duration.ofMinutes(5)).lockOwner("test").open();
    // then the subscription is not broken and other tasks are still handled
    waitUntil(() -> taskHandler.getHandledTasks().size() == 2);
    final long taskKey = task.getMetadata().getKey();
    assertThat(taskHandler.getHandledTasks()).extracting("metadata.key").containsExactly(taskKey, taskKey);
    assertThat(eventRecorder.hasTaskEvent(taskEvent("FAILED"))).isTrue();
    waitUntil(() -> eventRecorder.hasTaskEvent(taskEvent("COMPLETED")));
}
Also used : RecordingTaskHandler(io.zeebe.broker.it.util.RecordingTaskHandler) TaskEvent(io.zeebe.client.event.TaskEvent) Test(org.junit.Test)

Example 9 with TaskEvent

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

the class CompleteTaskTest method shouldCompleteTask.

@Test
public void shouldCompleteTask() {
    // given
    final TaskEventImpl baseEvent = Events.exampleTask();
    brokerRule.onExecuteCommandRequest(EventType.TASK_EVENT, "COMPLETE").respondWith().key(123).event().allOf((r) -> r.getCommand()).put("state", "COMPLETED").done().register();
    final String updatedPayload = "{\"fruit\":\"cherry\"}";
    // when
    final TaskEvent taskEvent = clientRule.tasks().complete(baseEvent).payload(updatedPayload).execute();
    // then
    final ExecuteCommandRequest request = brokerRule.getReceivedCommandRequests().get(0);
    assertThat(request.eventType()).isEqualTo(EventType.TASK_EVENT);
    assertThat(request.partitionId()).isEqualTo(StubBrokerRule.TEST_PARTITION_ID);
    assertThat(request.position()).isEqualTo(baseEvent.getMetadata().getPosition());
    assertThat(request.getCommand()).containsOnly(entry("state", "COMPLETE"), entry("lockTime", baseEvent.getLockExpirationTime().toEpochMilli()), entry("lockOwner", baseEvent.getLockOwner()), entry("retries", baseEvent.getRetries()), entry("type", baseEvent.getType()), entry("headers", baseEvent.getHeaders()), entry("customHeaders", baseEvent.getCustomHeaders()), entry("payload", converter.convertToMsgPack(updatedPayload)));
    assertThat(taskEvent.getMetadata().getKey()).isEqualTo(123L);
    assertThat(taskEvent.getMetadata().getTopicName()).isEqualTo(StubBrokerRule.TEST_TOPIC_NAME);
    assertThat(taskEvent.getMetadata().getPartitionId()).isEqualTo(StubBrokerRule.TEST_PARTITION_ID);
    assertThat(taskEvent.getState()).isEqualTo("COMPLETED");
    assertThat(taskEvent.getHeaders()).isEqualTo(baseEvent.getHeaders());
    assertThat(taskEvent.getLockExpirationTime()).isEqualTo(baseEvent.getLockExpirationTime());
    assertThat(taskEvent.getLockOwner()).isEqualTo(baseEvent.getLockOwner());
    assertThat(taskEvent.getRetries()).isEqualTo(baseEvent.getRetries());
    assertThat(taskEvent.getType()).isEqualTo(baseEvent.getType());
    assertThat(taskEvent.getPayload()).isEqualTo(updatedPayload);
}
Also used : TasksClient(io.zeebe.client.TasksClient) TaskEvent(io.zeebe.client.event.TaskEvent) Events(io.zeebe.client.util.Events) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Assertions.entry(org.assertj.core.api.Assertions.entry) StandardCharsets(java.nio.charset.StandardCharsets) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) RuleChain(org.junit.rules.RuleChain) Rule(org.junit.Rule) ClientRule(io.zeebe.client.util.ClientRule) ByteArrayInputStream(java.io.ByteArrayInputStream) EventType(io.zeebe.protocol.clientapi.EventType) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) ClientCommandRejectedException(io.zeebe.client.cmd.ClientCommandRejectedException) ExpectedException(org.junit.rules.ExpectedException) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Before(org.junit.Before) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) TaskEvent(io.zeebe.client.event.TaskEvent) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) Test(org.junit.Test)

Example 10 with TaskEvent

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

the class FailTaskTest method shouldFailTask.

@Test
public void shouldFailTask() {
    // given
    final TaskEventImpl baseEvent = Events.exampleTask();
    brokerRule.onExecuteCommandRequest(EventType.TASK_EVENT, "FAIL").respondWith().key(123).event().allOf((r) -> r.getCommand()).put("state", "FAILED").done().register();
    // when
    final TaskEvent taskEvent = clientRule.tasks().fail(baseEvent).retries(4).execute();
    // then
    final ExecuteCommandRequest request = brokerRule.getReceivedCommandRequests().get(0);
    assertThat(request.eventType()).isEqualTo(EventType.TASK_EVENT);
    assertThat(request.partitionId()).isEqualTo(StubBrokerRule.TEST_PARTITION_ID);
    assertThat(request.getCommand()).containsOnly(entry("state", "FAIL"), entry("lockTime", baseEvent.getLockExpirationTime().toEpochMilli()), entry("lockOwner", baseEvent.getLockOwner()), entry("retries", 4), entry("type", baseEvent.getType()), entry("headers", baseEvent.getHeaders()), entry("customHeaders", baseEvent.getCustomHeaders()), entry("payload", baseEvent.getPayloadMsgPack()));
    assertThat(taskEvent.getMetadata().getKey()).isEqualTo(123L);
    assertThat(taskEvent.getMetadata().getTopicName()).isEqualTo(StubBrokerRule.TEST_TOPIC_NAME);
    assertThat(taskEvent.getMetadata().getPartitionId()).isEqualTo(StubBrokerRule.TEST_PARTITION_ID);
    assertThat(taskEvent.getState()).isEqualTo("FAILED");
    assertThat(taskEvent.getHeaders()).isEqualTo(baseEvent.getHeaders());
    assertThat(taskEvent.getLockExpirationTime()).isEqualTo(baseEvent.getLockExpirationTime());
    assertThat(taskEvent.getLockOwner()).isEqualTo(baseEvent.getLockOwner());
    assertThat(taskEvent.getType()).isEqualTo(baseEvent.getType());
    assertThat(taskEvent.getPayload()).isEqualTo(baseEvent.getPayload());
    assertThat(taskEvent.getRetries()).isEqualTo(4);
}
Also used : TasksClient(io.zeebe.client.TasksClient) TaskEvent(io.zeebe.client.event.TaskEvent) Events(io.zeebe.client.util.Events) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Assertions.entry(org.assertj.core.api.Assertions.entry) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) RuleChain(org.junit.rules.RuleChain) Rule(org.junit.Rule) ClientRule(io.zeebe.client.util.ClientRule) EventType(io.zeebe.protocol.clientapi.EventType) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) ClientCommandRejectedException(io.zeebe.client.cmd.ClientCommandRejectedException) ExpectedException(org.junit.rules.ExpectedException) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) TaskEvent(io.zeebe.client.event.TaskEvent) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) 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