Skip to content

Commit 8ee58cf

Browse files
committed
test: rewrite tests with @cross/test
1 parent f3db52a commit 8ee58cf

13 files changed

+718
-699
lines changed

async_value_test.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1+
import { test } from "@cross/test";
12
import { assertEquals } from "@std/assert";
23
import { AsyncValue } from "./async_value.ts";
34

4-
Deno.test("AsyncValue", async (t) => {
5-
await t.step(
6-
"'get' returns a promise that resolves to the value set by 'set'",
7-
async () => {
8-
const v = new AsyncValue(0);
9-
assertEquals(await v.get(), 0);
10-
await v.set(1);
11-
assertEquals(await v.get(), 1);
12-
},
13-
);
14-
});
5+
test(
6+
"AsyncValue 'get' returns a promise that resolves to the value set by 'set'",
7+
async () => {
8+
const v = new AsyncValue(0);
9+
assertEquals(await v.get(), 0);
10+
await v.set(1);
11+
assertEquals(await v.get(), 1);
12+
},
13+
);

barrier_test.ts

Lines changed: 80 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,93 @@
1+
import { test } from "@cross/test";
12
import { assertEquals, assertRejects, assertThrows } from "@std/assert";
23
import { deadline, delay } from "@std/async";
34
import { Barrier } from "./barrier.ts";
45

5-
Deno.test("Barrier", async (t) => {
6-
await t.step(
7-
"'wait' waits until the number of waiters reached the size specified to the barrier",
8-
async () => {
9-
const barrier = new Barrier(5);
10-
const workers = [];
11-
const results: string[] = [];
12-
for (let i = 0; i < 5; i++) {
13-
workers.push((async () => {
14-
results.push(`before wait ${i}`);
15-
await barrier.wait();
16-
results.push(`after wait ${i}`);
17-
})());
18-
}
19-
await Promise.all(workers);
20-
assertEquals(results, [
21-
"before wait 0",
22-
"before wait 1",
23-
"before wait 2",
24-
"before wait 3",
25-
"before wait 4",
26-
"after wait 0",
27-
"after wait 1",
28-
"after wait 2",
29-
"after wait 3",
30-
"after wait 4",
31-
]);
32-
},
33-
);
6+
test(
7+
"Barrier 'wait' waits until the number of waiters reached the size specified to the barrier",
8+
async () => {
9+
const barrier = new Barrier(5);
10+
const workers = [];
11+
const results: string[] = [];
12+
for (let i = 0; i < 5; i++) {
13+
workers.push((async () => {
14+
results.push(`before wait ${i}`);
15+
await barrier.wait();
16+
results.push(`after wait ${i}`);
17+
})());
18+
}
19+
await Promise.all(workers);
20+
assertEquals(results, [
21+
"before wait 0",
22+
"before wait 1",
23+
"before wait 2",
24+
"before wait 3",
25+
"before wait 4",
26+
"after wait 0",
27+
"after wait 1",
28+
"after wait 2",
29+
"after wait 3",
30+
"after wait 4",
31+
]);
32+
},
33+
);
3434

35-
await t.step(
36-
"'wait' with non-aborted signal",
37-
async () => {
38-
const controller = new AbortController();
39-
const barrier = new Barrier(2);
35+
test(
36+
"Barrier 'wait' with non-aborted signal",
37+
async () => {
38+
const controller = new AbortController();
39+
const barrier = new Barrier(2);
4040

41-
await assertRejects(
42-
() => deadline(barrier.wait({ signal: controller.signal }), 100),
43-
DOMException,
44-
"Signal timed out.",
45-
);
46-
},
47-
);
41+
await assertRejects(
42+
() => deadline(barrier.wait({ signal: controller.signal }), 100),
43+
DOMException,
44+
"Signal timed out.",
45+
);
46+
},
47+
);
4848

49-
await t.step(
50-
"'wait' with signal aborted after delay",
51-
async () => {
52-
const controller = new AbortController();
53-
const barrier = new Barrier(2);
54-
const reason = new Error("Aborted");
49+
test(
50+
"Barrier 'wait' with signal aborted after delay",
51+
async () => {
52+
const controller = new AbortController();
53+
const barrier = new Barrier(2);
54+
const reason = new Error("Aborted");
5555

56-
delay(50).then(() => controller.abort(reason));
56+
delay(50).then(() => controller.abort(reason));
5757

58-
await assertRejects(
59-
() => deadline(barrier.wait({ signal: controller.signal }), 100),
60-
Error,
61-
"Aborted",
62-
);
63-
},
64-
);
58+
await assertRejects(
59+
() => deadline(barrier.wait({ signal: controller.signal }), 100),
60+
Error,
61+
"Aborted",
62+
);
63+
},
64+
);
6565

66-
await t.step(
67-
"'wait' with already aborted signal",
68-
async () => {
69-
const controller = new AbortController();
70-
const barrier = new Barrier(2);
71-
const reason = new Error("Aborted");
66+
test(
67+
"Barrier 'wait' with already aborted signal",
68+
async () => {
69+
const controller = new AbortController();
70+
const barrier = new Barrier(2);
71+
const reason = new Error("Aborted");
7272

73-
controller.abort(reason);
73+
controller.abort(reason);
7474

75-
await assertRejects(
76-
() => deadline(barrier.wait({ signal: controller.signal }), 100),
77-
Error,
78-
"Aborted",
79-
);
80-
},
81-
);
75+
await assertRejects(
76+
() => deadline(barrier.wait({ signal: controller.signal }), 100),
77+
Error,
78+
"Aborted",
79+
);
80+
},
81+
);
8282

83-
await t.step(
84-
"throws RangeError if size is not a positive safe integer",
85-
() => {
86-
assertThrows(() => new Barrier(NaN), RangeError);
87-
assertThrows(() => new Barrier(Infinity), RangeError);
88-
assertThrows(() => new Barrier(-Infinity), RangeError);
89-
assertThrows(() => new Barrier(-1), RangeError);
90-
assertThrows(() => new Barrier(1.1), RangeError);
91-
assertThrows(() => new Barrier(0), RangeError);
92-
},
93-
);
94-
});
83+
test(
84+
"Barrier throws RangeError if size is not a positive safe integer",
85+
() => {
86+
assertThrows(() => new Barrier(NaN), RangeError);
87+
assertThrows(() => new Barrier(Infinity), RangeError);
88+
assertThrows(() => new Barrier(-Infinity), RangeError);
89+
assertThrows(() => new Barrier(-1), RangeError);
90+
assertThrows(() => new Barrier(1.1), RangeError);
91+
assertThrows(() => new Barrier(0), RangeError);
92+
},
93+
);

