Search in sources :

Example 26 with RemoteAddress

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

the class PartitionedTopicSubscriptionTest method shouldReceiveEventsFromMultiplePartitions.

@Test
public void shouldReceiveEventsFromMultiplePartitions() {
    // given
    final int subscriberKey1 = 456;
    broker1.stubTopicSubscriptionApi(subscriberKey1);
    final int subscriberKey2 = 789;
    broker2.stubTopicSubscriptionApi(subscriberKey2);
    final RecordingEventHandler eventHandler = new RecordingEventHandler();
    client.topics().newSubscription(TOPIC).handler(eventHandler).name("hohoho").open();
    final RemoteAddress clientAddressFromBroker1 = broker1.getReceivedCommandRequests().get(0).getSource();
    final RemoteAddress clientAddressFromBroker2 = broker2.getReceivedCommandRequests().get(0).getSource();
    // when
    final long key1 = 3;
    broker1.pushTopicEvent(clientAddressFromBroker1, b -> b.partitionId(PARTITION_1).subscriberKey(subscriberKey1).key(key1));
    final long key2 = 4;
    broker2.pushTopicEvent(clientAddressFromBroker2, b -> b.partitionId(PARTITION_2).subscriberKey(subscriberKey2).key(key2));
    // then
    waitUntil(() -> eventHandler.numRecordedEvents() == 2);
    assertThat(eventHandler.getRecordedEvents()).extracting("metadata.key").containsExactlyInAnyOrder(key1, key2);
}
Also used : RemoteAddress(io.zeebe.transport.RemoteAddress) Test(org.junit.Test)

Example 27 with RemoteAddress

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

the class PollableTopicSubscriptionTest method shouldAcknowledgeOnlyOnceWhenTriggeredMultipleTimes.

@Test
public void shouldAcknowledgeOnlyOnceWhenTriggeredMultipleTimes() throws InterruptedException {
    // given
    final long subscriberKey = 123L;
    broker.stubTopicSubscriptionApi(subscriberKey);
    final ResponseController ackResponseController = stubAcknowledgeRequest().registerControlled();
    final PollableTopicSubscription subscription = clientRule.topics().newPollableSubscription(clientRule.getDefaultTopicName()).startAtHeadOfTopic().name(SUBSCRIPTION_NAME).open();
    final int subscriptionCapacity = ((ZeebeClientImpl) client).getSubscriptionPrefetchCapacity();
    final int replenishmentThreshold = (int) (subscriptionCapacity * (1.0d - Subscriber.REPLENISHMENT_THRESHOLD));
    final RemoteAddress clientAddress = receivedSubscribeCommands().findFirst().get().getSource();
    // push and handle as many events such that an ACK is triggered
    for (int i = 0; i < replenishmentThreshold + 1; i++) {
        broker.pushTopicEvent(clientAddress, subscriberKey, i, i);
    }
    final AtomicInteger handledEvents = new AtomicInteger(0);
    doRepeatedly(() -> handledEvents.addAndGet(subscription.poll(DO_NOTHING))).until(e -> e == replenishmentThreshold + 1);
    waitUntil(() -> receivedAckRequests().count() == 1);
    // when consuming another event (while the ACK is not yet confirmed)
    broker.pushTopicEvent(clientAddress, subscriberKey, 99, 99);
    doRepeatedly(() -> subscription.poll(DO_NOTHING)).until(e -> e == 1);
    // then
    // give some time for another ACK request
    Thread.sleep(500L);
    ackResponseController.unblockNextResponse();
    final List<ExecuteCommandRequest> ackRequests = receivedAckRequests().collect(Collectors.toList());
    // and not two
    assertThat(ackRequests).hasSize(1);
    // unblock the request so tear down succeeds
    stubAcknowledgeRequest().register();
}
Also used : RemoteAddress(io.zeebe.transport.RemoteAddress) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) ResponseController(io.zeebe.test.broker.protocol.brokerapi.ResponseController) Test(org.junit.Test)

Example 28 with RemoteAddress

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

