summaryrefslogtreecommitdiff
path: root/std/signal/test.ts
diff options
context:
space:
mode:
authorChris Knight <cknight1234@gmail.com>2020-04-10 15:05:56 +0100
committerGitHub <noreply@github.com>2020-04-10 10:05:56 -0400
commit5bf1e4de3b394eef203be3c393db57538e748be1 (patch)
treee45ab50e56bc07512c2071dead3b2dda9716c030 /std/signal/test.ts
parent02bc58d83253fd3be61787bb28b6b02e3aa71092 (diff)
feat(std/signal): add utility for listening to signal events (#4696)
Diffstat (limited to 'std/signal/test.ts')
-rw-r--r--std/signal/test.ts36
1 files changed, 35 insertions, 1 deletions
diff --git a/std/signal/test.ts b/std/signal/test.ts
index 16d1458a2..e36a697a5 100644
--- a/std/signal/test.ts
+++ b/std/signal/test.ts
@@ -1,7 +1,7 @@
const { test } = Deno;
import { assertEquals, assertThrows } from "../testing/asserts.ts";
import { delay } from "../util/async.ts";
-import { signal } from "./mod.ts";
+import { signal, onSignal } from "./mod.ts";
if (Deno.build.os !== "win") {
test("signal() throws when called with empty signals", (): void => {
@@ -58,4 +58,38 @@ if (Deno.build.os !== "win") {
await delay(10);
},
});
+
+ test({
+ name: "onSignal() registers and disposes of event handler",
+ 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);
+ // Clear timeout clears interval, but interval promise is not
+ // yet resolved, delay to next turn of event loop otherwise,
+ // we'll be leaking resources.
+ await delay(10);
+ },
+ });
}