Skip to content

Commit ae26b46

Browse files
committed
(#109) Restructured sleep tests to use deltas
1 parent bec3df4 commit ae26b46

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/sleep.function.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {busyWaitForNanoSeconds, sleep} from "./sleep.function";
22

3+
const maxTimeDeltaInMs = 3;
4+
35
describe("sleep", () => {
46
it("should resolve after x ms", async () => {
57
// GIVEN
@@ -9,24 +11,25 @@ describe("sleep", () => {
911
const before = Date.now();
1012
await sleep(timeout);
1113
const after = Date.now();
14+
const delta = Math.abs(after - before);
1215

1316
// THEN
14-
expect(after - before).toBeGreaterThanOrEqual(timeout);
17+
expect(delta).toBeLessThanOrEqual(maxTimeDeltaInMs);
1518
});
1619
});
1720

1821
describe("busyWaitForNanoSeconds", () => {
1922
it("should resolve after x ns", async () => {
2023
// GIVEN
2124
const timeoutNs = 5_000_000;
22-
const timeoutMs = 5;
2325

2426
// WHEN
2527
const before = Date.now();
2628
await busyWaitForNanoSeconds(timeoutNs);
2729
const after = Date.now();
30+
const delta = Math.abs(after - before);
2831

2932
// THEN
30-
expect(after - before).toBeGreaterThanOrEqual(timeoutMs);
33+
expect(delta).toBeLessThanOrEqual(maxTimeDeltaInMs);
3134
});
3235
});

0 commit comments

Comments
 (0)