-
Notifications
You must be signed in to change notification settings - Fork 1
/
mutex_test.ts
42 lines (41 loc) · 987 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
33
34
35
36
37
38
39
40
41
42
import { assert } from "./test_deps.ts";
import { delay } from "./test_deps.ts";
import { Mutex } from "./mod.ts";
Deno.test("tasks do not overlap", async () => {
var m = new Mutex();
var task1running = false;
var task2running = false;
var task1ran = false;
var task2ran = false;
await Promise.all([
m.acquire()
.then((release) => {
task1running = true;
task1ran = true;
return delay(10)
.then(() => {
assert(!task2running);
task1running = false;
release();
});
}),
m.acquire()
.then((release) => {
assert(!task1running);
task2running = true;
task2ran = true;
return delay(10)
.then(() => {
task2running = false;
release();
});
}),
])
.then(() => {
assert(!task1running);
assert(!task2running);
assert(task1ran);
assert(task2ran);
})
.catch();
});