Search in sources :

Example 31 with RemoteAddress

use of io.zeebe.transport.RemoteAddress in project zeebe by zeebe-io.

the class TopicSubscriptionTest method shouldOnlyAcknowledgeEventAndCloseSubscriptionAfterLastEventHasBeenHandled.

@Test
public void shouldOnlyAcknowledgeEventAndCloseSubscriptionAfterLastEventHasBeenHandled() throws InterruptedException, ExecutionException, TimeoutException {
    // given
    broker.stubTopicSubscriptionApi(123L);
    final ControllableHandler handler = new ControllableHandler();
    final TopicSubscriberGroup subscription = (TopicSubscriberGroup) 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);
    TestUtil.waitUntil(() -> handler.isWaiting());
    // when
    final ActorFuture<?> closeFuture = subscription.closeAsync();
    // then
    Thread.sleep(1000L);
    assertThat(closeFuture).isNotDone();
    boolean hasSentAck = broker.getReceivedCommandRequests().stream().filter((r) -> r.eventType() == EventType.SUBSCRIPTION_EVENT).findAny().isPresent();
    assertThat(hasSentAck).isFalse();
    // and when
    handler.signal();
    // then
    closeFuture.get(1L, TimeUnit.SECONDS);
    // and
    hasSentAck = broker.getReceivedCommandRequests().stream().filter((r) -> r.eventType() == EventType.SUBSCRIPTION_EVENT).findAny().isPresent();
    assertThat(hasSentAck).isTrue();
}
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) RemoteAddress(io.zeebe.transport.RemoteAddress) TopicSubscriberGroup(io.zeebe.client.event.impl.TopicSubscriberGroup) Test(org.junit.Test)

Example 32 with RemoteAddress

use of io.zeebe.transport.RemoteAddress in project zeebe by zeebe-io.

the class TopicSubscriptionTest method shouldRetryThreeTimesOnHandlerFailure.

@Test
public void shouldRetryThreeTimesOnHandlerFailure() throws InterruptedException {
    // given
    broker.stubTopicSubscriptionApi(123L);
    final FailingHandler handler = new FailingHandler();
    final TopicSubscription subscription = clientRule.topics().newSubscription(clientRule.getDefaultTopicName()).startAtHeadOfTopic().handler(handler).name(SUBSCRIPTION_NAME).open();
    final RemoteAddress clientAddress = broker.getReceivedCommandRequests().get(0).getSource();
    // when pushing two events
    broker.pushTopicEvent(clientAddress, 123L, 1L, 1L);
    broker.pushTopicEvent(clientAddress, 123L, 1L, 2L);
    // then
    waitUntil(() -> !subscription.isOpen());
    // wait an extra second as we might receive more events if this feature is broken
    Thread.sleep(1000L);
    assertThat(handler.getRecordedEvents()).hasSize(3);
    final Set<Long> eventPositions = handler.getRecordedEvents().stream().map((re) -> re.getMetadata().getPosition()).collect(Collectors.toSet());
    assertThat(eventPositions).containsExactly(1L);
}
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) RemoteAddress(io.zeebe.transport.RemoteAddress) Test(org.junit.Test)

Example 33 with RemoteAddress

use of io.zeebe.transport.RemoteAddress in project zeebe by zeebe-io.

the class TopicSubscriptionTest method shouldCloseSubscriptionWhenAckFails.

