Search in sources :

Example 1 with TopicSubscriberGroup

use of io.zeebe.client.event.impl.TopicSubscriberGroup in project zeebe by zeebe-io.

the class PollableTopicSubscriptionTest method shouldPollEventsWhileModifyingSubscribers.

@Test
public void shouldPollEventsWhileModifyingSubscribers() throws InterruptedException {
    // given
    final int subscriberKey = 456;
    broker.stubTopicSubscriptionApi(subscriberKey);
    final ControllableHandler handler = new ControllableHandler();
    final TopicSubscriberGroup subscription = (TopicSubscriberGroup) client.topics().newPollableSubscription(clientRule.getDefaultTopicName()).name("hohoho").open();
    final RemoteAddress clientAddress = broker.getReceivedCommandRequests().get(0).getSource();
    broker.pushTopicEvent(clientAddress, subscriberKey, 1, 1);
    // event is received
    waitUntil(() -> subscription.size() == 1);
    final AtomicReference<Throwable> failure = new AtomicReference<>();
    final Thread poller = new Thread(() -> subscription.poll(handler));
    poller.setUncaughtExceptionHandler((thread, throwable) -> failure.set(throwable));
    poller.start();
    waitUntil(() -> handler.isWaiting());
    // closing the subscriber, triggering reopen request
    broker.closeTransport();
    waitUntil(() -> subscription.numActiveSubscribers() == 0);
    // when continuing event handling
    handler.disableWait();
    handler.signal();
    // then the concurrent modification of subscribers did not affect the poller
    poller.join(Duration.ofSeconds(10).toMillis());
    assertThat(failure.get()).isNull();
    // make the reopen request time out immediately so
    // that the client can close without waiting for the timeout
    clientRule.getClock().addTime(Duration.ofSeconds(60));
}
Also used : RemoteAddress(io.zeebe.transport.RemoteAddress) TopicSubscriberGroup(io.zeebe.client.event.impl.TopicSubscriberGroup) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 2 with TopicSubscriberGroup

use of io.zeebe.client.event.impl.TopicSubscriberGroup in project zeebe by zeebe-io.

the class TopicSubscriptionTest method shouldCloseClientAfterSubscriptionCloseIsCalled.

@Test
public void shouldCloseClientAfterSubscriptionCloseIsCalled() throws Exception {
    // given
    broker.stubTopicSubscriptionApi(0L);
    final ResponseController responseController = broker.onControlMessageRequest((r) -> r.messageType() == ControlMessageType.REMOVE_TOPIC_SUBSCRIPTION).respondWith().data().allOf((r) -> r.getData()).done().registerControlled();
    final TopicSubscription foo = client.topics().newSubscription(clientRule.getDefaultTopicName()).handler(DO_NOTHING).name("foo").open();
    final ActorFuture<Void> future = ((TopicSubscriberGroup) foo).closeAsync();
    waitUntil(() -> broker.getReceivedControlMessageRequests().stream().filter(r -> r.messageType() == ControlMessageType.REMOVE_TOPIC_SUBSCRIPTION).count() == 1);
    final Thread closingThread = new Thread(client::close);
    closingThread.start();
    // when
    responseController.unblockNextResponse();
    // then
    closingThread.join();
    waitUntil(() -> future.isDone());
    assertThat(future).isDone();
}
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) TopicSubscriberGroup(io.zeebe.client.event.impl.TopicSubscriberGroup) ResponseController(io.zeebe.test.broker.protocol.brokerapi.ResponseController) Test(org.junit.Test)

Example 3 with TopicSubscriberGroup

use of io.zeebe.client.event.impl.TopicSubscriberGroup in project zeebe by zeebe-io.

the class PartitionedTopicSubscriptionTest method shouldCloseSuccessfulSubscriptionIfAnotherSubscriptionCannotBeOpened.

