Search in sources :

Example 1 with TaskEventImpl

use of io.zeebe.client.event.impl.TaskEventImpl in project zeebe by zeebe-io.

the class CompleteTaskTest method shouldCompleteTask.

@Test
public void shouldCompleteTask() {
    // given
    final TaskEventImpl baseEvent = Events.exampleTask();
    brokerRule.onExecuteCommandRequest(EventType.TASK_EVENT, "COMPLETE").respondWith().key(123).event().allOf((r) -> r.getCommand()).put("state", "COMPLETED").done().register();
    final String updatedPayload = "{\"fruit\":\"cherry\"}";
    // when
    final TaskEvent taskEvent = clientRule.tasks().complete(baseEvent).payload(updatedPayload).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(baseEvent.getMetadata().getPosition());
    assertThat(request.getCommand()).containsOnly(entry("state", "COMPLETE"), entry("lockTime", baseEvent.getLockExpirationTime().toEpochMilli()), entry("lockOwner", baseEvent.getLockOwner()), entry("retries", baseEvent.getRetries()), entry("type", baseEvent.getType()), entry("headers", baseEvent.getHeaders()), entry("customHeaders", baseEvent.getCustomHeaders()), entry("payload", converter.convertToMsgPack(updatedPayload)));
    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.getState()).isEqualTo("COMPLETED");
    assertThat(taskEvent.getHeaders()).isEqualTo(baseEvent.getHeaders());
    assertThat(taskEvent.getLockExpirationTime()).isEqualTo(baseEvent.getLockExpirationTime());
    assertThat(taskEvent.getLockOwner()).isEqualTo(baseEvent.getLockOwner());
    assertThat(taskEvent.getRetries()).isEqualTo(baseEvent.getRetries());
    assertThat(taskEvent.getType()).isEqualTo(baseEvent.getType());
    assertThat(taskEvent.getPayload()).isEqualTo(updatedPayload);
}
Also used : TasksClient(io.zeebe.client.TasksClient) TaskEvent(io.zeebe.client.event.TaskEvent) Events(io.zeebe.client.util.Events) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Assertions.entry(org.assertj.core.api.Assertions.entry) StandardCharsets(java.nio.charset.StandardCharsets) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) RuleChain(org.junit.rules.RuleChain) Rule(org.junit.Rule) ClientRule(io.zeebe.client.util.ClientRule) ByteArrayInputStream(java.io.ByteArrayInputStream) EventType(io.zeebe.protocol.clientapi.EventType) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) ClientCommandRejectedException(io.zeebe.client.cmd.ClientCommandRejectedException) ExpectedException(org.junit.rules.ExpectedException) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Before(org.junit.Before) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) TaskEvent(io.zeebe.client.event.TaskEvent) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) Test(org.junit.Test)

Example 2 with TaskEventImpl

use of io.zeebe.client.event.impl.TaskEventImpl in project zeebe by zeebe-io.

the class FailTaskTest method shouldFailTask.

@Test
public void shouldFailTask() {
    // given
    final TaskEventImpl baseEvent = Events.exampleTask();
    brokerRule.onExecuteCommandRequest(EventType.TASK_EVENT, "FAIL").respondWith().key(123).event().allOf((r) -> r.getCommand()).put("state", "FAILED").done().register();
    // when
    final TaskEvent taskEvent = clientRule.tasks().fail(baseEvent).retries(4).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.getCommand()).containsOnly(entry("state", "FAIL"), entry("lockTime", baseEvent.getLockExpirationTime().toEpochMilli()), entry("lockOwner", baseEvent.getLockOwner()), entry("retries", 4), entry("type", baseEvent.getType()), entry("headers", baseEvent.getHeaders()), entry("customHeaders", baseEvent.getCustomHeaders()), entry("payload", baseEvent.getPayloadMsgPack()));
    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.getState()).isEqualTo("FAILED");
    assertThat(taskEvent.getHeaders()).isEqualTo(baseEvent.getHeaders());
    assertThat(taskEvent.getLockExpirationTime()).isEqualTo(baseEvent.getLockExpirationTime());
    assertThat(taskEvent.getLockOwner()).isEqualTo(baseEvent.getLockOwner());
    assertThat(taskEvent.getType()).isEqualTo(baseEvent.getType());
    assertThat(taskEvent.getPayload()).isEqualTo(baseEvent.getPayload());
    assertThat(taskEvent.getRetries()).isEqualTo(4);
}
Also used : TasksClient(io.zeebe.client.TasksClient) TaskEvent(io.zeebe.client.event.TaskEvent) Events(io.zeebe.client.util.Events) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Assertions.entry(org.assertj.core.api.Assertions.entry) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) RuleChain(org.junit.rules.RuleChain) Rule(org.junit.Rule) ClientRule(io.zeebe.client.util.ClientRule) EventType(io.zeebe.protocol.clientapi.EventType) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) ClientCommandRejectedException(io.zeebe.client.cmd.ClientCommandRejectedException) ExpectedException(org.junit.rules.ExpectedException) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) ExecuteCommandRequest(io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest) TaskEvent(io.zeebe.client.event.TaskEvent) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) Test(org.junit.Test)

