use of io.zeebe.test.broker.protocol.brokerapi.ResponseController 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();
}
use of io.zeebe.test.broker.protocol.brokerapi.ResponseController 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);
}
use of io.zeebe.test.broker.protocol.brokerapi.ResponseController 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();
}
use of io.zeebe.test.broker.protocol.brokerapi.ResponseController 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);
}
Aggregations