1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-15 16:43:44 -05:00
denoland-deno/cli/tests/unit/signal_test.ts

240 lines
5.5 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertThrows, deferred, delay } from "./test_util.ts";
2020-01-24 08:15:31 -05:00
Deno.test(
{ ignore: Deno.build.os !== "windows" },
function signalsNotImplemented() {
2020-01-24 08:15:31 -05:00
assertThrows(
() => {
Deno.addSignalListener("SIGINT", () => {});
2020-01-24 08:15:31 -05:00
},
Error,
"not implemented",
2020-01-24 08:15:31 -05:00
);
assertThrows(
() => {
Deno.addSignalListener("SIGALRM", () => {});
2020-01-24 08:15:31 -05:00
},
Error,
"not implemented",
2020-01-24 08:15:31 -05:00
);
assertThrows(
() => {
Deno.addSignalListener("SIGCHLD", () => {});
2020-01-24 08:15:31 -05:00
},
Error,
"not implemented",
2020-01-24 08:15:31 -05:00
);
assertThrows(
() => {
Deno.addSignalListener("SIGHUP", () => {});
2020-01-24 08:15:31 -05:00
},
Error,
"not implemented",
2020-01-24 08:15:31 -05:00
);
assertThrows(
() => {
Deno.addSignalListener("SIGINT", () => {});
2020-01-24 08:15:31 -05:00
},
Error,
"not implemented",
2020-01-24 08:15:31 -05:00
);
assertThrows(
() => {
Deno.addSignalListener("SIGIO", () => {});
2020-01-24 08:15:31 -05:00
},
Error,
"not implemented",
2020-01-24 08:15:31 -05:00
);
assertThrows(
() => {
Deno.addSignalListener("SIGPIPE", () => {});
2020-01-24 08:15:31 -05:00
},
Error,
"not implemented",
2020-01-24 08:15:31 -05:00
);
assertThrows(
() => {
Deno.addSignalListener("SIGQUIT", () => {});
2020-01-24 08:15:31 -05:00
},
Error,
"not implemented",
2020-01-24 08:15:31 -05:00
);
assertThrows(
() => {
Deno.addSignalListener("SIGTERM", () => {});
2020-01-24 08:15:31 -05:00
},
Error,
"not implemented",
2020-01-24 08:15:31 -05:00
);
assertThrows(
() => {
Deno.addSignalListener("SIGUSR1", () => {});
2020-01-24 08:15:31 -05:00
},
Error,
"not implemented",
2020-01-24 08:15:31 -05:00
);
assertThrows(
() => {
Deno.addSignalListener("SIGUSR2", () => {});
2020-01-24 08:15:31 -05:00
},
Error,
"not implemented",
2020-01-24 08:15:31 -05:00
);
assertThrows(
() => {
Deno.addSignalListener("SIGWINCH", () => {});
2020-01-24 08:15:31 -05:00
},
Error,
"not implemented",
2020-01-24 08:15:31 -05:00
);
},
);
Deno.test(
{
ignore: Deno.build.os === "windows",
permissions: { run: true },
},
async function signalListenerTest() {
const resolvable = deferred();
2020-01-24 08:15:31 -05:00
let c = 0;
const listener = () => {
c += 1;
};
Deno.addSignalListener("SIGUSR1", listener);
2020-01-24 08:15:31 -05:00
setTimeout(async () => {
// Sends SIGUSR1 3 times.
2020-01-24 08:15:31 -05:00
for (const _ of Array(3)) {
await delay(20);
Deno.kill(Deno.pid, "SIGUSR1");
2020-01-24 08:15:31 -05:00
}
await delay(20);
Deno.removeSignalListener("SIGUSR1", listener);
resolvable.resolve();
2020-01-24 08:15:31 -05:00
});
await resolvable;
2020-01-24 08:15:31 -05:00
assertEquals(c, 3);
},
);
Deno.test(
{
ignore: Deno.build.os === "windows",
permissions: { run: true },
},
async function multipleSignalListenerTest() {
const resolvable = deferred();
let c = "";
const listener0 = () => {
c += "0";
};
const listener1 = () => {
c += "1";
};
Deno.addSignalListener("SIGUSR2", listener0);
Deno.addSignalListener("SIGUSR2", listener1);
setTimeout(async () => {
// Sends SIGUSR2 3 times.
for (const _ of Array(3)) {
await delay(20);
Deno.kill(Deno.pid, "SIGUSR2");
}
await delay(20);
Deno.removeSignalListener("SIGUSR2", listener1);
// Sends SIGUSR2 3 times.
for (const _ of Array(3)) {
await delay(20);
Deno.kill(Deno.pid, "SIGUSR2");
}
await delay(20);
// Sends SIGUSR1 (irrelevant signal) 3 times.
for (const _ of Array(3)) {
await delay(20);
Deno.kill(Deno.pid, "SIGUSR1");
}
await delay(20);
Deno.removeSignalListener("SIGUSR2", listener0);
resolvable.resolve();
});
2020-01-24 08:15:31 -05:00
await resolvable;
// The first 3 events are handled by both handlers
// The last 3 events are handled only by handler0
assertEquals(c, "010101000");
},
);
2020-01-24 08:15:31 -05:00
// This tests that pending op_signal_poll doesn't block the runtime from exiting the process.
Deno.test(
{
ignore: Deno.build.os === "windows",
permissions: { run: true, read: true },
},
async function canExitWhileListeningToSignal() {
const p = Deno.run({
cmd: [
Deno.execPath(),
"eval",
"--unstable",
"Deno.addSignalListener('SIGIO', () => {})",
],
});
const res = await p.status();
assertEquals(res.code, 0);
p.close();
},
);
Deno.test(
{
ignore: Deno.build.os === "windows",
permissions: { run: true },
},
function signalInvalidHandlerTest() {
assertThrows(() => {
// deno-lint-ignore no-explicit-any
Deno.addSignalListener("SIGINT", "handler" as any);
});
assertThrows(() => {
// deno-lint-ignore no-explicit-any
Deno.removeSignalListener("SIGINT", "handler" as any);
});
},
);
Deno.test(
{
ignore: Deno.build.os === "windows",
permissions: { run: true },
},
function signalForbiddenSignalTest() {
assertThrows(
() => Deno.addSignalListener("SIGKILL", () => {}),
TypeError,
"Binding to signal 'SIGKILL' is not allowed",
);
assertThrows(
() => Deno.addSignalListener("SIGSTOP", () => {}),
TypeError,
"Binding to signal 'SIGSTOP' is not allowed",
);
assertThrows(
() => Deno.addSignalListener("SIGILL", () => {}),
TypeError,
"Binding to signal 'SIGILL' is not allowed",
);
assertThrows(
() => Deno.addSignalListener("SIGFPE", () => {}),
TypeError,
"Binding to signal 'SIGFPE' is not allowed",
);
assertThrows(
() => Deno.addSignalListener("SIGSEGV", () => {}),
TypeError,
"Binding to signal 'SIGSEGV' is not allowed",
);
},
);