11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciif (common.isWindows) {
51cb0ef41Sopenharmony_ci  // No way to send CTRL_C_EVENT to processes from JS right now.
61cb0ef41Sopenharmony_ci  common.skip('platform not supported');
71cb0ef41Sopenharmony_ci}
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
111cb0ef41Sopenharmony_ciconst binding = internalBinding('contextify');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci[(next) => {
141cb0ef41Sopenharmony_ci  // Test with no signal observed.
151cb0ef41Sopenharmony_ci  binding.startSigintWatchdog();
161cb0ef41Sopenharmony_ci  const hadPendingSignals = binding.stopSigintWatchdog();
171cb0ef41Sopenharmony_ci  assert.strictEqual(hadPendingSignals, false);
181cb0ef41Sopenharmony_ci  next();
191cb0ef41Sopenharmony_ci},
201cb0ef41Sopenharmony_ci (next) => {
211cb0ef41Sopenharmony_ci   // Test with one call to the watchdog, one signal.
221cb0ef41Sopenharmony_ci   binding.startSigintWatchdog();
231cb0ef41Sopenharmony_ci   process.kill(process.pid, 'SIGINT');
241cb0ef41Sopenharmony_ci   waitForPendingSignal(common.mustCall(() => {
251cb0ef41Sopenharmony_ci     const hadPendingSignals = binding.stopSigintWatchdog();
261cb0ef41Sopenharmony_ci     assert.strictEqual(hadPendingSignals, true);
271cb0ef41Sopenharmony_ci     next();
281cb0ef41Sopenharmony_ci   }));
291cb0ef41Sopenharmony_ci },
301cb0ef41Sopenharmony_ci (next) => {
311cb0ef41Sopenharmony_ci   // Nested calls are okay.
321cb0ef41Sopenharmony_ci   binding.startSigintWatchdog();
331cb0ef41Sopenharmony_ci   binding.startSigintWatchdog();
341cb0ef41Sopenharmony_ci   process.kill(process.pid, 'SIGINT');
351cb0ef41Sopenharmony_ci   waitForPendingSignal(common.mustCall(() => {
361cb0ef41Sopenharmony_ci     const hadPendingSignals1 = binding.stopSigintWatchdog();
371cb0ef41Sopenharmony_ci     const hadPendingSignals2 = binding.stopSigintWatchdog();
381cb0ef41Sopenharmony_ci     assert.strictEqual(hadPendingSignals1, true);
391cb0ef41Sopenharmony_ci     assert.strictEqual(hadPendingSignals2, false);
401cb0ef41Sopenharmony_ci     next();
411cb0ef41Sopenharmony_ci   }));
421cb0ef41Sopenharmony_ci },
431cb0ef41Sopenharmony_ci () => {
441cb0ef41Sopenharmony_ci   // Signal comes in after first call to stop.
451cb0ef41Sopenharmony_ci   binding.startSigintWatchdog();
461cb0ef41Sopenharmony_ci   binding.startSigintWatchdog();
471cb0ef41Sopenharmony_ci   const hadPendingSignals1 = binding.stopSigintWatchdog();
481cb0ef41Sopenharmony_ci   process.kill(process.pid, 'SIGINT');
491cb0ef41Sopenharmony_ci   waitForPendingSignal(common.mustCall(() => {
501cb0ef41Sopenharmony_ci     const hadPendingSignals2 = binding.stopSigintWatchdog();
511cb0ef41Sopenharmony_ci     assert.strictEqual(hadPendingSignals1, false);
521cb0ef41Sopenharmony_ci     assert.strictEqual(hadPendingSignals2, true);
531cb0ef41Sopenharmony_ci   }));
541cb0ef41Sopenharmony_ci }].reduceRight((a, b) => common.mustCall(b).bind(null, a))();
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_cifunction waitForPendingSignal(cb) {
571cb0ef41Sopenharmony_ci  if (binding.watchdogHasPendingSigint())
581cb0ef41Sopenharmony_ci    cb();
591cb0ef41Sopenharmony_ci  else
601cb0ef41Sopenharmony_ci    setTimeout(waitForPendingSignal, 10, cb);
611cb0ef41Sopenharmony_ci}
62