Skip to content

Commit 38eeb3c

Browse files
committed
Retry the callCount assertions
1 parent 355b83e commit 38eeb3c

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

vscode/src/test/suite/helpers.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,21 @@ export function createContext() {
7070
},
7171
} as unknown as FakeContext;
7272
}
73+
74+
// Retries the given assertion function until it passes or the timeout (in ms) is exceeded.
75+
// Polls every 500ms. If the assertion never passes, the last assertion error is thrown.
76+
export async function retryForDuration(timeoutMs: number, fn: () => void) {
77+
const deadline = Date.now() + timeoutMs;
78+
79+
while (Date.now() < deadline) {
80+
try {
81+
fn();
82+
return;
83+
} catch (_error) {
84+
await new Promise((resolve) => setTimeout(resolve, 500));
85+
}
86+
}
87+
88+
// Final attempt — let it throw
89+
fn();
90+
}

vscode/src/test/suite/workspace.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { beforeEach, afterEach } from "mocha";
1010
import { Workspace } from "../../workspace";
1111

1212
import { FAKE_TELEMETRY } from "./fakeTelemetry";
13-
import { createContext, FakeContext } from "./helpers";
13+
import { createContext, FakeContext, retryForDuration } from "./helpers";
1414

1515
suite("Workspace", () => {
1616
let workspacePath: string;
@@ -56,13 +56,13 @@ suite("Workspace", () => {
5656
fs.rmSync(path.join(gitDir, "rebase-apply"));
5757
}
5858

59-
// Give enough time for all watchers to fire and all debounces to run off
60-
await new Promise((resolve) => setTimeout(resolve, 10000));
61-
62-
// The start call only happens once because of the inhibitRestart flag
63-
assert.strictEqual(startStub.callCount, 1);
64-
// The restart call only happens once because of the debounce
65-
assert.strictEqual(restartSpy.callCount, 1);
59+
// Retry assertions for up to 30 seconds to allow watchers and debounces to fire
60+
await retryForDuration(30000, () => {
61+
// The start call only happens once because of the inhibitRestart flag
62+
assert.strictEqual(startStub.callCount, 1);
63+
// The restart call only happens once because of the debounce
64+
assert.strictEqual(restartSpy.callCount, 1);
65+
});
6666
}).timeout(60000);
6767

6868
test("does not restart when watched files are touched without modifying contents", async () => {

0 commit comments

Comments
 (0)