Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unflake timer start event test #10867

Merged
merged 5 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.stream.Collectors;
import org.awaitility.Awaitility;
import org.junit.Rule;
import org.junit.Test;

Expand Down Expand Up @@ -1090,45 +1089,28 @@ public void shouldTriggerOnlyTwice() {
// when
engine.increaseTime(Duration.ofSeconds(5));

// disable because we'll await with Awaitility
RecordingExporter.disableAwaitingIncomingRecords();

// then
Awaitility.await()
.untilAsserted(
() -> {
// due timers are only checked if the engine is not currently processing #10112
// so we may need to move the clock slightly for each check
engine.increaseTime(Duration.ofMillis(100));
assertThat(RecordingExporter.timerRecords(TimerIntent.TRIGGERED).limit(4))
.extracting(record -> record.getValue().getProcessDefinitionKey())
.containsExactly(
firstDeploymentProcessDefinitionKey,
secondDeploymentProcessDefinitionKey,
firstDeploymentProcessDefinitionKey,
secondDeploymentProcessDefinitionKey);
});
assertThat(RecordingExporter.timerRecords(TimerIntent.TRIGGERED).limit(4))
.extracting(record -> record.getValue().getProcessDefinitionKey())
.containsExactly(
firstDeploymentProcessDefinitionKey,
secondDeploymentProcessDefinitionKey,
firstDeploymentProcessDefinitionKey,
secondDeploymentProcessDefinitionKey);

// when
engine.increaseTime(Duration.ofSeconds(10));

// then
Awaitility.await()
.untilAsserted(
() -> {
// due timers are only checked if the engine is not currently processing #10112
// so we may need to move the clock slightly for each check
engine.increaseTime(Duration.ofMillis(100));
assertThat(RecordingExporter.timerRecords(TimerIntent.TRIGGERED).limit(5))
.describedAs("Expect that start_1 triggered twice and start_2 triggered thrice")
.extracting(record -> record.getValue().getProcessDefinitionKey())
.containsExactly(
firstDeploymentProcessDefinitionKey,
secondDeploymentProcessDefinitionKey,
firstDeploymentProcessDefinitionKey,
secondDeploymentProcessDefinitionKey,
secondDeploymentProcessDefinitionKey);
});
assertThat(RecordingExporter.timerRecords(TimerIntent.TRIGGERED).limit(5))
.describedAs("Expect that start_1 triggered twice and start_2 triggered thrice")
.extracting(record -> record.getValue().getProcessDefinitionKey())
.containsExactly(
firstDeploymentProcessDefinitionKey,
secondDeploymentProcessDefinitionKey,
firstDeploymentProcessDefinitionKey,
secondDeploymentProcessDefinitionKey,
secondDeploymentProcessDefinitionKey);
}

@Test
Expand Down Expand Up @@ -1284,11 +1266,10 @@ public void shouldAvoidTriggeringMultipleTimes() {
final long processDefinitionKey = deployedProcess.getProcessDefinitionKey();

// when
engine.stop();
final long engineStoppedTime = engine.getClock().getCurrentTimeInMillis();
engine.forEachPartition(engine::pauseProcessing);
final long enginePausedTime = engine.getClock().getCurrentTimeInMillis();
engine.increaseTime(Duration.ofMinutes(35));
RecordingExporter.reset();
engine.start();
engine.forEachPartition(engine::resumeProcessing);

// then
final Record<TimerRecordValue> firstRecord =
Expand All @@ -1301,7 +1282,7 @@ public void shouldAvoidTriggeringMultipleTimes() {
.hasTargetElementId("start")
.hasElementInstanceKey(TimerInstance.NO_ELEMENT_INSTANCE);

assertThat(firstRecord.getTimestamp()).isGreaterThan(engineStoppedTime);
assertThat(firstRecord.getTimestamp()).isGreaterThan(enginePausedTime);

final TimerRecordValue secondTimerRecord =
RecordingExporter.timerRecords(TimerIntent.CREATED)
Expand Down
13 changes: 13 additions & 0 deletions engine/src/test/java/io/camunda/zeebe/engine/util/EngineRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import io.camunda.zeebe.scheduler.future.ActorFuture;
import io.camunda.zeebe.scheduler.future.CompletableActorFuture;
import io.camunda.zeebe.streamprocessor.StreamProcessor;
import io.camunda.zeebe.streamprocessor.StreamProcessor.Phase;
import io.camunda.zeebe.streamprocessor.StreamProcessorListener;
import io.camunda.zeebe.streamprocessor.StreamProcessorMode;
import io.camunda.zeebe.streamprocessor.TypedRecordImpl;
Expand Down Expand Up @@ -233,6 +234,18 @@ public void forEachPartition(final Consumer<Integer> partitionIdConsumer) {
}

public void increaseTime(final Duration duration) {
final var streamProcessor = environmentRule.getStreamProcessor(PARTITION_ID);
if (streamProcessor.getCurrentPhase().join() == Phase.PROCESSING) {
// When time traveling, we're generally want to make sure that the entire state machine cycle
// for processing a record is completed, including the execution of post-commit tasks. For
// example, we're often interested in scheduled timers when time traveling in tests, for which
// the due date checker is scheduled through a post-commit task. When the engine has reached
// the end of the log, all post-commit tasks have also been applied, because the state machine
// will have executed them before switching the hasReachEnd flag.
Awaitility.await("Expect that engine reaches the end of the log before increasing the time")
.until(this::hasReachedEnd);
}

environmentRule.getClock().addTime(duration);
}

Expand Down