Search in sources :

Example 1 with TypedResponseWriter

use of io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedResponseWriter in project zeebe by camunda.

the class StreamProcessorTest method shouldRepeatExecuteSideEffects.

@Test
public void shouldRepeatExecuteSideEffects() throws Exception {
    // given
    final CountDownLatch processLatch = new CountDownLatch(2);
    streamProcessorRule.startTypedStreamProcessor((processors, state) -> processors.onCommand(ValueType.PROCESS_INSTANCE, ProcessInstanceIntent.ACTIVATE_ELEMENT, new TypedRecordProcessor<>() {

        @Override
        public void processRecord(final long position, final TypedRecord<UnifiedRecordValue> record, final TypedResponseWriter responseWriter, final TypedStreamWriter streamWriter, final Consumer<SideEffectProducer> sideEffect) {
            sideEffect.accept(() -> {
                processLatch.countDown();
                return processLatch.getCount() < 1;
            });
        }
    }));
    // when
    streamProcessorRule.writeCommand(ProcessInstanceIntent.ACTIVATE_ELEMENT, PROCESS_INSTANCE_RECORD);
    // then
    assertThat(processLatch.await(5, TimeUnit.SECONDS)).isTrue();
}
Also used : Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) TypedStreamWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter) TypedResponseWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedResponseWriter) Test(org.junit.Test)

Example 2 with TypedResponseWriter

use of io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedResponseWriter in project zeebe by camunda.

the class StreamProcessorTest method shouldWriteResponseOnFailedEventProcessing.

@Test
public void shouldWriteResponseOnFailedEventProcessing() {
    // given
    streamProcessorRule.startTypedStreamProcessor((processors, context) -> processors.onCommand(ValueType.PROCESS_INSTANCE, ProcessInstanceIntent.ACTIVATE_ELEMENT, new TypedRecordProcessor<>() {

        @Override
        public void processRecord(final long position, final TypedRecord<UnifiedRecordValue> record, final TypedResponseWriter responseWriter, final TypedStreamWriter streamWriter, final Consumer<SideEffectProducer> sideEffect) {
            responseWriter.writeEventOnCommand(3, ProcessInstanceIntent.ELEMENT_ACTIVATING, record.getValue(), record);
            throw new RuntimeException("expected");
        }
    }));
    // when
    streamProcessorRule.writeCommand(ProcessInstanceIntent.ACTIVATE_ELEMENT, PROCESS_INSTANCE_RECORD);
    // then
    final CommandResponseWriter commandResponseWriter = streamProcessorRule.getCommandResponseWriter();
    final InOrder inOrder = inOrder(commandResponseWriter);
    // it doesn't send the staged command response
    inOrder.verify(commandResponseWriter, TIMEOUT.times(1)).key(3);
    inOrder.verify(commandResponseWriter, TIMEOUT.times(1)).intent(ProcessInstanceIntent.ELEMENT_ACTIVATING);
    inOrder.verify(commandResponseWriter, TIMEOUT.times(1)).recordType(RecordType.EVENT);
    inOrder.verify(commandResponseWriter, TIMEOUT.times(1)).valueType(ValueType.PROCESS_INSTANCE);
    // instead, it sends a rejection response because of the failure
    inOrder.verify(commandResponseWriter, TIMEOUT.times(1)).recordType(RecordType.COMMAND_REJECTION);
    inOrder.verify(commandResponseWriter, TIMEOUT.times(1)).rejectionType(RejectionType.PROCESSING_ERROR);
    inOrder.verify(commandResponseWriter, TIMEOUT.times(1)).tryWriteResponse(anyInt(), anyLong());
}
Also used : CommandResponseWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.CommandResponseWriter) InOrder(org.mockito.InOrder) Consumer(java.util.function.Consumer) TypedStreamWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter) TypedResponseWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedResponseWriter) Test(org.junit.Test)

Example 3 with TypedResponseWriter

use of io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedResponseWriter in project zeebe by camunda.

the class StreamProcessorTest method shouldSkipSideEffectsOnException.

