Search in sources :

Example 1 with TypedStreamWriter

use of io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter 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 TypedStreamWriter

use of io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter 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 TypedStreamWriter

use of io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter 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 TypedStreamWriter

use of io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter 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 TypedStreamWriter

use of io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter 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

TypedStreamWriter (io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter)32 TypedResponseWriter (io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedResponseWriter)30 Test (org.junit.Test)30 Consumer (java.util.function.Consumer)17 CountDownLatch (java.util.concurrent.CountDownLatch)11 UnifiedRecordValue (io.camunda.zeebe.protocol.impl.record.UnifiedRecordValue)9 CommandResponseWriter (io.camunda.zeebe.engine.processing.streamprocessor.writers.CommandResponseWriter)8 MutableZeebeState (io.camunda.zeebe.engine.state.mutable.MutableZeebeState)8 RecordMetadata (io.camunda.zeebe.protocol.impl.record.RecordMetadata)8 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)8 SideEffectProducer (io.camunda.zeebe.engine.processing.streamprocessor.sideeffect.SideEffectProducer)6 MockTypedRecord (io.camunda.zeebe.engine.util.MockTypedRecord)6 ArrayList (java.util.ArrayList)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 InOrder (org.mockito.InOrder)4 JobRecord (io.camunda.zeebe.protocol.impl.record.value.job.JobRecord)3 ProcessInstanceRecord (io.camunda.zeebe.protocol.impl.record.value.processinstance.ProcessInstanceRecord)3 ZeebeDb (io.camunda.zeebe.db.ZeebeDb)2 ZeebeDbFactory (io.camunda.zeebe.db.ZeebeDbFactory)2 Loggers (io.camunda.zeebe.engine.Loggers)2