deno.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@core/asyncutil/stack": "./stack.ts",
4444
"@core/asyncutil/wait-group": "./wait_group.ts",
4545
"@core/iterutil": "jsr:@core/iterutil@^0.6.0",
46+
"@cross/test": "jsr:@cross/test@^0.0.9",
4647
"@std/assert": "jsr:@std/assert@^1.0.2",
4748
"@std/async": "jsr:@std/async@^1.0.2"
4849
},

deno.lock

Lines changed: 29 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lock_test.ts

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,53 @@
1+
import { test } from "@cross/test";
12
import { assertEquals } from "@std/assert";
23
import { AsyncValue } from "./async_value.ts";
34
import { Lock } from "./lock.ts";
45

5-
Deno.test("Lock", async (t) => {
6-
await t.step(
7-
"Processing over multiple event loops is not atomic",
8-
async () => {
9-
const count = new AsyncValue(0);
10-
const operation = async () => {
6+
test(
7+
"Lock Processing over multiple event loops is not atomic",
8+
async () => {
9+
const count = new AsyncValue(0);
10+
const operation = async () => {
11+
const v = await count.get();
12+
await count.set(v + 1);
13+
};
14+
await Promise.all([...Array(10)].map(() => operation()));
15+
assertEquals(await count.get(), 1);
16+
},
17+
);
18+
19+
test(
20+
"Lock Processing over multiple event loops is not atomic, but can be changed to atomic by using Lock",
21+
async () => {
22+
const count = new Lock(new AsyncValue(0));
23+
const operation = () => {
24+
return count.lock(async (count) => {
1125
const v = await count.get();
1226
await count.set(v + 1);
13-
};
14-
await Promise.all([...Array(10)].map(() => operation()));
15-
assertEquals(await count.get(), 1);
16-
},
17-
);
18-
19-
await t.step(
20-
"Processing over multiple event loops is not atomic, but can be changed to atomic by using Lock",
21-
async () => {
22-
const count = new Lock(new AsyncValue(0));
23-
const operation = () => {
24-
return count.lock(async (count) => {
25-
const v = await count.get();
26-
await count.set(v + 1);
27-
});
28-
};
29-
await Promise.all([...Array(10)].map(() => operation()));
30-
assertEquals(await count.lock((v) => v.get()), 10);
31-
},
32-
);
27+
});
28+
};
29+
await Promise.all([...Array(10)].map(() => operation()));
30+
assertEquals(await count.lock((v) => v.get()), 10);
31+
},
32+
);
3333

34-
await t.step(
35-
"'lock' should allow only one operation at a time",
36-
async () => {
37-
let noperations = 0;
38-
const results: number[] = [];
39-
const count = new Lock(new AsyncValue(0));
40-
const operation = () => {
41-
return count.lock(async (count) => {
42-
noperations += 1;
43-
results.push(noperations);
44-
const v = await count.get();
45-
await count.set(v + 1);
46-
noperations -= 1;
47-
});
48-
};
49-
await Promise.all([...Array(10)].map(() => operation()));
50-
assertEquals(noperations, 0);
51-
assertEquals(results, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
52-
},
53-
);
54-
});
34+
test(
35+
"Lock 'lock' should allow only one operation at a time",
36+
async () => {
37+
let noperations = 0;
38+
const results: number[] = [];
39+
const count = new Lock(new AsyncValue(0));
40+
const operation = () => {
41+
return count.lock(async (count) => {
42+
noperations += 1;
43+
results.push(noperations);
44+
const v = await count.get();
45+
await count.set(v + 1);
46+
noperations -= 1;
47+
});
48+
};
49+
await Promise.all([...Array(10)].map(() => operation()));
50+
assertEquals(noperations, 0);
51+
assertEquals(results, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
52+
},
53+
);

0 commit comments

Comments
 (0)