-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutex_test.ts
32 lines (30 loc) · 957 Bytes
/
mutex_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
import { test } from "@cross/test";
import { assertEquals } from "@std/assert";
import { AsyncValue } from "./async_value.ts";
import { Mutex } from "./mutex.ts";
test(
"Mutex 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(
"Mutex Processing over multiple event loops is not atomic, but can be changed to atomic by using Mutex",
async () => {
const mu = new Mutex();
const count = new AsyncValue(0);
const operation = async () => {
using _lock = await mu.acquire();
const v = await count.get();
await count.set(v + 1);
};
await Promise.all([...Array(10)].map(() => operation()));
assertEquals(await count.get(), 10);
},
);