@Test
public void shouldCloseSuccessfulSubscriptionIfAnotherSubscriptionCannotBeOpened() throws Exception {
    // given
    final int subscriberKey1 = 456;
    broker1.stubTopicSubscriptionApi(subscriberKey1);
    final ResponseController responseController = broker2.onExecuteCommandRequest(EventType.SUBSCRIBER_EVENT, "SUBSCRIBE").respondWithError().errorCode(ErrorCode.REQUEST_PROCESSING_FAILURE).errorData("foo").registerControlled();
    // assuming that subscription to broker 1 is successful
    final TopicSubscriptionBuilderImpl builder = (TopicSubscriptionBuilderImpl) client.topics().newSubscription(TOPIC).handler(new RecordingEventHandler()).name("hohoho");
    final Future<TopicSubscriberGroup> groupFuture = builder.buildSubscriberGroup();
    waitUntil(() -> getOpenSubscriptionRequests(broker1).size() == 1);
    // when
    // triggering the error response and continuing
    responseController.unblockNextResponse();
    // then
    waitUntil(() -> groupFuture.isDone());
    assertFailed(groupFuture);
    final List<ControlMessageRequest> closeRequestsBroker1 = getCloseSubscriptionRequests(broker1);
    assertThat(closeRequestsBroker1).hasSize(1);
    assertThat(closeRequestsBroker1.get(0).getData().get("subscriberKey")).isEqualTo(subscriberKey1);
}
Also used : ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) TopicSubscriberGroup(io.zeebe.client.event.impl.TopicSubscriberGroup) TopicSubscriptionBuilderImpl(io.zeebe.client.event.impl.TopicSubscriptionBuilderImpl) ResponseController(io.zeebe.test.broker.protocol.brokerapi.ResponseController) Test(org.junit.Test)

Example 4 with TopicSubscriberGroup

use of io.zeebe.client.event.impl.TopicSubscriberGroup in project zeebe by zeebe-io.

the class SubscriptionManager method openTopicSubscription.

@SuppressWarnings({ "rawtypes", "unchecked" })
public ActorFuture<TopicSubscriberGroup> openTopicSubscription(TopicSubscriptionSpec spec) {
    final CompletableActorFuture<TopicSubscriberGroup> future = new CompletableActorFuture<>();
    actor.call(() -> {
        final TopicSubscriberGroup group = new TopicSubscriberGroup(actor, client, this, spec);
        topicSubscribers.addGroup(group);
        group.open((CompletableActorFuture) future);
    });
    return future;
}
Also used : CompletableActorFuture(io.zeebe.util.sched.future.CompletableActorFuture) TopicSubscriberGroup(io.zeebe.client.event.impl.TopicSubscriberGroup)

Example 5 with TopicSubscriberGroup

use of io.zeebe.client.event.impl.TopicSubscriberGroup 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)

Aggregations

TopicSubscriberGroup (io.zeebe.client.event.impl.TopicSubscriberGroup)6 Test (org.junit.Test)5 TopicSubscriptionBuilderImpl (io.zeebe.client.event.impl.TopicSubscriptionBuilderImpl)4 ControlMessageRequest (io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest)4 ResponseController (io.zeebe.test.broker.protocol.brokerapi.ResponseController)4 RemoteAddress (io.zeebe.transport.RemoteAddress)4 ZeebeClient (io.zeebe.client.ZeebeClient)3 ZeebeClientImpl (io.zeebe.client.impl.ZeebeClientImpl)3 SubscriptionManager (io.zeebe.client.task.impl.subscription.SubscriptionManager)3 ClientRule (io.zeebe.client.util.ClientRule)3 ControlMessageType (io.zeebe.protocol.clientapi.ControlMessageType)3 ErrorCode (io.zeebe.protocol.clientapi.ErrorCode)3 EventType (io.zeebe.protocol.clientapi.EventType)3 ExecuteCommandRequest (io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest)3 StubBrokerRule (io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule)3 Conditions (io.zeebe.test.util.Conditions)3 TestUtil (io.zeebe.test.util.TestUtil)3 TestUtil.waitUntil (io.zeebe.test.util.TestUtil.waitUntil)3 ActorFuture (io.zeebe.util.sched.future.ActorFuture)3 Duration (java.time.Duration)3