2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-01-24 08:15:31 -05:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2020-03-03 12:22:53 -05:00
|
|
|
assertThrows,
|
2020-11-26 11:22:36 -05:00
|
|
|
deferred,
|
2021-06-24 21:44:14 -04:00
|
|
|
delay,
|
2020-09-27 06:22:32 -04:00
|
|
|
unitTest,
|
2020-01-24 08:15:31 -05:00
|
|
|
} from "./test_util.ts";
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2020-04-28 12:35:23 -04:00
|
|
|
{ ignore: Deno.build.os !== "windows" },
|
2021-08-05 07:08:58 -04:00
|
|
|
function signalsNotImplemented() {
|
2020-01-24 08:15:31 -05:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.signal("SIGINT");
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"not implemented",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.signal("SIGALRM");
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"not implemented",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.signal("SIGCHLD");
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"not implemented",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.signal("SIGHUP");
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"not implemented",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.signal("SIGINT");
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"not implemented",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.signal("SIGIO");
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"not implemented",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.signal("SIGPIPE");
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"not implemented",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.signal("SIGQUIT");
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"not implemented",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.signal("SIGTERM");
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"not implemented",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.signal("SIGUSR1");
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"not implemented",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.signal("SIGUSR2");
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"not implemented",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.signal("SIGWINCH");
|
2020-01-24 08:15:31 -05:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"not implemented",
|
2020-01-24 08:15:31 -05:00
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { run: true, net: true },
|
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function signalStreamTest() {
|
2020-11-26 11:22:36 -05:00
|
|
|
const resolvable = deferred();
|
2020-01-24 08:15:31 -05:00
|
|
|
// This prevents the program from exiting.
|
|
|
|
const t = setInterval(() => {}, 1000);
|
|
|
|
|
|
|
|
let c = 0;
|
2021-09-06 10:05:33 -04:00
|
|
|
const sig = Deno.signal("SIGUSR1");
|
2020-01-24 08:15:31 -05:00
|
|
|
setTimeout(async () => {
|
2021-06-24 21:44:14 -04:00
|
|
|
await delay(20);
|
2020-01-24 08:15:31 -05:00
|
|
|
for (const _ of Array(3)) {
|
|
|
|
// Sends SIGUSR1 3 times.
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.kill(Deno.pid, "SIGUSR1");
|
2021-06-24 21:44:14 -04:00
|
|
|
await delay(20);
|
2020-01-24 08:15:31 -05:00
|
|
|
}
|
|
|
|
sig.dispose();
|
2020-03-03 12:22:53 -05:00
|
|
|
resolvable.resolve();
|
2020-01-24 08:15:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
for await (const _ of sig) {
|
|
|
|
c += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(c, 3);
|
|
|
|
|
2020-03-03 12:22:53 -05:00
|
|
|
clearInterval(t);
|
|
|
|
await resolvable;
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-01-24 08:15:31 -05:00
|
|
|
|
2021-06-25 00:15:35 -04:00
|
|
|
// This tests that pending op_signal_poll doesn't block the runtime from exiting the process.
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
permissions: { run: true, read: true },
|
|
|
|
},
|
2021-08-05 07:08:58 -04:00
|
|
|
async function signalStreamExitTest() {
|
2021-06-25 00:15:35 -04:00
|
|
|
const p = Deno.run({
|
|
|
|
cmd: [
|
|
|
|
Deno.execPath(),
|
|
|
|
"eval",
|
|
|
|
"--unstable",
|
2021-09-06 10:05:33 -04:00
|
|
|
"(async () => { for await (const _ of Deno.signal('SIGIO')) {} })()",
|
2021-06-25 00:15:35 -04:00
|
|
|
],
|
|
|
|
});
|
|
|
|
const res = await p.status();
|
|
|
|
assertEquals(res.code, 0);
|
|
|
|
p.close();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", permissions: { run: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function signalPromiseTest() {
|
2020-11-26 11:22:36 -05:00
|
|
|
const resolvable = deferred();
|
2020-03-03 12:22:53 -05:00
|
|
|
// This prevents the program from exiting.
|
|
|
|
const t = setInterval(() => {}, 1000);
|
2020-01-24 08:15:31 -05:00
|
|
|
|
2021-09-06 10:05:33 -04:00
|
|
|
const sig = Deno.signal("SIGUSR1");
|
2020-03-03 12:22:53 -05:00
|
|
|
setTimeout(() => {
|
2021-09-06 10:05:33 -04:00
|
|
|
Deno.kill(Deno.pid, "SIGUSR1");
|
2020-03-03 12:22:53 -05:00
|
|
|
resolvable.resolve();
|
|
|
|
}, 20);
|
|
|
|
await sig;
|
|
|
|
sig.dispose();
|
2020-01-24 08:15:31 -05:00
|
|
|
|
2020-03-03 12:22:53 -05:00
|
|
|
clearInterval(t);
|
|
|
|
await resolvable;
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2020-01-24 08:15:31 -05:00
|
|
|
|
2021-06-22 02:17:44 -04:00
|
|
|
// https://github.com/denoland/deno/issues/9806
|
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", permissions: { run: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
async function signalPromiseTest2() {
|
2021-06-22 02:17:44 -04:00
|
|
|
const resolvable = deferred();
|
|
|
|
// This prevents the program from exiting.
|
|
|
|
const t = setInterval(() => {}, 1000);
|
|
|
|
|
|
|
|
let called = false;
|
2021-09-06 10:05:33 -04:00
|
|
|
const sig = Deno.signal("SIGUSR1");
|
2021-06-22 02:17:44 -04:00
|
|
|
sig.then(() => {
|
|
|
|
called = true;
|
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
sig.dispose();
|
|
|
|
setTimeout(() => {
|
|
|
|
resolvable.resolve();
|
|
|
|
}, 10);
|
|
|
|
}, 10);
|
|
|
|
|
|
|
|
clearInterval(t);
|
|
|
|
await resolvable;
|
|
|
|
|
|
|
|
// Promise callback is not called because it didn't get
|
|
|
|
// the corresponding signal.
|
|
|
|
assert(!called);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
2021-09-22 19:50:50 -04:00
|
|
|
{ ignore: Deno.build.os === "windows", permissions: { run: true } },
|
2021-08-05 07:08:58 -04:00
|
|
|
function signalShorthandsTest() {
|
2020-01-24 08:15:31 -05:00
|
|
|
let s: Deno.SignalStream;
|
2021-09-06 10:05:33 -04:00
|
|
|
s = Deno.signal("SIGALRM");
|
2020-01-24 08:15:31 -05:00
|
|
|
assert(s instanceof Deno.SignalStream);
|
|
|
|
s.dispose();
|
2021-09-06 10:05:33 -04:00
|
|
|
s = Deno.signal("SIGCHLD");
|
2020-01-24 08:15:31 -05:00
|
|
|
assert(s instanceof Deno.SignalStream);
|
|
|
|
s.dispose();
|
2021-09-06 10:05:33 -04:00
|
|
|
s = Deno.signal("SIGHUP");
|
2020-01-24 08:15:31 -05:00
|
|
|
assert(s instanceof Deno.SignalStream);
|
|
|
|
s.dispose();
|
2021-09-06 10:05:33 -04:00
|
|
|
s = Deno.signal("SIGINT");
|
2020-01-24 08:15:31 -05:00
|
|
|
assert(s instanceof Deno.SignalStream);
|
|
|
|
s.dispose();
|
2021-09-06 10:05:33 -04:00
|
|
|
s = Deno.signal("SIGIO");
|
2020-01-24 08:15:31 -05:00
|
|
|
assert(s instanceof Deno.SignalStream);
|
|
|
|
s.dispose();
|
2021-09-06 10:05:33 -04:00
|
|
|
s = Deno.signal("SIGPIPE");
|
2020-01-24 08:15:31 -05:00
|
|
|
assert(s instanceof Deno.SignalStream);
|
|
|
|
s.dispose();
|
2021-09-06 10:05:33 -04:00
|
|
|
s = Deno.signal("SIGQUIT");
|
2020-01-24 08:15:31 -05:00
|
|
|
assert(s instanceof Deno.SignalStream);
|
|
|
|
s.dispose();
|
2021-09-06 10:05:33 -04:00
|
|
|
s = Deno.signal("SIGTERM");
|
2020-01-24 08:15:31 -05:00
|
|
|
assert(s instanceof Deno.SignalStream);
|
|
|
|
s.dispose();
|
2021-09-06 10:05:33 -04:00
|
|
|
s = Deno.signal("SIGUSR1");
|
2020-01-24 08:15:31 -05:00
|
|
|
assert(s instanceof Deno.SignalStream);
|
|
|
|
s.dispose();
|
2021-09-06 10:05:33 -04:00
|
|
|
s = Deno.signal("SIGUSR2");
|
2020-01-24 08:15:31 -05:00
|
|
|
assert(s instanceof Deno.SignalStream);
|
|
|
|
s.dispose();
|
2021-09-06 10:05:33 -04:00
|
|
|
s = Deno.signal("SIGWINCH");
|
2020-01-24 08:15:31 -05:00
|
|
|
assert(s instanceof Deno.SignalStream);
|
|
|
|
s.dispose();
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|