Search in sources :

Example 1 with ControlMessageRequest

use of io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest 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 2 with ControlMessageRequest

use of io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldNotLoseCreditsOnFailureToReportTaskFailure.

/**
 * i.e. if signalling task failure itself fails
 */
@Test
public void shouldNotLoseCreditsOnFailureToReportTaskFailure() throws InterruptedException {
    // given
    broker.stubTaskSubscriptionApi(123L);
    failTaskFailure();
    final int subscriptionCapacity = 8;
    final AtomicInteger failedTasks = new AtomicInteger(0);
    final TaskHandler taskHandler = (c, t) -> {
        failedTasks.incrementAndGet();
        throw new RuntimeException("foo");
    };
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(taskHandler).lockOwner("owner").lockTime(1000L).taskFetchSize(subscriptionCapacity).taskType("foo").open();
    final RemoteAddress clientAddress = broker.getReceivedControlMessageRequests().get(0).getSource();
    for (int i = 0; i < subscriptionCapacity; i++) {
        broker.pushLockedTask(clientAddress, 123L, i, i, "owner", "foo");
    }
    // when
    TestUtil.waitUntil(() -> failedTasks.get() == 8);
    // give the client a bit of time to submit credits; this is not coupled to any defined event, so we just sleep for a bit
    Thread.sleep(500L);
    // then
    final List<ControlMessageRequest> creditRequests = getCreditRequests().collect(Collectors.toList());
    assertThat(creditRequests).isNotEmpty();
    final int numSubmittedCredits = creditRequests.stream().mapToInt((r) -> (int) r.getData().get("credits")).sum();
    assertThat(numSubmittedCredits).isGreaterThan(0);
}
Also used : TasksClient(io.zeebe.client.TasksClient) TaskEvent(io.zeebe.client.event.TaskEvent) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) HashMap(java.util.HashMap) 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) ErrorCode(io.zeebe.protocol.clientapi.ErrorCode) RemoteAddress(io.zeebe.transport.RemoteAddress) ClientRule(io.zeebe.client.util.ClientRule) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) ExpectedException(org.junit.rules.ExpectedException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Before(org.junit.Before) MsgPackHelper(io.zeebe.test.broker.protocol.MsgPackHelper) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Properties(java.util.Properties) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Test(org.junit.Test) TaskSubscription(io.zeebe.client.task.TaskSubscription) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) Collectors(java.util.stream.Collectors) TasksClientImpl(io.zeebe.client.impl.TasksClientImpl) SubscriptionType(io.zeebe.protocol.clientapi.SubscriptionType) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) EventType(io.zeebe.protocol.clientapi.EventType) ClientProperties(io.zeebe.client.ClientProperties) TaskHandler(io.zeebe.client.task.TaskHandler) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Subscriber(io.zeebe.client.task.impl.subscription.Subscriber) TestUtil(io.zeebe.test.util.TestUtil) ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) RemoteAddress(io.zeebe.transport.RemoteAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TaskHandler(io.zeebe.client.task.TaskHandler) Test(org.junit.Test)

Example 3 with ControlMessageRequest

use of io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldSendCorrectCreditsRequest.

@Test
public void shouldSendCorrectCreditsRequest() {
    // given
    broker.stubTaskSubscriptionApi(123L);
    final TasksClientImpl taskClient = (TasksClientImpl) clientRule.tasks();
    // when
    taskClient.increaseSubscriptionCredits(StubBrokerRule.TEST_PARTITION_ID).credits(123).subscriberKey(456L).execute();
    // then
    final List<ControlMessageRequest> controlMessageRequests = broker.getReceivedControlMessageRequests().stream().filter(r -> r.messageType() == ControlMessageType.INCREASE_TASK_SUBSCRIPTION_CREDITS).collect(Collectors.toList());
    assertThat(controlMessageRequests).hasSize(1);
    final ControlMessageRequest request = controlMessageRequests.get(0);
    assertThat(request.messageType()).isEqualTo(ControlMessageType.INCREASE_TASK_SUBSCRIPTION_CREDITS);
    assertThat(request.getData()).contains(entry("credits", 123), entry("subscriberKey", 456));
}
Also used : ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) TasksClient(io.zeebe.client.TasksClient) TaskEvent(io.zeebe.client.event.TaskEvent) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) HashMap(java.util.HashMap) 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) ErrorCode(io.zeebe.protocol.clientapi.ErrorCode) RemoteAddress(io.zeebe.transport.RemoteAddress) ClientRule(io.zeebe.client.util.ClientRule) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) ExpectedException(org.junit.rules.ExpectedException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Before(org.junit.Before) MsgPackHelper(io.zeebe.test.broker.protocol.MsgPackHelper) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Properties(java.util.Properties) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Test(org.junit.Test) TaskSubscription(io.zeebe.client.task.TaskSubscription) Instant(java.time.Instant) Assertions.entry(org.assertj.core.api.Assertions.entry) Collectors(java.util.stream.Collectors) TasksClientImpl(io.zeebe.client.impl.TasksClientImpl) SubscriptionType(io.zeebe.protocol.clientapi.SubscriptionType) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) List(java.util.List) Stream(java.util.stream.Stream) Rule(org.junit.Rule) EventType(io.zeebe.protocol.clientapi.EventType) ClientProperties(io.zeebe.client.ClientProperties) TaskHandler(io.zeebe.client.task.TaskHandler) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Subscriber(io.zeebe.client.task.impl.subscription.Subscriber) TestUtil(io.zeebe.test.util.TestUtil) TasksClientImpl(io.zeebe.client.impl.TasksClientImpl) Test(org.junit.Test)

Example 4 with ControlMessageRequest

use of io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldOpenSubscriptionWithLockTimeAsDuration.

@Test
public void shouldOpenSubscriptionWithLockTimeAsDuration() {
    // given
    broker.stubTaskSubscriptionApi(123L);
    // when
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(DO_NOTHING).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 5 with ControlMessageRequest

use of io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest in project zeebe by zeebe-io.

the class TaskSubscriptionTest method shouldReopenSubscriptionAfterChannelInterruption.

@Test
public void shouldReopenSubscriptionAfterChannelInterruption() {
    // given
    broker.stubTaskSubscriptionApi(123L);
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(DO_NOTHING).lockOwner("foo").lockTime(10000L).taskType("bar").open();
    // when
    broker.interruptAllServerChannels();
    // then
    TestUtil.waitUntil(() -> getSubscribeRequests().count() == 2);
    final ControlMessageRequest reopenRequest = getSubscribeRequests().skip(1).findFirst().get();
    assertThat(reopenRequest.getData()).contains(entry("lockOwner", "foo"), entry("lockDuration", 10000), entry("taskType", "bar"));
}
Also used : ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) 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