-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rw_lock_test.ts
128 lines (122 loc) · 3.62 KB
/
rw_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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { test } from "@cross/test";
import { assertEquals } from "@std/assert";
import { flushPromises } from "./flush_promises.ts";
import { peekPromiseState } from "./peek_promise_state.ts";
import { AsyncValue } from "./async_value.ts";
import { RwLock } from "./rw_lock.ts";
test(
"RwLock 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(
"RwLock Processing over multiple event loops is not atomic, but can be changed to atomic by using RwLock",
async () => {
const count = new RwLock(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(
"RwLock 'lock' should allow only one writer at a time",
async () => {
let nwriters = 0;
const results: number[] = [];
const count = new RwLock(new AsyncValue(0));
const writer = () => {
return count.lock(async (count) => {
nwriters += 1;
results.push(nwriters);
await count.set(await count.get() + 1);
nwriters -= 1;
});
};
await Promise.all([...Array(10)].map(() => writer()));
assertEquals(nwriters, 0);
assertEquals(results, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
},
);
test(
"RwLock 'rlock' should allow multiple readers at a time",
async () => {
let nreaders = 0;
const results: number[] = [];
const count = new RwLock(new AsyncValue(0));
const reader = () => {
return count.rlock(async (count) => {
nreaders += 1;
results.push(nreaders);
assertEquals(await count.get(), 0);
nreaders -= 1;
});
};
await Promise.all([...Array(10)].map(() => reader()));
assertEquals(nreaders, 0);
assertEquals(results, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
},
);
test(
"RwLock 'lock' should block until all readers are done",
async () => {
const count = new RwLock(new AsyncValue(0));
const { promise, resolve } = Promise.withResolvers<void>();
const writer = () => {
return count.lock(() => {
// Do nothing
});
};
const reader = () => {
return count.rlock(async () => {
await promise;
});
};
const r = reader();
const w = writer();
await flushPromises();
assertEquals(await peekPromiseState(r), "pending");
assertEquals(await peekPromiseState(w), "pending");
resolve();
await flushPromises();
assertEquals(await peekPromiseState(r), "fulfilled");
assertEquals(await peekPromiseState(w), "fulfilled");
},
);
test(
"RwLock 'rlock' should block until all writers are done",
async () => {
const count = new RwLock(new AsyncValue(0));
const { promise, resolve } = Promise.withResolvers<void>();
const writer = () => {
return count.lock(async () => {
await promise;
});
};
const reader = () => {
return count.rlock(() => {
// Do nothing
});
};
const w = writer();
const r = reader();
await flushPromises();
assertEquals(await peekPromiseState(w), "pending");
assertEquals(await peekPromiseState(r), "pending");
resolve();
await flushPromises();
assertEquals(await peekPromiseState(w), "fulfilled");
assertEquals(await peekPromiseState(r), "fulfilled");
},
);