@Test
public void shouldCloseSubscriptionWhenAckFails() {
    // given
    final long subscriberKey = 123L;
    broker.stubTopicSubscriptionApi(subscriberKey);
    broker.onExecuteCommandRequest(EventType.SUBSCRIPTION_EVENT, "ACKNOWLEDGE").respondWithError().errorCode(ErrorCode.REQUEST_PROCESSING_FAILURE).errorData("foo").register();
    final TopicSubscription subscription = clientRule.topics().newSubscription(clientRule.getDefaultTopicName()).startAtHeadOfTopic().handler(DO_NOTHING).name(SUBSCRIPTION_NAME).open();
    final RemoteAddress clientAddress = receivedSubscribeCommands().findFirst().get().getSource();
    final int subscriptionCapacity = ((ZeebeClientImpl) client).getSubscriptionPrefetchCapacity();
    // when
    for (int i = 0; i < subscriptionCapacity; i++) {
        broker.pushTopicEvent(clientAddress, subscriberKey, i, i);
    }
    // then
    waitUntil(() -> subscription.isClosed());
    assertThat(subscription.isClosed()).isTrue();
}
Also used : RemoteAddress(io.zeebe.transport.RemoteAddress) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) Test(org.junit.Test)

Example 34 with RemoteAddress

use of io.zeebe.transport.RemoteAddress 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 35 with RemoteAddress

use of io.zeebe.transport.RemoteAddress in project zeebe by zeebe-io.

the class PartitionedTaskSubscriptionTest method shouldSumWorkCountOfPollableSubscription.

@Test
public void shouldSumWorkCountOfPollableSubscription() {
    // given
    final int subscriberKey1 = 456;
    broker1.stubTaskSubscriptionApi(subscriberKey1);
    final int subscriberKey2 = 789;
    broker2.stubTaskSubscriptionApi(subscriberKey2);
    final RecordingTaskHandler eventHandler = new RecordingTaskHandler();
    final PollableTaskSubscription subscription = client.tasks().newPollableTaskSubscription(TOPIC).taskType(TASK_TYPE).lockOwner("bumbum").lockTime(Duration.ofSeconds(6)).open();
    final RemoteAddress clientAddressFromBroker1 = broker1.getReceivedControlMessageRequests().get(0).getSource();
    final RemoteAddress clientAddressFromBroker2 = broker2.getReceivedControlMessageRequests().get(0).getSource();
    final long key1 = 3;
    broker1.newSubscribedEvent().eventType(EventType.TASK_EVENT).partitionId(PARTITION_1).subscriberKey(subscriberKey1).key(key1).subscriptionType(SubscriptionType.TASK_SUBSCRIPTION).event().done().push(clientAddressFromBroker1);
    final long key2 = 4;
    broker2.newSubscribedEvent().eventType(EventType.TASK_EVENT).partitionId(PARTITION_1).subscriberKey(subscriberKey1).key(key2).subscriptionType(SubscriptionType.TASK_SUBSCRIPTION).event().done().push(clientAddressFromBroker2);
    waitUntil(() -> ((SubscriberGroup<?>) subscription).size() == 2);
    // when
    final int polledEvents = subscription.poll(eventHandler);
    // then
    assertThat(polledEvents).isEqualTo(2);
}
Also used : RemoteAddress(io.zeebe.transport.RemoteAddress) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) SubscriberGroup(io.zeebe.client.task.impl.subscription.SubscriberGroup) Test(org.junit.Test)

Aggregations

RemoteAddress (io.zeebe.transport.RemoteAddress)35 Test (org.junit.Test)34 ZeebeClientImpl (io.zeebe.client.impl.ZeebeClientImpl)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 ControlMessageRequest (io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest)12 ExecuteCommandRequest (io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest)12 ZeebeClient (io.zeebe.client.ZeebeClient)11 ClientRule (io.zeebe.client.util.ClientRule)11 ControlMessageType (io.zeebe.protocol.clientapi.ControlMessageType)11 ErrorCode (io.zeebe.protocol.clientapi.ErrorCode)11 EventType (io.zeebe.protocol.clientapi.EventType)11 StubBrokerRule (io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule)11 TestUtil (io.zeebe.test.util.TestUtil)11 TestUtil.waitUntil (io.zeebe.test.util.TestUtil.waitUntil)11 Duration (java.time.Duration)11 List (java.util.List)11 TimeUnit (java.util.concurrent.TimeUnit)11 Collectors (java.util.stream.Collectors)11 Stream (java.util.stream.Stream)11 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)11