summaryrefslogtreecommitdiff
path: root/std/signal/test.ts
blob: e36a697a53bf2e4b8dcd2d40e32178f1260c771b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const { test } = Deno;
import { assertEquals, assertThrows } from "../testing/asserts.ts";
import { delay } from "../util/async.ts";
import { signal, onSignal } from "./mod.ts";

if (Deno.build.os !== "win") {
  test("signal() throws when called with empty signals", (): void => {
    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",
    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);
      // 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);
    },
  });

  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);
    },
  });
}