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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "./test_util.ts";
Deno.test(
{ permissions: { read: true, run: true, hrtime: true } },
async function flockFileSync() {
await runFlockTests({ sync: true });
},
);
Deno.test(
{ permissions: { read: true, run: true, hrtime: true } },
async function flockFileAsync() {
await runFlockTests({ sync: false });
},
);
async function runFlockTests(opts: { sync: boolean }) {
assertEquals(
await checkFirstBlocksSecond({
firstExclusive: true,
secondExclusive: false,
sync: opts.sync,
}),
true,
"exclusive blocks shared",
);
assertEquals(
await checkFirstBlocksSecond({
firstExclusive: false,
secondExclusive: true,
sync: opts.sync,
}),
true,
"shared blocks exclusive",
);
assertEquals(
await checkFirstBlocksSecond({
firstExclusive: true,
secondExclusive: true,
sync: opts.sync,
}),
true,
"exclusive blocks exclusive",
);
assertEquals(
await checkFirstBlocksSecond({
firstExclusive: false,
secondExclusive: false,
sync: opts.sync,
// need to wait for both to enter the lock to prevent the case where the
// first process enters and exits the lock before the second even enters
waitBothEnteredLock: true,
}),
false,
"shared does not block shared",
);
}
async function checkFirstBlocksSecond(opts: {
firstExclusive: boolean;
secondExclusive: boolean;
sync: boolean;
waitBothEnteredLock?: boolean;
}) {
const firstProcess = runFlockTestProcess({
exclusive: opts.firstExclusive,
sync: opts.sync,
});
const secondProcess = runFlockTestProcess({
exclusive: opts.secondExclusive,
sync: opts.sync,
});
try {
const sleep = (time: number) => new Promise((r) => setTimeout(r, time));
await Promise.all([
firstProcess.waitStartup(),
secondProcess.waitStartup(),
]);
await firstProcess.enterLock();
await firstProcess.waitEnterLock();
await secondProcess.enterLock();
await sleep(100);
if (!opts.waitBothEnteredLock) {
await firstProcess.exitLock();
}
await secondProcess.waitEnterLock();
if (opts.waitBothEnteredLock) {
await firstProcess.exitLock();
}
await secondProcess.exitLock();
// collect the final output
const firstPsTimes = await firstProcess.getTimes();
const secondPsTimes = await secondProcess.getTimes();
return firstPsTimes.exitTime < secondPsTimes.enterTime;
} finally {
await firstProcess.close();
await secondProcess.close();
}
}
function runFlockTestProcess(opts: { exclusive: boolean; sync: boolean }) {
const path = "cli/tests/testdata/assets/fixture.json";
const scriptText = `
const { rid } = Deno.openSync("${path}");
// ready signal
Deno.stdout.writeSync(new Uint8Array(1));
// wait for enter lock signal
Deno.stdin.readSync(new Uint8Array(1));
// entering signal
Deno.stdout.writeSync(new Uint8Array(1));
// lock and record the entry time
${
opts.sync
? `Deno.flockSync(rid, ${opts.exclusive ? "true" : "false"});`
: `await Deno.flock(rid, ${opts.exclusive ? "true" : "false"});`
}
const enterTime = new Date().getTime();
// entered signal
Deno.stdout.writeSync(new Uint8Array(1));
// wait for exit lock signal
Deno.stdin.readSync(new Uint8Array(1));
// record the exit time and wait a little bit before releasing
// the lock so that the enter time of the next process doesn't
// occur at the same time as this exit time
const exitTime = new Date().getTime();
await new Promise(resolve => setTimeout(resolve, 100));
// release the lock
${opts.sync ? "Deno.funlockSync(rid);" : "await Deno.funlock(rid);"}
// exited signal
Deno.stdout.writeSync(new Uint8Array(1));
// output the enter and exit time
console.log(JSON.stringify({ enterTime, exitTime }));
`;
const process = new Deno.Command(Deno.execPath(), {
args: ["eval", "--unstable", scriptText],
stdin: "piped",
stdout: "piped",
stderr: "null",
}).spawn();
const waitSignal = async () => {
const reader = process.stdout.getReader({ mode: "byob" });
await reader.read(new Uint8Array(1));
reader.releaseLock();
};
const signal = async () => {
const writer = process.stdin.getWriter();
await writer.write(new Uint8Array(1));
writer.releaseLock();
};
return {
async waitStartup() {
await waitSignal();
},
async enterLock() {
await signal();
await waitSignal(); // entering signal
},
async waitEnterLock() {
await waitSignal();
},
async exitLock() {
await signal();
await waitSignal();
},
getTimes: async () => {
const { stdout } = await process.output();
const text = new TextDecoder().decode(stdout);
return JSON.parse(text) as {
enterTime: number;
exitTime: number;
};
},
close: async () => {
await process.status;
await process.stdin.close();
},
};
}
|