1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-02 09:34:19 -04:00
denoland-deno/std/signal/test.ts
Nayeem Rahman f184332c09
BREAKING(std): reorganization (#5087)
* Prepend underscores to private modules
* Remove collectUint8Arrays() It would be a misuse of Deno.iter()'s result.
* Move std/_util/async.ts to std/async
* Move std/util/sha*.ts to std/hash
2020-05-09 08:34:47 -04:00

91 lines
2.2 KiB
TypeScript

const { test } = Deno;
import { assertEquals, assertThrows } from "../testing/asserts.ts";
import { delay } from "../async/delay.ts";
import { signal, onSignal } from "./mod.ts";
test({
name: "signal() throws when called with empty signals",
ignore: Deno.build.os === "windows",
fn() {
assertThrows(
() => {
// @ts-ignore
signal();
},
Error,
"No signals are given. You need to specify at least one signal to create a signal stream."
);
},
});
test({
name: "signal() iterates for multiple signals",
ignore: Deno.build.os === "windows",
fn: async (): Promise<void> => {
// This prevents the program from exiting.
const t = setInterval(() => {}, 1000);
let c = 0;
const sig = signal(
Deno.Signal.SIGUSR1,
Deno.Signal.SIGUSR2,
Deno.Signal.SIGINT
);
setTimeout(async () => {
await delay(20);
Deno.kill(Deno.pid, Deno.Signal.SIGINT);
await delay(20);
Deno.kill(Deno.pid, Deno.Signal.SIGUSR2);
await delay(20);
Deno.kill(Deno.pid, Deno.Signal.SIGUSR1);
await delay(20);
Deno.kill(Deno.pid, Deno.Signal.SIGUSR2);
await delay(20);
Deno.kill(Deno.pid, Deno.Signal.SIGUSR1);
await delay(20);
Deno.kill(Deno.pid, Deno.Signal.SIGINT);
await delay(20);
sig.dispose();
});
for await (const _ of sig) {
c += 1;
}
assertEquals(c, 6);
clearTimeout(t);
},
});
test({
name: "onSignal() registers and disposes of event handler",
ignore: Deno.build.os === "windows",
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);
clearTimeout(t);
},
});