the class TopicSubscriptionTest method shouldContinueEventHandlingAfterSuccessfulRetry.

@Test
public void shouldContinueEventHandlingAfterSuccessfulRetry() {
    // given
    broker.stubTopicSubscriptionApi(123L);
    final AtomicInteger counter = new AtomicInteger(3);
    final FailingHandler handler = new FailingHandler(e -> e.getMetadata().getPosition() == 1L && counter.decrementAndGet() > 0);
    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(() -> handler.getRecordedEvents().stream().anyMatch(re -> re.getMetadata().getPosition() == 2L));
    final List<Long> handledEventPositions = handler.getRecordedEvents().stream().map((re) -> re.getMetadata().getPosition()).collect(Collectors.toList());
    assertThat(handledEventPositions).containsExactly(1L, 1L, 1L, 2L);
}
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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 29 with RemoteAddress

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

the class TopicSubscriptionTest method shouldInvokeTasktHandlerForTaskEvent.

@Test
public void shouldInvokeTasktHandlerForTaskEvent() {
    // given
    broker.stubTopicSubscriptionApi(123L);
    final RecordingTopicEventHandler eventHandler = subscribeToAllEvents();
    final RemoteAddress clientAddress = broker.getReceivedCommandRequests().get(0).getSource();
    // when pushing two events
    broker.pushTopicEvent(clientAddress, 123L, 1L, 1L, EventType.TASK_EVENT);
    broker.pushTopicEvent(clientAddress, 123L, 1L, 2L, EventType.TASK_EVENT);
    // then
    waitUntil(() -> eventHandler.numTaskEvents() >= 2);
    final TaskEvent event1 = eventHandler.taskEvents.get(0);
    final TaskEvent event2 = eventHandler.taskEvents.get(1);
    assertMetadata(event1, 1L, 1L, TopicEventType.TASK);
    assertMetadata(event2, 1L, 2L, TopicEventType.TASK);
    assertThat(eventHandler.numTopicEvents()).isEqualTo(0);
    assertThat(eventHandler.numTaskEvents()).isEqualTo(2);
    assertThat(eventHandler.numWorkflowEvents()).isEqualTo(0);
    assertThat(eventHandler.numWorkflowInstanceEvents()).isEqualTo(0);
    assertThat(eventHandler.numIncidentEvents()).isEqualTo(0);
}
Also used : RemoteAddress(io.zeebe.transport.RemoteAddress) Test(org.junit.Test)

Example 30 with RemoteAddress

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

the class TopicSubscriptionTest method shouldInvokeIncidentEventHandlerForIncidentEvent.

@Test
public void shouldInvokeIncidentEventHandlerForIncidentEvent() {
    // given
    broker.stubTopicSubscriptionApi(123L);
    final RecordingTopicEventHandler eventHandler = subscribeToAllEvents();
    final RemoteAddress clientAddress = broker.getReceivedCommandRequests().get(0).getSource();
    // when pushing two events
    broker.pushTopicEvent(clientAddress, 123L, 1L, 1L, EventType.INCIDENT_EVENT);
    broker.pushTopicEvent(clientAddress, 123L, 1L, 2L, EventType.INCIDENT_EVENT);
    // then
    waitUntil(() -> eventHandler.numIncidentEvents() >= 2);
    final IncidentEvent event1 = eventHandler.incidentEvents.get(0);
    final IncidentEvent event2 = eventHandler.incidentEvents.get(1);
    assertMetadata(event1, 1L, 1L, TopicEventType.INCIDENT);
    assertMetadata(event2, 1L, 2L, TopicEventType.INCIDENT);
    assertThat(eventHandler.numTopicEvents()).isEqualTo(0);
    assertThat(eventHandler.numTaskEvents()).isEqualTo(0);
    assertThat(eventHandler.numWorkflowEvents()).isEqualTo(0);
    assertThat(eventHandler.numWorkflowInstanceEvents()).isEqualTo(0);
    assertThat(eventHandler.numIncidentEvents()).isEqualTo(2);
}
Also used : RemoteAddress(io.zeebe.transport.RemoteAddress) 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