summaryrefslogtreecommitdiff
path: root/tests/unit/signal_test.ts
blob: 65b5ba78e12b0e062a6322398cd199aad2734206 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertThrows, delay } from "./test_util.ts";

Deno.test(
  { ignore: Deno.build.os !== "windows" },
  function signalsNotImplemented() {
    const msg =
      "Windows only supports ctrl-c (SIGINT) and ctrl-break (SIGBREAK).";
    assertThrows(
      () => {
        Deno.addSignalListener("SIGALRM", () => {});
      },
      Error,
      msg,
    );
    assertThrows(
      () => {
        Deno.addSignalListener("SIGCHLD", () => {});
      },
      Error,
      msg,
    );
    assertThrows(
      () => {
        Deno.addSignalListener("SIGHUP", () => {});
      },
      Error,
      msg,
    );
    assertThrows(
      () => {
        Deno.addSignalListener("SIGIO", () => {});
      },
      Error,
      msg,
    );
    assertThrows(
      () => {
        Deno.addSignalListener("SIGPIPE", () => {});
      },
      Error,
      msg,
    );
    assertThrows(
      () => {
        Deno.addSignalListener("SIGQUIT", () => {});
      },
      Error,
      msg,
    );
    assertThrows(
      () => {
        Deno.addSignalListener("SIGTERM", () => {});
      },
      Error,
      msg,
    );
    assertThrows(
      () => {
        Deno.addSignalListener("SIGUSR1", () => {});
      },
      Error,
      msg,
    );
    assertThrows(
      () => {
        Deno.addSignalListener("SIGUSR2", () => {});
      },
      Error,
      msg,
    );
    assertThrows(
      () => {
        Deno.addSignalListener("SIGWINCH", () => {});
      },
      Error,
      msg,
    );
    assertThrows(
      () => Deno.addSignalListener("SIGKILL", () => {}),
      Error,
      msg,
    );
    assertThrows(
      () => Deno.addSignalListener("SIGSTOP", () => {}),
      Error,
      msg,
    );
    assertThrows(
      () => Deno.addSignalListener("SIGILL", () => {}),
      Error,
      msg,
    );
    assertThrows(
      () => Deno.addSignalListener("SIGFPE", () => {}),
      Error,
      msg,
    );
    assertThrows(
      () => Deno.addSignalListener("SIGSEGV", () => {}),
      Error,
      msg,
    );
  },
);

Deno.test(
  {
    ignore: Deno.build.os === "windows",
    permissions: { run: true },
  },
  async function signalListenerTest() {
    let c = 0;
    const listener = () => {
      c += 1;
    };
    // This test needs to be careful that it doesn't accidentally aggregate multiple
    // signals into one. Sending two or more SIGxxx before the handler can be run will
    // result in signal coalescing.
    Deno.addSignalListener("SIGUSR1", listener);
    // Sends SIGUSR1 3 times.
    for (let i = 1; i <= 3; i++) {
      await delay(1);
      Deno.kill(Deno.pid, "SIGUSR1");
      while (c < i) {
        await delay(20);
      }
    }
    Deno.removeSignalListener("SIGUSR1", listener);
    await delay(100);
    assertEquals(c, 3);
  },
);

Deno.test(
  {
    ignore: Deno.build.os === "windows",
    permissions: { run: true },
  },
  async function multipleSignalListenerTest() {
    let c = "";
    const listener0 = () => {
      c += "0";
    };
    const listener1 = () => {
      c += "1";
    };
    // This test needs to be careful that it doesn't accidentally aggregate multiple
    // signals into one. Sending two or more SIGxxx before the handler can be run will
    // result in signal coalescing.
    Deno.addSignalListener("SIGUSR2", listener0);
    Deno.addSignalListener("SIGUSR2", listener1);

    // Sends SIGUSR2 3 times.
    for (let i = 1; i <= 3; i++) {
      await delay(1);
      Deno.kill(Deno.pid, "SIGUSR2");
      while (c.length < i * 2) {
        await delay(20);
      }
    }

    Deno.removeSignalListener("SIGUSR2", listener1);

    // Sends SIGUSR2 3 times.
    for (let i = 1; i <= 3; i++) {
      await delay(1);
      Deno.kill(Deno.pid, "SIGUSR2");
      while (c.length < 6 + i) {
        await delay(20);
      }
    }

    // Sends SIGUSR1 (irrelevant signal) 3 times.
    // By default SIGUSR1 terminates, so set it to a no-op for this test.
    let count = 0;
    const irrelevant = () => {
      count++;
    };
    Deno.addSignalListener("SIGUSR1", irrelevant);
    for (const _ of Array(3)) {
      await delay(20);
      Deno.kill(Deno.pid, "SIGUSR1");
    }
    while (count < 3) {
      await delay(20);
    }
    Deno.removeSignalListener("SIGUSR1", irrelevant);

    // No change
    assertEquals(c, "010101000");

    Deno.removeSignalListener("SIGUSR2", listener0);

    await delay(100);

    // The first 3 events are handled by both handlers
    // The last 3 events are handled only by handler0
    assertEquals(c, "010101000");
  },
);

// This tests that pending op_signal_poll doesn't block the runtime from exiting the process.
Deno.test(
  {
    permissions: { run: true, read: true },
  },
  async function canExitWhileListeningToSignal() {
    const { code } = await new Deno.Command(Deno.execPath(), {
      args: [
        "eval",
        "Deno.addSignalListener('SIGINT', () => {})",
      ],
    }).output();
    assertEquals(code, 0);
  },
);

Deno.test(
  {
    ignore: Deno.build.os !== "windows",
    permissions: { run: true },
  },
  function windowsThrowsOnNegativeProcessIdTest() {
    assertThrows(
      () => {
        Deno.kill(-1, "SIGKILL");
      },
      TypeError,
      "Invalid pid",
    );
  },
);

Deno.test(
  {
    ignore: Deno.build.os !== "windows",
    permissions: { run: true },
  },
  function noOpenSystemIdleProcessTest() {
    let signal: Deno.Signal = "SIGKILL";

    assertThrows(
      () => {
        Deno.kill(0, signal);
      },
      TypeError,
      `Invalid pid`,
    );

    signal = "SIGTERM";
    assertThrows(
      () => {
        Deno.kill(0, signal);
      },
      TypeError,
      `Invalid pid`,
    );
  },
);

Deno.test(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",
    );
  },
);

Deno.test(
  { ignore: Deno.build.os !== "linux" },
  function signalAliasLinux() {
    const i = () => {};
    Deno.addSignalListener("SIGUNUSED", i);
    Deno.addSignalListener("SIGPOLL", i);

    Deno.removeSignalListener("SIGUNUSED", i);
    Deno.removeSignalListener("SIGPOLL", i);
  },
);