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