@Test
public void shouldSkipSideEffectsOnException() throws Exception {
    // given
    final CountDownLatch processLatch = new CountDownLatch(2);
    streamProcessorRule.startTypedStreamProcessor((processors, state) -> processors.onCommand(ValueType.PROCESS_INSTANCE, ProcessInstanceIntent.ACTIVATE_ELEMENT, new TypedRecordProcessor<>() {

        @Override
        public void processRecord(final long position, final TypedRecord<UnifiedRecordValue> record, final TypedResponseWriter responseWriter, final TypedStreamWriter streamWriter, final Consumer<SideEffectProducer> sideEffect) {
            sideEffect.accept(() -> {
                throw new RuntimeException("expected");
            });
            processLatch.countDown();
        }
    }));
    // when
    streamProcessorRule.writeCommand(ProcessInstanceIntent.ACTIVATE_ELEMENT, PROCESS_INSTANCE_RECORD);
    streamProcessorRule.writeCommand(ProcessInstanceIntent.ACTIVATE_ELEMENT, PROCESS_INSTANCE_RECORD);
    // then
    assertThat(processLatch.await(5, TimeUnit.SECONDS)).isTrue();
}
Also used : Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) TypedStreamWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter) TypedResponseWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedResponseWriter) Test(org.junit.Test)

Example 4 with TypedResponseWriter

use of io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedResponseWriter in project zeebe by camunda.

the class SkipFailingEventsTest method shouldWriteErrorEventWithNoMessage.

@Test
public void shouldWriteErrorEventWithNoMessage() {
    // given
    streams.startStreamProcessor(STREAM_NAME, DefaultZeebeDbFactory.defaultFactory(), (processingContext) -> {
        zeebeState = processingContext.getZeebeState();
        return TypedRecordProcessors.processors(zeebeState.getKeyGenerator(), processingContext.getWriters()).onCommand(ValueType.PROCESS_INSTANCE, ProcessInstanceIntent.ACTIVATE_ELEMENT, new TypedRecordProcessor<>() {

            @Override
            public void processRecord(final TypedRecord<UnifiedRecordValue> record, final TypedResponseWriter responseWriter, final TypedStreamWriter streamWriter) {
                throw new NullPointerException();
            }
        });
    });
    final long failingEventPosition = streams.newRecord(STREAM_NAME).event(PROCESS_INSTANCE_RECORD).recordType(RecordType.COMMAND).intent(ProcessInstanceIntent.ACTIVATE_ELEMENT).key(keyGenerator.nextKey()).write();
    // when
    waitForRecordWhichSatisfies(e -> Records.isEvent(e, ValueType.ERROR, ErrorIntent.CREATED));
    // then
    final ErrorRecord errorRecord = new RecordStream(streams.events(STREAM_NAME)).onlyErrorRecords().getFirst().getValue();
    assertThat(errorRecord.getErrorEventPosition()).isEqualTo(failingEventPosition);
    assertThat(BufferUtil.bufferAsString(errorRecord.getExceptionMessageBuffer())).isEqualTo("Without exception message.");
    assertThat(errorRecord.getProcessInstanceKey()).isEqualTo(1);
}
Also used : RecordStream(io.camunda.zeebe.engine.util.RecordStream) UnifiedRecordValue(io.camunda.zeebe.protocol.impl.record.UnifiedRecordValue) TypedStreamWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter) TypedResponseWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedResponseWriter) ErrorRecord(io.camunda.zeebe.protocol.impl.record.value.error.ErrorRecord) Test(org.junit.Test)

Example 5 with TypedResponseWriter

use of io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedResponseWriter in project zeebe by camunda.

the class SkipFailingEventsTest method shouldNotBlacklistInstanceAndIgnoreTimerStartEvents.

