use of io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest in project zeebe by zeebe-io.
the class TaskSubscriptionTest method shouldOpenPollableSubscriptionWithLockTimeAsDuration.
@Test
public void shouldOpenPollableSubscriptionWithLockTimeAsDuration() {
// given
broker.stubTaskSubscriptionApi(123L);
// when
clientRule.tasks().newPollableTaskSubscription(clientRule.getDefaultTopicName()).lockOwner("foo").lockTime(Duration.ofDays(10)).taskType("bar").open();
// then
final ControlMessageRequest subscriptionRequest = getSubscribeRequests().findFirst().get();
assertThat(subscriptionRequest.getData()).contains(entry("lockDuration", (int) TimeUnit.DAYS.toMillis(10L)));
}
use of io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest in project zeebe by zeebe-io.
the class RequestTopicsTest method shouldRequestTopics.
@Test
public void shouldRequestTopics() {
// given
final List<Map<String, Object>> partitions = new ArrayList<>();
partitions.add(buildPartition(1, "foo"));
partitions.add(buildPartition(2, "foo"));
partitions.add(buildPartition(3, "bar"));
brokerRule.onControlMessageRequest(r -> r.messageType() == ControlMessageType.REQUEST_PARTITIONS && r.partitionId() == Protocol.SYSTEM_PARTITION).respondWith().data().put("partitions", partitions).done().register();
final ZeebeClient client = clientRule.getClient();
// when
final Topics result = client.topics().getTopics().execute();
// then
final List<Topic> returnedTopics = result.getTopics();
assertThat(returnedTopics).hasSize(2);
final Map<String, List<Partition>> topicsByName = returnedTopics.stream().collect(Collectors.toMap(Topic::getName, Topic::getPartitions));
assertThat(topicsByName.get("foo")).hasSize(2).areExactly(1, matching(1, "foo")).areExactly(1, matching(2, "foo"));
assertThat(topicsByName.get("bar")).hasSize(1).areExactly(1, matching(3, "bar"));
final List<ControlMessageRequest> partitionRequests = brokerRule.getReceivedControlMessageRequests().stream().filter(r -> r.messageType() == ControlMessageType.REQUEST_PARTITIONS).collect(Collectors.toList());
assertThat(partitionRequests).hasSize(1);
final ControlMessageRequest request = partitionRequests.get(0);
assertThat(request.partitionId()).isEqualTo(Protocol.SYSTEM_PARTITION);
}
use of io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest in project zeebe by zeebe-io.
the class TopicSubscriptionTest method shouldCloseSubscriptionWhileOpeningSubscriber.
@Test
public void shouldCloseSubscriptionWhileOpeningSubscriber() {
// given
final int subscriberKey = 123;
broker.stubTopicSubscriptionApi(0L);
final ResponseController responseController = broker.onExecuteCommandRequest(EventType.SUBSCRIBER_EVENT, "SUBSCRIBE").respondWith().key(subscriberKey).event().allOf((r) -> r.getCommand()).put("state", "SUBSCRIBED").done().registerControlled();
final TopicSubscriptionBuilderImpl builder = (TopicSubscriptionBuilderImpl) client.topics().newSubscription(clientRule.getDefaultTopicName()).handler(DO_NOTHING).name("foo");
final Future<TopicSubscriberGroup> future = builder.buildSubscriberGroup();
waitUntil(() -> broker.getReceivedCommandRequests().stream().filter(r -> r.eventType() == EventType.SUBSCRIBER_EVENT && "SUBSCRIBE".equals(r.getCommand().get("state"))).count() == 1);
final Thread closingThread = new Thread(client::close);
closingThread.start();
final SubscriptionManager subscriptionManager = ((ZeebeClientImpl) client).getSubscriptionManager();
waitUntil(() -> subscriptionManager.isClosing());
// when
responseController.unblockNextResponse();
// then
waitUntil(() -> future.isDone());
assertThat(future).isDone();
final Optional<ControlMessageRequest> closeRequest = broker.getReceivedControlMessageRequests().stream().filter(c -> c.messageType() == ControlMessageType.REMOVE_TOPIC_SUBSCRIPTION).findFirst();
assertThat(closeRequest).isPresent();
final ControlMessageRequest request = closeRequest.get();
assertThat(request.getData()).containsEntry("subscriberKey", subscriberKey);
}
use of io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest in project zeebe by zeebe-io.
the class TopicSubscriptionTest method shouldResumeSubscriptionBeforeFailedEventAfterHandlerFailure.
@Test
public void shouldResumeSubscriptionBeforeFailedEventAfterHandlerFailure() {
// given
broker.stubTopicSubscriptionApi(123L);
final FailingHandler handler = new FailingHandler(e -> e.getMetadata().getPosition() == 2L);
final TopicSubscription subscription = clientRule.topics().newSubscription(clientRule.getDefaultTopicName()).startAtHeadOfTopic().handler(handler).name(SUBSCRIPTION_NAME).open();
final RemoteAddress clientAddress = broker.getReceivedCommandRequests().get(0).getSource();
broker.pushTopicEvent(clientAddress, 123L, 1L, 1L);
// when
broker.pushTopicEvent(clientAddress, 123L, 1L, 2L);
// then
TestUtil.waitUntil(() -> subscription.isClosed());
final List<ExecuteCommandRequest> commandRequests = broker.getReceivedCommandRequests();
final List<ExecuteCommandRequest> acknowledgements = commandRequests.stream().filter((c) -> c.eventType() == EventType.SUBSCRIPTION_EVENT).filter((c) -> "ACKNOWLEDGE".equals(c.getCommand().get("state"))).collect(Collectors.toList());
assertThat(acknowledgements).isNotEmpty();
final ExecuteCommandRequest lastAck = acknowledgements.get(acknowledgements.size() - 1);
assertThat(lastAck.getCommand().get("name")).isEqualTo(SUBSCRIPTION_NAME);
assertThat(lastAck.getCommand().get("ackPosition")).isEqualTo(1);
final ControlMessageRequest removeRequest = broker.getReceivedControlMessageRequests().stream().filter((c) -> c.messageType() == ControlMessageType.REMOVE_TOPIC_SUBSCRIPTION).findFirst().get();
final List<Object> requests = broker.getAllReceivedRequests();
assertThat(requests).contains(lastAck);
assertThat(requests.indexOf(lastAck)).isLessThan(requests.indexOf(removeRequest));
}
use of io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest in project zeebe by zeebe-io.
the class PartitionedTaskSubscriptionTest method shouldSubscribeToMultiplePartitionsOfATopic.
@Test
public void shouldSubscribeToMultiplePartitionsOfATopic() {
// given
broker1.stubTaskSubscriptionApi(456);
broker2.stubTaskSubscriptionApi(789);
// when
final TaskSubscription subscription = client.tasks().newTaskSubscription(TOPIC).handler(new RecordingTaskHandler()).taskType(TASK_TYPE).lockOwner("bumbum").lockTime(Duration.ofSeconds(6)).open();
// then
assertThat(subscription.isOpen()).isTrue();
final List<ControlMessageRequest> subscribeRequestsBroker1 = getSubscribeRequests(broker1);
assertThat(subscribeRequestsBroker1).hasSize(1);
final ControlMessageRequest request1 = subscribeRequestsBroker1.get(0);
assertThat(request1.partitionId()).isEqualTo(PARTITION_1);
final List<ControlMessageRequest> subscribeRequestsBroker2 = getSubscribeRequests(broker2);
assertThat(subscribeRequestsBroker2).hasSize(1);
final ControlMessageRequest request2 = subscribeRequestsBroker2.get(0);
assertThat(request2.partitionId()).isEqualTo(PARTITION_2);
}
Aggregations