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