@Test
public void shouldNotBlacklistInstanceAndIgnoreTimerStartEvents() {
    // given
    when(commandResponseWriter.tryWriteResponse(anyInt(), anyLong())).thenReturn(true);
    final List<Long> processedInstances = new ArrayList<>();
    final TypedRecordProcessor<DeploymentRecord> errorProneProcessor = new TypedRecordProcessor<>() {

        @Override
        public void processRecord(final TypedRecord<DeploymentRecord> record, final TypedResponseWriter responseWriter, final TypedStreamWriter streamWriter) {
            if (record.getKey() == 0) {
                throw new RuntimeException("expected");
            }
            processedInstances.add(TimerInstance.NO_ELEMENT_INSTANCE);
            streamWriter.appendFollowUpEvent(record.getKey(), TimerIntent.CREATED, Records.timer(TimerInstance.NO_ELEMENT_INSTANCE));
        }
    };
    final BpmnModelInstance process = Bpmn.createExecutableProcess("process").startEvent().timerWithDuration("PT1S").endEvent().done();
    final DeploymentRecord deploymentRecord = new DeploymentRecord();
    deploymentRecord.resources().add().setResourceName("process.bpmn").setResource(Bpmn.convertToString(process).getBytes());
    streams.startStreamProcessor(STREAM_NAME, DefaultZeebeDbFactory.defaultFactory(), (processingContext) -> {
        zeebeState = processingContext.getZeebeState();
        return TypedRecordProcessors.processors(zeebeState.getKeyGenerator(), processingContext.getWriters()).onCommand(ValueType.DEPLOYMENT, DeploymentIntent.CREATE, errorProneProcessor);
    });
    streams.newRecord(STREAM_NAME).event(deploymentRecord).recordType(RecordType.COMMAND).intent(DeploymentIntent.CREATE).key(0).write();
    streams.newRecord(STREAM_NAME).event(deploymentRecord).recordType(RecordType.COMMAND).intent(DeploymentIntent.CREATE).key(1).write();
    // when
    waitForRecordWhichSatisfies(e -> Records.isEvent(e, ValueType.TIMER, TimerIntent.CREATED));
    // then
    final RecordMetadata metadata = new RecordMetadata();
    metadata.valueType(ValueType.TIMER);
    final MockTypedRecord<TimerRecord> mockTypedRecord = new MockTypedRecord<>(0, metadata, Records.timer(TimerInstance.NO_ELEMENT_INSTANCE));
    Assertions.assertThat(zeebeState.getBlackListState().isOnBlacklist(mockTypedRecord)).isFalse();
    assertThat(processedInstances).containsExactly(TimerInstance.NO_ELEMENT_INSTANCE);
}
Also used : TimerRecord(io.camunda.zeebe.protocol.impl.record.value.timer.TimerRecord) MockTypedRecord(io.camunda.zeebe.engine.util.MockTypedRecord) ArrayList(java.util.ArrayList) BpmnModelInstance(io.camunda.zeebe.model.bpmn.BpmnModelInstance) TypedStreamWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter) TypedResponseWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedResponseWriter) RecordMetadata(io.camunda.zeebe.protocol.impl.record.RecordMetadata) DeploymentRecord(io.camunda.zeebe.protocol.impl.record.value.deployment.DeploymentRecord) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) MockTypedRecord(io.camunda.zeebe.engine.util.MockTypedRecord) Test(org.junit.Test)

Aggregations

TypedResponseWriter (io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedResponseWriter)30 TypedStreamWriter (io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter)30 Test (org.junit.Test)30 Consumer (java.util.function.Consumer)15 UnifiedRecordValue (io.camunda.zeebe.protocol.impl.record.UnifiedRecordValue)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 SideEffectProducer (io.camunda.zeebe.engine.processing.streamprocessor.sideeffect.SideEffectProducer)6 CommandResponseWriter (io.camunda.zeebe.engine.processing.streamprocessor.writers.CommandResponseWriter)6 MutableZeebeState (io.camunda.zeebe.engine.state.mutable.MutableZeebeState)6 MockTypedRecord (io.camunda.zeebe.engine.util.MockTypedRecord)6 RecordMetadata (io.camunda.zeebe.protocol.impl.record.RecordMetadata)6 ArrayList (java.util.ArrayList)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)6 InOrder (org.mockito.InOrder)6 RecordStream (io.camunda.zeebe.engine.util.RecordStream)3 BpmnModelInstance (io.camunda.zeebe.model.bpmn.BpmnModelInstance)3 DeploymentRecord (io.camunda.zeebe.protocol.impl.record.value.deployment.DeploymentRecord)3 ErrorRecord (io.camunda.zeebe.protocol.impl.record.value.error.ErrorRecord)3 JobRecord (io.camunda.zeebe.protocol.impl.record.value.job.JobRecord)3