2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-08-14 16:50:06 -04:00
|
|
|
import { assertEquals, DENO_FUTURE } from "./test_util.ts";
|
2021-08-24 09:21:31 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2024-08-14 16:50:06 -04:00
|
|
|
{ ignore: DENO_FUTURE, permissions: { read: true, run: true, hrtime: true } },
|
2021-08-24 09:21:31 -04:00
|
|
|
async function flockFileSync() {
|
2021-09-17 09:02:23 -04:00
|
|
|
await runFlockTests({ sync: true });
|
2021-08-24 09:21:31 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2024-08-14 16:50:06 -04:00
|
|
|
{ ignore: DENO_FUTURE, permissions: { read: true, run: true, hrtime: true } },
|
2021-08-24 09:21:31 -04:00
|
|
|
async function flockFileAsync() {
|
2021-09-17 09:02:23 -04:00
|
|
|
await runFlockTests({ sync: false });
|
2021-08-24 09:21:31 -04:00
|
|
|
},
|
|
|
|
);
|
2021-09-17 09:02:23 -04:00
|
|
|
|
|
|
|
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,
|
2021-09-30 15:45:13 -04:00
|
|
|
// 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,
|
2021-09-17 09:02:23 -04:00
|
|
|
}),
|
|
|
|
false,
|
|
|
|
"shared does not block shared",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkFirstBlocksSecond(opts: {
|
|
|
|
firstExclusive: boolean;
|
|
|
|
secondExclusive: boolean;
|
|
|
|
sync: boolean;
|
2021-09-30 15:45:13 -04:00
|
|
|
waitBothEnteredLock?: boolean;
|
2021-09-17 09:02:23 -04:00
|
|
|
}) {
|
|
|
|
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));
|
|
|
|
|
2021-09-30 15:45:13 -04:00
|
|
|
await Promise.all([
|
|
|
|
firstProcess.waitStartup(),
|
|
|
|
secondProcess.waitStartup(),
|
|
|
|
]);
|
2021-09-17 09:02:23 -04:00
|
|
|
|
2021-09-30 15:45:13 -04:00
|
|
|
await firstProcess.enterLock();
|
|
|
|
await firstProcess.waitEnterLock();
|
|
|
|
|
|
|
|
await secondProcess.enterLock();
|
2021-09-17 14:17:01 -04:00
|
|
|
await sleep(100);
|
2021-09-30 15:45:13 -04:00
|
|
|
|
|
|
|
if (!opts.waitBothEnteredLock) {
|
|
|
|
await firstProcess.exitLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
await secondProcess.waitEnterLock();
|
|
|
|
|
|
|
|
if (opts.waitBothEnteredLock) {
|
|
|
|
await firstProcess.exitLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
await secondProcess.exitLock();
|
|
|
|
|
|
|
|
// collect the final output
|
2021-09-17 14:17:01 -04:00
|
|
|
const firstPsTimes = await firstProcess.getTimes();
|
2021-09-17 09:02:23 -04:00
|
|
|
const secondPsTimes = await secondProcess.getTimes();
|
|
|
|
return firstPsTimes.exitTime < secondPsTimes.enterTime;
|
|
|
|
} finally {
|
2022-05-18 16:00:11 -04:00
|
|
|
await firstProcess.close();
|
|
|
|
await secondProcess.close();
|
2021-09-17 09:02:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function runFlockTestProcess(opts: { exclusive: boolean; sync: boolean }) {
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
const path = "tests/testdata/assets/lock_target.txt";
|
2021-09-17 09:02:23 -04:00
|
|
|
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
|
2021-09-17 14:17:01 -04:00
|
|
|
// occur at the same time as this exit time
|
2021-09-17 09:02:23 -04:00
|
|
|
const exitTime = new Date().getTime();
|
2022-06-13 15:28:00 -04:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 100));
|
2021-09-17 09:02:23 -04:00
|
|
|
|
|
|
|
// release the lock
|
|
|
|
${opts.sync ? "Deno.funlockSync(rid);" : "await Deno.funlock(rid);"}
|
|
|
|
|
2021-09-30 15:45:13 -04:00
|
|
|
// exited signal
|
|
|
|
Deno.stdout.writeSync(new Uint8Array(1));
|
|
|
|
|
2021-09-17 09:02:23 -04:00
|
|
|
// output the enter and exit time
|
|
|
|
console.log(JSON.stringify({ enterTime, exitTime }));
|
|
|
|
`;
|
|
|
|
|
2022-12-02 08:43:17 -05:00
|
|
|
const process = new Deno.Command(Deno.execPath(), {
|
2022-05-18 16:00:11 -04:00
|
|
|
args: ["eval", "--unstable", scriptText],
|
2021-09-17 09:02:23 -04:00
|
|
|
stdin: "piped",
|
2022-12-12 23:12:19 -05:00
|
|
|
stdout: "piped",
|
|
|
|
stderr: "null",
|
2022-12-02 08:43:17 -05:00
|
|
|
}).spawn();
|
2021-09-17 09:02:23 -04:00
|
|
|
|
2022-05-18 16:00:11 -04:00
|
|
|
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();
|
|
|
|
};
|
2021-09-30 15:45:13 -04:00
|
|
|
|
2021-09-17 09:02:23 -04:00
|
|
|
return {
|
2021-09-30 15:45:13 -04:00
|
|
|
async waitStartup() {
|
|
|
|
await waitSignal();
|
|
|
|
},
|
|
|
|
async enterLock() {
|
|
|
|
await signal();
|
|
|
|
await waitSignal(); // entering signal
|
|
|
|
},
|
|
|
|
async waitEnterLock() {
|
|
|
|
await waitSignal();
|
|
|
|
},
|
|
|
|
async exitLock() {
|
|
|
|
await signal();
|
|
|
|
await waitSignal();
|
|
|
|
},
|
2021-09-17 09:02:23 -04:00
|
|
|
getTimes: async () => {
|
2022-05-18 16:00:11 -04:00
|
|
|
const { stdout } = await process.output();
|
|
|
|
const text = new TextDecoder().decode(stdout);
|
2021-09-17 09:02:23 -04:00
|
|
|
return JSON.parse(text) as {
|
|
|
|
enterTime: number;
|
|
|
|
exitTime: number;
|
|
|
|
};
|
|
|
|
},
|
2022-05-18 16:00:11 -04:00
|
|
|
close: async () => {
|
|
|
|
await process.status;
|
|
|
|
await process.stdin.close();
|
2021-09-17 09:02:23 -04:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|