Skip to content

Commit b32b578

Browse files
committed
flaky test fix
1 parent 9569714 commit b32b578

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

packages/test/src/helpers-integration.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,16 @@ export function configurableHelpers<T>(
285285
};
286286
}
287287

288+
/**
289+
* Set the pause state of an activity and wait for the state to be reflected in the workflow description.
290+
*
291+
* This function handles a race condition where an activity might complete (due to catching a cancellation)
292+
* before we can verify that it's been paused. In that case, if the activity is no longer in pendingActivities
293+
* and we're trying to pause it, we consider the operation successful rather than timing out.
294+
*
295+
* This is particularly important for tests where the activity can complete immediately upon receiving a pause
296+
* cancellation.
297+
*/
288298
export async function setActivityPauseState(handle: WorkflowHandle, activityId: string, pause: boolean): Promise<void> {
289299
const desc = await handle.describe();
290300
const req = {
@@ -303,7 +313,14 @@ export async function setActivityPauseState(handle: WorkflowHandle, activityId:
303313
await waitUntil(async () => {
304314
const { raw } = await handle.describe();
305315
const activityInfo = raw.pendingActivities?.find((act) => act.activityId === activityId);
306-
if (!activityInfo) return false;
316+
317+
// If we're trying to pause and the activity is no longer in pendingActivities,
318+
// it might have completed due to catching the cancellation. Consider this a success.
319+
if (!activityInfo) {
320+
// Only consider this a success for pause operations, not unpause
321+
return pause;
322+
}
323+
307324
if (pause) {
308325
return activityInfo.paused ?? false;
309326
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,10 +1517,22 @@ test('Activity pause returns expected cancellation details', async (t) => {
15171517
const handle = await startWorkflow(heartbeatPauseWorkflow, { args: [testActivityId, true, 1] });
15181518

15191519
await waitUntil(async () => handle.query(activityStartedQuery, 1), 5000);
1520+
// Wait for activity to send at least one heartbeat before pausing
1521+
await waitUntil(async () => {
1522+
const { raw } = await handle.describe();
1523+
const activityInfo = raw.pendingActivities?.find((act) => act.activityId === testActivityId);
1524+
return activityInfo !== undefined;
1525+
}, 5000);
15201526
await setActivityPauseState(handle, testActivityId, true);
15211527
await handle.signal(proceedSignal);
15221528

15231529
await waitUntil(async () => handle.query(activityStartedQuery, 2), 5000);
1530+
// Wait for activity to be present before pausing
1531+
await waitUntil(async () => {
1532+
const { raw } = await handle.describe();
1533+
const activityInfo = raw.pendingActivities?.find((act) => act.activityId === `${testActivityId}-2`);
1534+
return activityInfo !== undefined;
1535+
}, 5000);
15241536
await setActivityPauseState(handle, `${testActivityId}-2`, true);
15251537
await handle.signal(proceedSignal);
15261538

0 commit comments

Comments
 (0)