Search in sources :

Example 41 with TaskEvent

use of io.zeebe.client.event.TaskEvent in project zeebe by zeebe-io.

the class ZeebeClientTest method shouldDistributeNewEntitiesRoundRobin.

@Test
public void shouldDistributeNewEntitiesRoundRobin() {
    // given
    final String topic = "foo";
    broker.clearTopology();
    broker.addSystemTopic();
    broker.addTopic(topic, 0);
    broker.addTopic(topic, 1);
    stubTaskResponse();
    // when
    final TaskEvent task1 = client.tasks().create(topic, "bar").execute();
    final TaskEvent task2 = client.tasks().create(topic, "bar").execute();
    final TaskEvent task3 = client.tasks().create(topic, "bar").execute();
    final TaskEvent task4 = client.tasks().create(topic, "bar").execute();
    // then
    assertThat(Arrays.asList(task1, task2, task3, task4)).extracting("metadata.partitionId").containsExactlyInAnyOrder(0, 1, 0, 1);
}
Also used : TaskEvent(io.zeebe.client.event.TaskEvent) Test(org.junit.Test)

Example 42 with TaskEvent

use of io.zeebe.client.event.TaskEvent 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 43 with TaskEvent

use of io.zeebe.client.event.TaskEvent in project zeebe by zeebe-io.

the class CreateTopicClusteredTest method shouldChooseNewLeaderForCreatedTopicAfterLeaderDies.

@Test
public void shouldChooseNewLeaderForCreatedTopicAfterLeaderDies() {
    // given
    final int partitionsCount = 1;
    clusteringRule.createTopic("foo", partitionsCount);
    final TaskEvent taskEvent = client.tasks().create("foo", "bar").execute();
    final int partitionId = taskEvent.getMetadata().getPartitionId();
    final TopologyBroker leaderForPartition = clusteringRule.getLeaderForPartition(partitionId);
    final SocketAddress currentLeaderAddress = leaderForPartition.getSocketAddress();
    // when
    clusteringRule.stopBroker(currentLeaderAddress);
    // then
    final TopologyBroker newLeader = clusteringRule.getLeaderForPartition(partitionId);
    assertThat(newLeader.getSocketAddress()).isNotEqualTo(leaderForPartition.getSocketAddress());
}
Also used : TaskEvent(io.zeebe.client.event.TaskEvent) SocketAddress(io.zeebe.transport.SocketAddress) TopologyBroker(io.zeebe.client.clustering.impl.TopologyBroker) Test(org.junit.Test)

Example 44 with TaskEvent

use of io.zeebe.client.event.TaskEvent in project zeebe by zeebe-io.

the class BrokerRecoveryTest method shouldCompleteStandaloneTaskAfterRestart.

@Test
public void shouldCompleteStandaloneTaskAfterRestart() {
    // given
    clientRule.tasks().create(clientRule.getDefaultTopic(), "foo").execute();
    final RecordingTaskHandler recordingTaskHandler = new RecordingTaskHandler();
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopic()).taskType("foo").lockOwner("test").lockTime(Duration.ofSeconds(5)).handler(recordingTaskHandler).open();
    waitUntil(() -> !recordingTaskHandler.getHandledTasks().isEmpty());
    // when
    restartBroker();
    final TaskEvent task = recordingTaskHandler.getHandledTasks().get(0);
    clientRule.tasks().complete(task).execute();
    // then
    waitUntil(() -> eventRecorder.hasTaskEvent(taskEvent("COMPLETED")));
}
Also used : RecordingTaskHandler(io.zeebe.broker.it.util.RecordingTaskHandler) TaskEvent(io.zeebe.client.event.TaskEvent) Test(org.junit.Test)

Example 45 with TaskEvent

use of io.zeebe.client.event.TaskEvent in project zeebe by zeebe-io.

the class BrokerRecoveryTest method shouldReceiveLockExpiredTasksAfterRestart.

@Test
public void shouldReceiveLockExpiredTasksAfterRestart() {
    // given
    clientRule.tasks().create(clientRule.getDefaultTopic(), "foo").execute();
    final RecordingTaskHandler recordingTaskHandler = new RecordingTaskHandler();
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopic()).taskType("foo").lockTime(Duration.ofSeconds(5)).lockOwner("test").handler(recordingTaskHandler).open();
    waitUntil(() -> !recordingTaskHandler.getHandledTasks().isEmpty());
    // when
    restartBroker();
    // wait until stream processor and scheduler process the lock task event which is not re-processed on recovery
    doRepeatedly(() -> {
        // retriggers lock expiration check in broker
        brokerRule.getClock().addTime(Duration.ofSeconds(60));
        return null;
    }).until(t -> eventRecorder.hasTaskEvent(taskEvent("LOCK_EXPIRED")));
    recordingTaskHandler.clear();
    clientRule.tasks().newTaskSubscription(clientRule.getDefaultTopic()).taskType("foo").lockTime(Duration.ofMinutes(10L)).lockOwner("test").handler(recordingTaskHandler).open();
    // then
    waitUntil(() -> !recordingTaskHandler.getHandledTasks().isEmpty());
    final TaskEvent task = recordingTaskHandler.getHandledTasks().get(0);
    clientRule.tasks().complete(task).execute();
    waitUntil(() -> eventRecorder.hasTaskEvent(taskEvent("COMPLETED")));
}
Also used : RecordingTaskHandler(io.zeebe.broker.it.util.RecordingTaskHandler) TaskEvent(io.zeebe.client.event.TaskEvent) Test(org.junit.Test)

Aggregations

TaskEvent (io.zeebe.client.event.TaskEvent)47 Test (org.junit.Test)43 RecordingTaskHandler (io.zeebe.broker.it.util.RecordingTaskHandler)20 ExpectedException (org.junit.rules.ExpectedException)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 Rule (org.junit.Rule)9 ZeebeClient (io.zeebe.client.ZeebeClient)7 ClientCommandRejectedException (io.zeebe.client.cmd.ClientCommandRejectedException)7 TaskEventImpl (io.zeebe.client.event.impl.TaskEventImpl)7 EventType (io.zeebe.protocol.clientapi.EventType)7 StubBrokerRule (io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule)7 RuleChain (org.junit.rules.RuleChain)7 TopologyBroker (io.zeebe.client.clustering.impl.TopologyBroker)6 ClientRule (io.zeebe.client.util.ClientRule)5 Events (io.zeebe.client.util.Events)5 ExecuteCommandRequest (io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest)5 SocketAddress (io.zeebe.transport.SocketAddress)5 StandardCharsets (java.nio.charset.StandardCharsets)5 Assertions.entry (org.assertj.core.api.Assertions.entry)5 WorkflowInstanceEvent (io.zeebe.client.event.WorkflowInstanceEvent)4