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)));
}
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);
}
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();
}
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);
}
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);
}
Aggregations