-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_test.ts
53 lines (50 loc) · 1.52 KB
/
lock_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { test } from "@cross/test";
import { assertEquals } from "@std/assert";
import { AsyncValue } from "./async_value.ts";
import { Lock } from "./lock.ts";
test(
"Lock Processing over multiple event loops is not atomic",
async () => {
const count = new AsyncValue(0);
const operation = async () => {
const v = await count.get();
await count.set(v + 1);
};
await Promise.all([...Array(10)].map(() => operation()));
assertEquals(await count.get(), 1);
},
);
test(
"Lock Processing over multiple event loops is not atomic, but can be changed to atomic by using Lock",
async () => {
const count = new Lock(new AsyncValue(0));
const operation = () => {
return count.lock(async (count) => {
const v = await count.get();
await count.set(v + 1);
});
};
await Promise.all([...Array(10)].map(() => operation()));
assertEquals(await count.lock((v) => v.get()), 10);
},
);
test(
"Lock 'lock' should allow only one operation at a time",
async () => {
let noperations = 0;
const results: number[] = [];
const count = new Lock(new AsyncValue(0));
const operation = () => {
return count.lock(async (count) => {
noperations += 1;
results.push(noperations);
const v = await count.get();
await count.set(v + 1);
noperations -= 1;
});
};
await Promise.all([...Array(10)].map(() => operation()));
assertEquals(noperations, 0);
assertEquals(results, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
},
);