Search in sources :

Example 31 with ExecuteCommandRequest

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

the class CreateTaskTest method shouldSetPayloadAsStream.

@Test
public void shouldSetPayloadAsStream() {
    // given
    brokerRule.onExecuteCommandRequest(EventType.TASK_EVENT, "CREATE").respondWith().key(123).event().allOf((r) -> r.getCommand()).put("state", "CREATED").put("lockTime", Protocol.INSTANT_NULL_VALUE).put("lockOwner", "").done().register();
    final String payload = "{\"foo\":\"bar\"}";
    // when
    clientRule.tasks().create(clientRule.getDefaultTopicName(), "fooType").payload(new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8))).execute();
    // then
    final ExecuteCommandRequest request = brokerRule.getReceivedCommandRequests().get(0);
    assertThat(request.getCommand()).contains(entry("payload", converter.convertToMsgPack(payload)));
}
Also used : TaskEvent(io.zeebe.client.event.TaskEvent) Maps(org.assertj.core.util.Maps) ActorSchedulerRule(io.zeebe.util.sched.testing.ActorSchedulerRule) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) Test(org.junit.Test) Protocol(io.zeebe.protocol.Protocol) Assertions.entry(org.assertj.core.api.Assertions.entry) StandardCharsets(java.nio.charset.StandardCharsets) ExecuteCommandRequestEncoder(io.zeebe.protocol.clientapi.ExecuteCommandRequestEncoder) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) RuleChain(org.junit.rules.RuleChain) Rule(org.junit.Rule) ClientRule(io.zeebe.client.util.ClientRule) MsgPackUtil(io.zeebe.test.util.MsgPackUtil) ByteArrayInputStream(java.io.ByteArrayInputStream) EventType(io.zeebe.protocol.clientapi.EventType) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) ExpectedException(org.junit.rules.ExpectedException) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Before(org.junit.Before) CreateTaskCommand(io.zeebe.client.task.cmd.CreateTaskCommand) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 32 with ExecuteCommandRequest

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

the class CreateTaskTest method shouldCreateTask.

@Test
public void shouldCreateTask() {
    // given
    brokerRule.onExecuteCommandRequest(EventType.TASK_EVENT, "CREATE").respondWith().key(123).position(456).event().allOf((r) -> r.getCommand()).put("state", "CREATED").put("lockTime", Protocol.INSTANT_NULL_VALUE).put("lockOwner", "").done().register();
    final String payload = "{\"foo\":\"bar\"}";
    // when
    final TaskEvent taskEvent = clientRule.tasks().create(clientRule.getDefaultTopicName(), "fooType").retries(3).addCustomHeader("beverage", "apple juice").payload(payload).execute();
    // then
    final ExecuteCommandRequest request = brokerRule.getReceivedCommandRequests().get(0);
    assertThat(request.eventType()).isEqualTo(EventType.TASK_EVENT);
    assertThat(request.partitionId()).isEqualTo(StubBrokerRule.TEST_PARTITION_ID);
    assertThat(request.position()).isEqualTo(ExecuteCommandRequestEncoder.positionNullValue());
    assertThat(request.getCommand()).containsOnly(entry("state", "CREATE"), entry("retries", 3), entry("type", "fooType"), entry("headers", new HashMap<>()), entry("customHeaders", Maps.newHashMap("beverage", "apple juice")), entry("lockTime", Protocol.INSTANT_NULL_VALUE), entry("payload", converter.convertToMsgPack(payload)));
    assertThat(taskEvent.getMetadata().getKey()).isEqualTo(123L);
    assertThat(taskEvent.getMetadata().getTopicName()).isEqualTo(StubBrokerRule.TEST_TOPIC_NAME);
    assertThat(taskEvent.getMetadata().getPartitionId()).isEqualTo(StubBrokerRule.TEST_PARTITION_ID);
    assertThat(taskEvent.getMetadata().getPosition()).isEqualTo(456);
    assertThat(taskEvent.getState()).isEqualTo("CREATED");
    assertThat(taskEvent.getHeaders()).isEmpty();
    assertThat(taskEvent.getCustomHeaders()).containsOnly(entry("beverage", "apple juice"));
    assertThat(taskEvent.getLockExpirationTime()).isNull();
    assertThat(taskEvent.getLockOwner()).isEmpty();
    assertThat(taskEvent.getRetries()).isEqualTo(3);
    assertThat(taskEvent.getType()).isEqualTo("fooType");
    assertThat(taskEvent.getPayload()).isEqualTo(payload);
}
Also used : TaskEvent(io.zeebe.client.event.TaskEvent) Maps(org.assertj.core.util.Maps) ActorSchedulerRule(io.zeebe.util.sched.testing.ActorSchedulerRule) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) Test(org.junit.Test) Protocol(io.zeebe.protocol.Protocol) Assertions.entry(org.assertj.core.api.Assertions.entry) StandardCharsets(java.nio.charset.StandardCharsets) ExecuteCommandRequestEncoder(io.zeebe.protocol.clientapi.ExecuteCommandRequestEncoder) ZeebeClient(io.zeebe.client.ZeebeClient) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) RuleChain(org.junit.rules.RuleChain) Rule(org.junit.Rule) ClientRule(io.zeebe.client.util.ClientRule) MsgPackUtil(io.zeebe.test.util.MsgPackUtil) ByteArrayInputStream(java.io.ByteArrayInputStream) EventType(io.zeebe.protocol.clientapi.EventType) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) ExpectedException(org.junit.rules.ExpectedException) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Before(org.junit.Before) CreateTaskCommand(io.zeebe.client.task.cmd.CreateTaskCommand) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) HashMap(java.util.HashMap) TaskEvent(io.zeebe.client.event.TaskEvent) Test(org.junit.Test)

