Skip to content

Commit 4dcc82a

Browse files
authored
fix: Fix race in TestEnvironment test causing flakes (#1266)
1 parent 657dd91 commit 4dcc82a

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

packages/test/src/test-testenvironment.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import anyTest, { TestFn } from 'ava';
22
import { v4 as uuid4 } from 'uuid';
33
import { WorkflowFailedError } from '@temporalio/client';
44
import { TestWorkflowEnvironment, workflowInterceptorModules } from '@temporalio/testing';
5-
import { Connection } from '@temporalio/testing/lib/connection';
65
import { bundleWorkflowCode, WorkflowBundleWithSourceMap } from '@temporalio/worker';
76
import {
87
assertFromWorkflow,
@@ -79,11 +78,6 @@ test.serial('TestEnvironment can toggle between normal and skipped time', async
7978
});
8079

8180
test.serial('TestEnvironment sleep can be used to delay activity completion', async (t) => {
82-
// TODO: check why this fails on windows
83-
if (process.platform === 'win32') {
84-
t.pass();
85-
return;
86-
}
8781
const { client, nativeConnection, sleep } = t.context.testEnv;
8882

8983
const worker = await Worker.create({
@@ -106,26 +100,14 @@ test.serial('TestEnvironment sleep can be used to delay activity completion', as
106100
t.is(winner, expectedWinner);
107101
};
108102
await worker.runUntil(async () => {
109-
// TODO: there's an issue with the Java test server where if an activity
110-
// does not complete before its scheduling workflow, time skipping stays
111-
// locked.
112-
// If the order of the below 2 statements is reversed, this test will hang.
113103
await run('activity');
114104
await run('timer');
115105
});
116106
t.pass();
117107
});
118108

119109
test.serial('TestEnvironment sleep can be used to delay sending a signal', async (t) => {
120-
// TODO: check why this fails on windows
121-
if (process.platform === 'win32') {
122-
t.pass();
123-
return;
124-
}
125110
const { client, nativeConnection, sleep } = t.context.testEnv;
126-
// TODO: due to the test server issue mentioned in the test avove we need to manually unlock time skipping
127-
// for the current test to balance out the time skipping lock counter.
128-
await (t.context.testEnv.connection as Connection).testService.unlockTimeSkipping({});
129111

130112
const worker = await Worker.create({
131113
connection: nativeConnection,

packages/test/src/workflows/testenv-test-workflows.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ export async function raceActivityAndTimer(expectedWinner: 'timer' | 'activity')
1616
const timerShouldWin = expectedWinner === 'timer';
1717
const timerDuration = timerShouldWin ? 1_000_000 : 1_500_000;
1818
const activityDuration = timerShouldWin ? 1_500_000 : 1_000_000;
19-
return await Promise.race([
20-
sleep(timerDuration).then(() => 'timer'),
21-
activities.sleep(activityDuration).then(() => 'activity'),
22-
]);
19+
20+
const timerPromise = sleep(timerDuration).then(() => 'timer');
21+
const activityPromise = activities.sleep(activityDuration).then(() => 'activity');
22+
const winner = await Promise.race([timerPromise, activityPromise]);
23+
24+
// TODO: there's an issue with the Java test server where if an activity does not complete
25+
// before its scheduling workflow, time skipping stays locked, thus potentially causing errors
26+
// on later tests. Work around this by making sure activity is completed before returning.
27+
// See https://github.com/temporalio/sdk-java/issues/1138
28+
await activityPromise;
29+
30+
return winner;
2331
}
2432

2533
export async function waitOnSignalWithTimeout(): Promise<void> {

packages/worker/src/worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ export class Worker {
765765
*/
766766
shutdown(): void {
767767
if (this.state !== 'RUNNING') {
768-
throw new IllegalStateError('Not running');
768+
throw new IllegalStateError(`Not running. Current state: ${this.state}`);
769769
}
770770
this.state = 'STOPPING';
771771
this.nativeWorker

0 commit comments

Comments
 (0)