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")));
}
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());
}
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")));
}
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);
}
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);
}
Aggregations