Example 3 with TaskEventImpl

use of io.zeebe.client.event.impl.TaskEventImpl in project zeebe by zeebe-io.

the class ZeebeClientTest method shouldThrottleTopologyRefreshRequestsWhenPartitionLeaderCannotBeDetermined.

@Test
public void shouldThrottleTopologyRefreshRequestsWhenPartitionLeaderCannotBeDetermined() {
    // when
    final int nonExistingPartition = 999;
    final TaskEventImpl taskEvent = new TaskEventImpl("CREATED", new MsgPackConverter());
    taskEvent.setPartitionId(nonExistingPartition);
    assertThatThrownBy(() -> {
        client.tasks().complete(taskEvent).execute();
    });
    // +2 (one for the extra request when client is started)
    final long requestTimeout = Long.parseLong(client.getInitializationProperties().getProperty(ClientProperties.CLIENT_REQUEST_TIMEOUT_SEC));
    final long requestTimeoutMs = TimeUnit.SECONDS.toMillis(requestTimeout);
    final long expectedMaximumTopologyRequests = (requestTimeoutMs / ClientTopologyManager.MIN_REFRESH_INTERVAL_MILLIS.toMillis()) + 2;
    final long actualTopologyRequests = broker.getReceivedControlMessageRequests().stream().filter(r -> r.messageType() == ControlMessageType.REQUEST_TOPOLOGY).count();
    assertThat(actualTopologyRequests).isLessThanOrEqualTo(expectedMaximumTopologyRequests);
}
Also used : TaskEvent(io.zeebe.client.event.TaskEvent) java.util(java.util) Events(io.zeebe.client.util.Events) TransportListener(io.zeebe.transport.TransportListener) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) ClientTopologyManager(io.zeebe.client.clustering.impl.ClientTopologyManager) Protocol(io.zeebe.protocol.Protocol) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) TopicSubscription(io.zeebe.client.event.TopicSubscription) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) RemoteAddress(io.zeebe.transport.RemoteAddress) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) TestName(org.junit.rules.TestName) After(org.junit.After) ClientCommandRejectedException(io.zeebe.client.cmd.ClientCommandRejectedException) ExpectedException(org.junit.rules.ExpectedException) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Before(org.junit.Before) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) java.util.concurrent(java.util.concurrent) ServerTransport(io.zeebe.transport.ServerTransport) Test(org.junit.Test) ClientTransport(io.zeebe.transport.ClientTransport) Assertions.fail(org.assertj.core.api.Assertions.fail) Rule(org.junit.Rule) ClientException(io.zeebe.client.cmd.ClientException) EventType(io.zeebe.protocol.clientapi.EventType) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) ClientApiRule(io.zeebe.test.broker.protocol.clientapi.ClientApiRule) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Test(org.junit.Test)

Example 4 with TaskEventImpl

use of io.zeebe.client.event.impl.TaskEventImpl in project zeebe by zeebe-io.

the class ZeebeClientTest method shouldIncludeCallingFrameInExceptionStacktraceOnAsyncRootCause.

@Test
public void shouldIncludeCallingFrameInExceptionStacktraceOnAsyncRootCause() throws Exception {
    // given
    final TaskEventImpl baseEvent = Events.exampleTask();
    broker.onExecuteCommandRequest(EventType.TASK_EVENT, "COMPLETE").respondWith().key(r -> r.key()).event().allOf((r) -> r.getCommand()).put("state", "COMPLETE_REJECTED").done().register();
    // when
    try {
        client.tasks().complete(baseEvent).executeAsync().get();
        fail("should throw exception");
    } catch (ExecutionException e) {
        // then
        assertThat(e.getStackTrace()).anySatisfy(frame -> {
            assertThat(frame.getClassName()).isEqualTo(this.getClass().getName());
            assertThat(frame.getMethodName()).isEqualTo(testContext.getMethodName());
        });
    }
}
Also used : TaskEvent(io.zeebe.client.event.TaskEvent) java.util(java.util) Events(io.zeebe.client.util.Events) TransportListener(io.zeebe.transport.TransportListener) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) ClientTopologyManager(io.zeebe.client.clustering.impl.ClientTopologyManager) Protocol(io.zeebe.protocol.Protocol) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) TopicSubscription(io.zeebe.client.event.TopicSubscription) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) RemoteAddress(io.zeebe.transport.RemoteAddress) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) TestName(org.junit.rules.TestName) After(org.junit.After) ClientCommandRejectedException(io.zeebe.client.cmd.ClientCommandRejectedException) ExpectedException(org.junit.rules.ExpectedException) MsgPackConverter(io.zeebe.client.impl.data.MsgPackConverter) Before(org.junit.Before) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) java.util.concurrent(java.util.concurrent) ServerTransport(io.zeebe.transport.ServerTransport) Test(org.junit.Test) ClientTransport(io.zeebe.transport.ClientTransport) Assertions.fail(org.assertj.core.api.Assertions.fail) Rule(org.junit.Rule) ClientException(io.zeebe.client.cmd.ClientException) EventType(io.zeebe.protocol.clientapi.EventType) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) ClientApiRule(io.zeebe.test.broker.protocol.clientapi.ClientApiRule) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) Test(org.junit.Test)

