use of io.zeebe.test.broker.protocol.clientapi.SubscribedEvent in project zeebe by zeebe-io.
the class CompleteTaskTest method shouldRejectCompletionIfNotLockOwner.
@Test
public void shouldRejectCompletionIfNotLockOwner() {
// given
final String lockOwner = "kermit";
createTask(TASK_TYPE);
apiRule.createControlMessageRequest().partitionId(apiRule.getDefaultPartitionId()).messageType(ControlMessageType.ADD_TASK_SUBSCRIPTION).data().put("taskType", TASK_TYPE).put("lockDuration", Duration.ofSeconds(30).toMillis()).put("lockOwner", lockOwner).put("credits", 10).done().sendAndAwait();
final SubscribedEvent subscribedEvent = receiveSingleSubscribedEvent();
final Map<String, Object> event = subscribedEvent.event();
event.put("lockOwner", "ms piggy");
// when
final ExecuteCommandResponse response = completeTask(subscribedEvent.key(), event);
// then
assertThat(response.getEvent()).containsEntry("state", "COMPLETE_REJECTED");
}
use of io.zeebe.test.broker.protocol.clientapi.SubscribedEvent in project zeebe by zeebe-io.
the class TaskSubscriptionTest method shouldNoLongerLockTasksAfterRemoval.
@Test
public void shouldNoLongerLockTasksAfterRemoval() throws InterruptedException {
// given
final String taskType = "foo";
final ControlMessageResponse openResponse = apiRule.openTaskSubscription(taskType).await();
final int subscriberKey = (int) openResponse.getData().get("subscriberKey");
apiRule.closeTaskSubscription(subscriberKey).await();
// when
testClient.createTask(taskType);
// then
apiRule.openTopicSubscription("test", 0).await();
Thread.sleep(500L);
final int eventsAvailable = apiRule.numSubscribedEventsAvailable();
final List<SubscribedEvent> receivedEvents = apiRule.subscribedEvents().limit(eventsAvailable).collect(Collectors.toList());
assertThat(receivedEvents).hasSize(2);
assertThat(receivedEvents).allMatch(e -> e.subscriptionType() == SubscriptionType.TOPIC_SUBSCRIPTION);
assertThat(receivedEvents).extracting("event").extracting("state").containsExactly("CREATE", // no more LOCK etc.
"CREATED");
}
use of io.zeebe.test.broker.protocol.clientapi.SubscribedEvent in project zeebe by zeebe-io.
the class TaskSubscriptionTest method shouldCloseSubscriptionOnTransportChannelClose.
@Test
public void shouldCloseSubscriptionOnTransportChannelClose() throws InterruptedException {
// given
apiRule.openTaskSubscription("foo").await();
// when the transport channel is closed
apiRule.interruptAllChannels();
// then the subscription has been closed, so we can create a new task and lock it for a new subscription
// closing subscriptions happens asynchronously
Thread.sleep(1000L);
final ExecuteCommandResponse response = testClient.createTask("foo");
final ControlMessageResponse subscriptionResponse = apiRule.openTaskSubscription("foo").await();
final int secondSubscriberKey = (int) subscriptionResponse.getData().get("subscriberKey");
final Optional<SubscribedEvent> taskEvent = apiRule.subscribedEvents().filter((s) -> s.subscriptionType() == SubscriptionType.TASK_SUBSCRIPTION && s.key() == response.key()).findFirst();
assertThat(taskEvent).isPresent();
assertThat(taskEvent.get().subscriberKey()).isEqualTo(secondSubscriberKey);
}
use of io.zeebe.test.broker.protocol.clientapi.SubscribedEvent in project zeebe by zeebe-io.
the class TaskSubscriptionTest method shouldAddTaskSubscription.
@Test
public void shouldAddTaskSubscription() throws InterruptedException {
// given
apiRule.createControlMessageRequest().messageType(ControlMessageType.ADD_TASK_SUBSCRIPTION).partitionId(apiRule.getDefaultPartitionId()).data().put("taskType", "foo").put("lockDuration", 10000L).put("lockOwner", "bar").put("credits", 5).done().send();
// when
final ExecuteCommandResponse response = testClient.createTask("foo");
// then
final SubscribedEvent taskEvent = testClient.receiveSingleEvent(taskEvents("LOCKED"));
assertThat(taskEvent.key()).isEqualTo(response.key());
assertThat(taskEvent.position()).isGreaterThan(response.position());
assertThat(taskEvent.event()).containsEntry("type", "foo").containsEntry("retries", 3).containsEntry("lockOwner", "bar");
final List<Object> taskStates = testClient.receiveEvents(taskEvents()).limit(4).map(e -> e.event().get("state")).collect(Collectors.toList());
assertThat(taskStates).containsExactly("CREATE", "CREATED", "LOCK", "LOCKED");
}
use of io.zeebe.test.broker.protocol.clientapi.SubscribedEvent in project zeebe by zeebe-io.
the class TaskSubscriptionTest method shouldPublishTasksToSecondSubscription.
@Test
public void shouldPublishTasksToSecondSubscription() {
// given
final int credits = 2;
final String taskType = "foo";
apiRule.openTaskSubscription(apiRule.getDefaultPartitionId(), taskType, 10000, credits).await();
for (int i = 0; i < credits; i++) {
testClient.createTask(taskType);
}
waitUntil(() -> apiRule.numSubscribedEventsAvailable() == credits);
apiRule.moveMessageStreamToTail();
final int secondSubscriber = (int) apiRule.openTaskSubscription(apiRule.getDefaultPartitionId(), taskType, 10000, credits).await().getData().get("subscriberKey");
// when
testClient.createTask(taskType);
// then
waitUntil(() -> apiRule.numSubscribedEventsAvailable() == 1);
final SubscribedEvent subscribedEvent = apiRule.subscribedEvents().findFirst().get();
assertThat(subscribedEvent.subscriberKey()).isEqualTo(secondSubscriber);
}
Aggregations