Skip to content

Commit b579216

Browse files
toubatbrianclaude
andcommitted
test: use fake timers in manual-abort Task test to deflake CI
The test asserted an exact tick count ([0,1,2]) against real timers with a 5ms margin, which flakes on loaded CI runners. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 882491c commit b579216

1 file changed

Lines changed: 29 additions & 19 deletions

File tree

agents/src/utils.test.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -239,29 +239,39 @@ describe('utils', () => {
239239
});
240240

241241
it('should handle task that checks abort signal manually', async () => {
242-
const arr: number[] = [];
243-
const task = Task.from(async (controller) => {
244-
for (let i = 0; i < 10; i++) {
245-
if (controller.signal.aborted) {
246-
throw new Error('Task was aborted');
242+
// fake timers: with real timers the exact tick count is scheduling-
243+
// dependent and flakes on loaded CI runners
244+
vi.useFakeTimers();
245+
try {
246+
const arr: number[] = [];
247+
const task = Task.from(async (controller) => {
248+
for (let i = 0; i < 10; i++) {
249+
if (controller.signal.aborted) {
250+
throw new Error('Task was aborted');
251+
}
252+
await delay(10);
253+
arr.push(i);
247254
}
248-
await delay(10);
249-
arr.push(i);
250-
}
251-
return 'completed';
252-
});
255+
return 'completed';
256+
});
253257

254-
await delay(35);
255-
task.cancel();
258+
await vi.advanceTimersByTimeAsync(35);
259+
task.cancel();
256260

257-
expect(arr).toEqual([0, 1, 2]);
258-
try {
259-
await task.result;
260-
} catch (error: unknown) {
261-
expect((error as Error).message).toBe('Task was aborted');
262-
}
261+
expect(arr).toEqual([0, 1, 2]);
262+
// the pending (signal-less) delay must elapse for the loop to reach
263+
// its manual abort checkpoint
264+
await vi.advanceTimersByTimeAsync(10);
265+
try {
266+
await task.result;
267+
} catch (error: unknown) {
268+
expect((error as Error).message).toBe('Task was aborted');
269+
}
263270

264-
expect(task.done).toBe(true);
271+
expect(task.done).toBe(true);
272+
} finally {
273+
vi.useRealTimers();
274+
}
265275
});
266276

267277
it('should handle cleanup in finally block', async () => {

0 commit comments

Comments
 (0)