Example 5 with TaskEventImpl

use of io.zeebe.client.event.impl.TaskEventImpl in project zeebe by zeebe-io.

the class ZeebeClientTopologyTimeoutTest method shouldRetryTopologyRequestAfterTimeout.

@Test
public void shouldRetryTopologyRequestAfterTimeout() {
    // given
    final int topologyTimeoutSeconds = 1;
    broker.onTopologyRequest().doNotRespond();
    broker.onExecuteCommandRequest(EventType.TASK_EVENT, "COMPLETE").respondWith().key(123).event().allOf((r) -> r.getCommand()).put("state", "COMPLETED").done().register();
    final TaskEventImpl baseEvent = Events.exampleTask();
    final ZeebeClient client = buildClient();
    // wait for a hanging topology request
    waitUntil(() -> broker.getReceivedControlMessageRequests().stream().filter(r -> r.messageType() == ControlMessageType.REQUEST_TOPOLOGY).count() == 1);
    // make topology available
    broker.stubTopologyRequest();
    // let request time out
    clientClock.addTime(Duration.ofSeconds(topologyTimeoutSeconds + 1));
    // when making a new request
    final TaskEvent response = client.tasks().complete(baseEvent).execute();
    // then the topology has been refreshed and the request succeeded
    assertThat(response.getState()).isEqualTo("COMPLETED");
}
Also used : TaskEvent(io.zeebe.client.event.TaskEvent) Events(io.zeebe.client.util.Events) TestUtil.waitUntil(io.zeebe.test.util.TestUtil.waitUntil) Properties(java.util.Properties) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ControlMessageType(io.zeebe.protocol.clientapi.ControlMessageType) Test(org.junit.Test) ZeebeClientImpl(io.zeebe.client.impl.ZeebeClientImpl) StubBrokerRule(io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule) ControlledActorClock(io.zeebe.util.sched.clock.ControlledActorClock) Rule(org.junit.Rule) ClientException(io.zeebe.client.cmd.ClientException) EventType(io.zeebe.protocol.clientapi.EventType) Duration(java.time.Duration) After(org.junit.After) ExpectedException(org.junit.rules.ExpectedException) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) AutoCloseableRule(io.zeebe.test.util.AutoCloseableRule) TaskEvent(io.zeebe.client.event.TaskEvent) TaskEventImpl(io.zeebe.client.event.impl.TaskEventImpl) Test(org.junit.Test)

Aggregations

TaskEventImpl (io.zeebe.client.event.impl.TaskEventImpl)19 Test (org.junit.Test)17 TaskEvent (io.zeebe.client.event.TaskEvent)14 ExpectedException (org.junit.rules.ExpectedException)13 ClientCommandRejectedException (io.zeebe.client.cmd.ClientCommandRejectedException)12 Events (io.zeebe.client.util.Events)12 EventType (io.zeebe.protocol.clientapi.EventType)12 StubBrokerRule (io.zeebe.test.broker.protocol.brokerapi.StubBrokerRule)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 Rule (org.junit.Rule)12 TasksClient (io.zeebe.client.TasksClient)8 MsgPackConverter (io.zeebe.client.impl.data.MsgPackConverter)8 ClientRule (io.zeebe.client.util.ClientRule)8 ExecuteCommandRequest (io.zeebe.test.broker.protocol.brokerapi.ExecuteCommandRequest)8 Assertions.entry (org.assertj.core.api.Assertions.entry)8 RuleChain (org.junit.rules.RuleChain)8 Before (org.junit.Before)7 ClientException (io.zeebe.client.cmd.ClientException)5 ZeebeClientImpl (io.zeebe.client.impl.ZeebeClientImpl)4 ControlMessageType (io.zeebe.protocol.clientapi.ControlMessageType)4