2020-09-21 08:26:41 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-02-11 11:24:27 -05:00
|
|
|
import { assertEquals, assertThrows } from "../testing/asserts.ts";
|
2020-05-09 08:34:47 -04:00
|
|
|
import { delay } from "../async/delay.ts";
|
2020-09-27 06:22:32 -04:00
|
|
|
import { onSignal, signal } from "./mod.ts";
|
2020-02-07 01:53:15 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-04-10 22:03:41 -04:00
|
|
|
name: "signal() throws when called with empty signals",
|
2020-04-28 12:35:23 -04:00
|
|
|
ignore: Deno.build.os === "windows",
|
2020-04-10 22:03:41 -04:00
|
|
|
fn() {
|
2020-02-07 01:53:15 -05:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
2020-06-02 00:24:44 -04:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
(signal as any)();
|
2020-02-07 01:53:15 -05:00
|
|
|
},
|
|
|
|
Error,
|
2020-07-14 15:24:17 -04:00
|
|
|
"No signals are given. You need to specify at least one signal to create a signal stream.",
|
2020-02-07 01:53:15 -05:00
|
|
|
);
|
2020-04-10 22:03:41 -04:00
|
|
|
},
|
|
|
|
});
|
2020-02-07 01:53:15 -05:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-04-10 22:03:41 -04:00
|
|
|
name: "signal() iterates for multiple signals",
|
2020-04-28 12:35:23 -04:00
|
|
|
ignore: Deno.build.os === "windows",
|
2020-04-10 22:03:41 -04:00
|
|
|
fn: async (): Promise<void> => {
|
|
|
|
// This prevents the program from exiting.
|
|
|
|
const t = setInterval(() => {}, 1000);
|
2020-02-07 01:53:15 -05:00
|
|
|
|
2020-04-10 22:03:41 -04:00
|
|
|
let c = 0;
|
|
|
|
const sig = signal(
|
|
|
|
Deno.Signal.SIGUSR1,
|
|
|
|
Deno.Signal.SIGUSR2,
|
2020-07-14 15:24:17 -04:00
|
|
|
Deno.Signal.SIGINT,
|
2020-04-10 22:03:41 -04:00
|
|
|
);
|
2020-04-10 10:05:56 -04:00
|
|
|
|
2020-04-10 22:03:41 -04:00
|
|
|
setTimeout(async () => {
|
2020-04-10 10:05:56 -04:00
|
|
|
await delay(20);
|
|
|
|
Deno.kill(Deno.pid, Deno.Signal.SIGINT);
|
|
|
|
await delay(20);
|
|
|
|
Deno.kill(Deno.pid, Deno.Signal.SIGUSR2);
|
|
|
|
await delay(20);
|
2020-04-10 22:03:41 -04:00
|
|
|
Deno.kill(Deno.pid, Deno.Signal.SIGUSR1);
|
|
|
|
await delay(20);
|
|
|
|
Deno.kill(Deno.pid, Deno.Signal.SIGUSR2);
|
2020-04-10 10:05:56 -04:00
|
|
|
await delay(20);
|
|
|
|
Deno.kill(Deno.pid, Deno.Signal.SIGUSR1);
|
|
|
|
await delay(20);
|
|
|
|
Deno.kill(Deno.pid, Deno.Signal.SIGINT);
|
|
|
|
await delay(20);
|
2020-04-10 22:03:41 -04:00
|
|
|
sig.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
for await (const _ of sig) {
|
|
|
|
c += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
assertEquals(c, 6);
|
|
|
|
|
|
|
|
clearTimeout(t);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-04-10 22:03:41 -04:00
|
|
|
name: "onSignal() registers and disposes of event handler",
|
2020-04-28 12:35:23 -04:00
|
|
|
ignore: Deno.build.os === "windows",
|
2020-04-10 22:03:41 -04:00
|
|
|
async fn() {
|
|
|
|
// This prevents the program from exiting.
|
|
|
|
const t = setInterval(() => {}, 1000);
|
|
|
|
|
|
|
|
let calledCount = 0;
|
|
|
|
const handle = onSignal(Deno.Signal.SIGINT, () => {
|
|
|
|
calledCount++;
|
|
|
|
});
|
|
|
|
|
|
|
|
await delay(20);
|
|
|
|
Deno.kill(Deno.pid, Deno.Signal.SIGINT);
|
|
|
|
await delay(20);
|
|
|
|
Deno.kill(Deno.pid, Deno.Signal.SIGINT);
|
|
|
|
await delay(20);
|
|
|
|
Deno.kill(Deno.pid, Deno.Signal.SIGUSR2);
|
|
|
|
await delay(20);
|
|
|
|
handle.dispose(); // stop monitoring SIGINT
|
|
|
|
await delay(20);
|
|
|
|
Deno.kill(Deno.pid, Deno.Signal.SIGUSR1);
|
|
|
|
await delay(20);
|
|
|
|
Deno.kill(Deno.pid, Deno.Signal.SIGINT);
|
|
|
|
await delay(20);
|
|
|
|
assertEquals(calledCount, 2);
|
2020-04-10 10:05:56 -04:00
|
|
|
|
2020-04-10 22:03:41 -04:00
|
|
|
clearTimeout(t);
|
|
|
|
},
|
|
|
|
});
|