Search in sources :

Example 6 with ControlMessageRequest

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

the class TaskSubscriptionTest method shouldUseDefaultTaskFetchSize.

@Test
public void shouldUseDefaultTaskFetchSize() {
    // given
    broker.stubTaskSubscriptionApi(123L);
    // when
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(DO_NOTHING).lockOwner("foo").lockTime(10000L).taskType("bar").open();
    // then
    final ControlMessageRequest subscriptionRequest = getSubscribeRequests().findFirst().get();
    assertThat(subscriptionRequest.messageType()).isEqualByComparingTo(ControlMessageType.ADD_TASK_SUBSCRIPTION);
    assertThat(subscriptionRequest.getData()).containsEntry("credits", 32);
}
Also used : ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) Test(org.junit.Test)

Example 7 with ControlMessageRequest

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

the class TaskSubscriptionTest method shouldCloseSubscription.

@Test
public void shouldCloseSubscription() {
    // given
    broker.stubTaskSubscriptionApi(123L);
    final TaskSubscription subscription = clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(DO_NOTHING).lockOwner("foo").lockTime(10000L).taskType("bar").open();
    // when
    subscription.close();
    // then
    assertThat(subscription.isClosed()).isTrue();
    assertThat(subscription.isOpen()).isFalse();
    final ControlMessageRequest subscriptionRequest = getUnsubscribeRequests().findFirst().get();
    assertThat(subscriptionRequest.messageType()).isEqualByComparingTo(ControlMessageType.REMOVE_TASK_SUBSCRIPTION);
    assertThat(subscriptionRequest.partitionId()).isEqualTo(clientRule.getDefaultPartitionId());
    assertThat(subscriptionRequest.getData()).contains(entry("subscriberKey", 123));
}
Also used : ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) TaskSubscription(io.zeebe.client.task.TaskSubscription) Test(org.junit.Test)

Example 8 with ControlMessageRequest

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

the class TaskSubscriptionTest method shouldOpenPollableSubscription.

@Test
public void shouldOpenPollableSubscription() {
    // given
    broker.stubTaskSubscriptionApi(123L);
    // when
    final PollableTaskSubscription subscription = clientRule.tasks().newPollableTaskSubscription(clientRule.getDefaultTopicName()).lockOwner("foo").lockTime(10000L).taskType("bar").taskFetchSize(456).open();
    // then
    assertThat(subscription.isOpen()).isTrue();
    final ControlMessageRequest subscriptionRequest = getSubscribeRequests().findFirst().get();
    assertThat(subscriptionRequest.messageType()).isEqualByComparingTo(ControlMessageType.ADD_TASK_SUBSCRIPTION);
    assertThat(subscriptionRequest.getData()).contains(entry("lockOwner", "foo"), entry("lockDuration", 10000), entry("taskType", "bar"), entry("credits", 456));
}
Also used : ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) Test(org.junit.Test)

Example 9 with ControlMessageRequest

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

the class TaskSubscriptionTest method shouldNotAttemptReplenishmentForZeroCredits.

@Test
public void shouldNotAttemptReplenishmentForZeroCredits() throws InterruptedException {
    // given
    final int subscriptionCapacity = 16;
    final int replenishmentThreshold = (int) (Math.ceil(subscriptionCapacity * Subscriber.REPLENISHMENT_THRESHOLD));
    final int tasksToHandleBeforeReplenishment = subscriptionCapacity - replenishmentThreshold;
    broker.stubTaskSubscriptionApi(123L);
    final WaitingTaskHandler handler = new WaitingTaskHandler();
    handler.shouldWait = false;
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(handler).lockOwner("owner").lockTime(10000L).taskFetchSize(subscriptionCapacity).taskType("type").open();
    final RemoteAddress clientAddress = getSubscribeRequests().findFirst().get().getSource();
    // handling these tasks should not yet trigger replenishment; the next handled task would
    for (int i = 0; i < tasksToHandleBeforeReplenishment; i++) {
        broker.pushLockedTask(clientAddress, 123L, 4L + i, 5L + i, "foo", "type");
    }
    waitUntil(() -> handler.numHandledEvents.get() == tasksToHandleBeforeReplenishment);
    handler.shouldWait = true;
    for (int i = 0; i < NUM_EXECUTION_THREADS; i++) {
        broker.pushLockedTask(clientAddress, 123L, 4L + i, 5L + i, "foo", "type");
    }
    waitUntil(() -> handler.numWaitingThreads.get() == NUM_EXECUTION_THREADS);
    // when all task handling threads trigger credit replenishment
    continueTaskHandlingThreads();
    // then
    waitUntil(() -> getCreditRequests().count() >= 1);
    // waiting for potentially more credit requests
    Thread.sleep(500L);
    final List<ControlMessageRequest> creditRequests = getCreditRequests().collect(Collectors.toList());
    assertThat(creditRequests.size()).isGreaterThanOrEqualTo(1);
    int totalReplenishedCredits = 0;
    for (ControlMessageRequest request : creditRequests) {
        final int replenishedCredits = (int) request.getData().get("credits");
        assertThat(replenishedCredits).isGreaterThan(0);
        totalReplenishedCredits += replenishedCredits;
    }
    assertThat(totalReplenishedCredits).isGreaterThanOrEqualTo(tasksToHandleBeforeReplenishment + 1);
}
Also used : ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) RemoteAddress(io.zeebe.transport.RemoteAddress) Test(org.junit.Test)

Example 10 with ControlMessageRequest

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

the class TaskSubscriptionTest method shouldOpenSubscription.

@Test
public void shouldOpenSubscription() {
    // given
    broker.stubTaskSubscriptionApi(123L);
    // when
    final TaskSubscription subscription = clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopicName()).handler(DO_NOTHING).lockOwner("foo").lockTime(10000L).taskType("bar").taskFetchSize(456).open();
    // then
    assertThat(subscription.isOpen()).isTrue();
    assertThat(subscription.isClosed()).isFalse();
    final ControlMessageRequest subscriptionRequest = getSubscribeRequests().findFirst().get();
    assertThat(subscriptionRequest.messageType()).isEqualByComparingTo(ControlMessageType.ADD_TASK_SUBSCRIPTION);
    assertThat(subscriptionRequest.partitionId()).isEqualTo(clientRule.getDefaultPartitionId());
    assertThat(subscriptionRequest.getData()).contains(entry("lockOwner", "foo"), entry("lockDuration", 10000), entry("taskType", "bar"), entry("credits", 456));
}
Also used : ControlMessageRequest(io.zeebe.test.broker.protocol.brokerapi.ControlMessageRequest) PollableTaskSubscription(io.zeebe.client.task.PollableTaskSubscription) TaskSubscription(io.zeebe.client.task.TaskSubscription) 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