Search in sources :

Example 11 with ControlMessageRequest

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)));
}
Also used : ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) Test(org.junit.Test)

Example 12 with ControlMessageRequest

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);
}
Also used : Predicate(java.util.function.Predicate) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) HashMap(java.util.HashMap) Test(org.junit.Test) Protocol(io.zeebe.protocol.Protocol) Collectors(java.util.stream.Collectors) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ArrayList(java.util.ArrayList) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) RuleChain(org.junit.rules.RuleChain) List(java.util.List) Rule(org.junit.Rule) ClientRule(io.zeebe.client.util.ClientRule) Map(java.util.Map) Condition(org.assertj.core.api.Condition) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ArrayList(java.util.ArrayList) ZeebeClient(io.zeebe.client.ZeebeClient) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 13 with ControlMessageRequest

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);
}
Also used : SubscriptionManager(io.zeebe.client.task.impl.subscription.SubscriptionManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) TimeoutException(java.util.concurrent.TimeoutException) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) Future(java.util.concurrent.Future) ErrorCode(io.zeebe.protocol.clientapi.ErrorCode) RemoteAddress(io.zeebe.transport.RemoteAddress) ClientRule(io.zeebe.client.util.ClientRule) TopicSubscriberGroup(io.zeebe.client.event.impl.TopicSubscriberGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) TopicSubscriptionBuilderImpl(io.zeebe.client.event.impl.TopicSubscriptionBuilderImpl) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ResponseController(io.zeebe.test.broker.protocol.brokerapi.ResponseController) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) ActorFuture(io.zeebe.util.sched.future.ActorFuture) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) EventType(io.zeebe.protocol.clientapi.EventType) Conditions(io.zeebe.test.util.Conditions) Optional(java.util.Optional) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) TestUtil(io.zeebe.test.util.TestUtil) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) TopicSubscriberGroup(io.zeebe.client.event.impl.TopicSubscriberGroup) TopicSubscriptionBuilderImpl(io.zeebe.client.event.impl.TopicSubscriptionBuilderImpl) SubscriptionManager(io.zeebe.client.task.impl.subscription.SubscriptionManager) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ResponseController(io.zeebe.test.broker.protocol.brokerapi.ResponseController) Test(org.junit.Test)

Example 14 with ControlMessageRequest

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));
}
Also used : SubscriptionManager(io.zeebe.client.task.impl.subscription.SubscriptionManager) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) TimeoutException(java.util.concurrent.TimeoutException) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) Future(java.util.concurrent.Future) ErrorCode(io.zeebe.protocol.clientapi.ErrorCode) RemoteAddress(io.zeebe.transport.RemoteAddress) ClientRule(io.zeebe.client.util.ClientRule) TopicSubscriberGroup(io.zeebe.client.event.impl.TopicSubscriberGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) TopicSubscriptionBuilderImpl(io.zeebe.client.event.impl.TopicSubscriptionBuilderImpl) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ResponseController(io.zeebe.test.broker.protocol.brokerapi.ResponseController) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) ActorFuture(io.zeebe.util.sched.future.ActorFuture) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) EventType(io.zeebe.protocol.clientapi.EventType) Conditions(io.zeebe.test.util.Conditions) Optional(java.util.Optional) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) TestUtil(io.zeebe.test.util.TestUtil) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) RemoteAddress(io.zeebe.transport.RemoteAddress) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) Test(org.junit.Test)

Example 15 with ControlMessageRequest

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);
}
Also used : ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) TaskSubscription(io.zeebe.client.task.TaskSubscription) Test(org.junit.Test)

Aggregations

ControlMessageRequest (io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest)15 Test (org.junit.Test)15 PollableTaskSubscription (io.zeebe.client.task.PollableTaskSubscription)6 ZeebeClient (io.zeebe.client.ZeebeClient)5 TaskSubscription (io.zeebe.client.task.TaskSubscription)5 ClientRule (io.zeebe.client.util.ClientRule)5 ControlMessageType (io.zeebe.protocol.clientapi.ControlMessageType)5 StubBrokerRule (io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule)5 RemoteAddress (io.zeebe.transport.RemoteAddress)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 ZeebeClientImpl (io.zeebe.client.impl.ZeebeClientImpl)4 ErrorCode (io.zeebe.protocol.clientapi.ErrorCode)4 EventType (io.zeebe.protocol.clientapi.EventType)4 ExecuteCommandRequest (io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest)4 TestUtil (io.zeebe.test.util.TestUtil)4 TestUtil.waitUntil (io.zeebe.test.util.TestUtil.waitUntil)4 Duration (java.time.Duration)4 TimeUnit (java.util.concurrent.TimeUnit)4