Example 33 with ExecuteCommandRequest

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

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

the class TopicSubscriptionTest method shouldReopenSubscriptionOnChannelInterruption.

@Test
public void shouldReopenSubscriptionOnChannelInterruption() throws InterruptedException {
    // given
    broker.stubTopicSubscriptionApi(123L);
    clientRule.topics().newSubscription(clientRule.getDefaultTopicName()).startAtHeadOfTopic().handler(DO_NOTHING).name(SUBSCRIPTION_NAME).open();
    // when
    broker.interruptAllServerChannels();
    // then
    final ExecuteCommandRequest reopenRequest = TestUtil.doRepeatedly(() -> receivedSubscribeCommands().skip(1).findFirst()).until(v -> v.isPresent()).get();
    assertThat(reopenRequest.getCommand()).containsEntry("state", "SUBSCRIBE").containsEntry("startPosition", 0).containsEntry("prefetchCapacity", 32).containsEntry("name", SUBSCRIPTION_NAME).doesNotContainEntry("forceStart", true);
}
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) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) Test(org.junit.Test)

Example 35 with ExecuteCommandRequest

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

the class TopicSubscriptionTest method shouldOpenSubscription.

@Test
public void shouldOpenSubscription() {
    // given
    broker.stubTopicSubscriptionApi(123L);
    // when
    clientRule.topics().newSubscription(clientRule.getDefaultTopicName()).startAtHeadOfTopic().handler(DO_NOTHING).name(SUBSCRIPTION_NAME).open();
    // then
    final ExecuteCommandRequest subscribeRequest = broker.getReceivedCommandRequests().stream().filter((e) -> e.eventType() == EventType.SUBSCRIBER_EVENT).findFirst().get();
    assertThat(subscribeRequest.getCommand()).containsEntry("state", "SUBSCRIBE").containsEntry("startPosition", 0).containsEntry("prefetchCapacity", 32).containsEntry("name", SUBSCRIPTION_NAME).doesNotContainEntry("forceStart", true);
}
Also used : ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) Test(org.junit.Test)

Aggregations

ExecuteCommandRequest (io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest)41 Test (org.junit.Test)30 ClientRule (io.zeebe.client.util.ClientRule)15 StubBrokerRule (io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule)15 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)15 ExpectedException (org.junit.rules.ExpectedException)15 RuleChain (org.junit.rules.RuleChain)15 EventType (io.zeebe.protocol.clientapi.EventType)14 Rule (org.junit.Rule)14 Assertions.entry (org.assertj.core.api.Assertions.entry)12 TaskEvent (io.zeebe.client.event.TaskEvent)11 Before (org.junit.Before)11 ZeebeClient (io.zeebe.client.ZeebeClient)10 TasksClient (io.zeebe.client.TasksClient)9 MsgPackConverter (io.zeebe.client.impl.data.MsgPackConverter)9 ZeebeClientImpl (io.zeebe.client.impl.ZeebeClientImpl)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ClientCommandRejectedException (io.zeebe.client.cmd.ClientCommandRejectedException)6 ControlMessageType (io.zeebe.protocol.clientapi.ControlMessageType)6 ErrorCode (io.zeebe.protocol.clientapi.ErrorCode)6