From 4416ffab8a48a5b5433ff669b0739c3e15b7e2cc Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 1 Aug 2016 12:58:24 +0200 Subject: [PATCH] test,util: fix flaky test-util-sigint-watchdog Fix parallel/test-util-sigint-watchdog by polling until the signal has definitely been received instead of just using a timeout. Fixes: https://github.com/nodejs/node/issues/7919 PR-URL: https://github.com/nodejs/node/pull/7933 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- src/node_util.cc | 8 ++++++++ src/node_watchdog.cc | 7 +++++++ src/node_watchdog.h | 1 + test/parallel/test-util-sigint-watchdog.js | 19 +++++++++++++------ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/node_util.cc b/src/node_util.cc index 18e89b7264..ee8584b945 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -103,6 +103,13 @@ void StopSigintWatchdog(const FunctionCallbackInfo& args) { args.GetReturnValue().Set(had_pending_signals); } + +void WatchdogHasPendingSigint(const FunctionCallbackInfo& args) { + bool ret = SigintWatchdogHelper::GetInstance()->HasPendingSignal(); + args.GetReturnValue().Set(ret); +} + + void Initialize(Local target, Local unused, Local context) { @@ -118,6 +125,7 @@ void Initialize(Local target, env->SetMethod(target, "startSigintWatchdog", StartSigintWatchdog); env->SetMethod(target, "stopSigintWatchdog", StopSigintWatchdog); + env->SetMethod(target, "watchdogHasPendingSigint", WatchdogHasPendingSigint); } } // namespace util diff --git a/src/node_watchdog.cc b/src/node_watchdog.cc index af781392e8..756ba64696 100644 --- a/src/node_watchdog.cc +++ b/src/node_watchdog.cc @@ -258,6 +258,13 @@ bool SigintWatchdogHelper::Stop() { } +bool SigintWatchdogHelper::HasPendingSignal() { + Mutex::ScopedLock lock(list_mutex_); + + return has_pending_signal_; +} + + void SigintWatchdogHelper::Register(SigintWatchdog* wd) { Mutex::ScopedLock lock(list_mutex_); diff --git a/src/node_watchdog.h b/src/node_watchdog.h index 43842147f9..dd97e4e735 100644 --- a/src/node_watchdog.h +++ b/src/node_watchdog.h @@ -63,6 +63,7 @@ class SigintWatchdogHelper { static SigintWatchdogHelper* GetInstance() { return &instance; } void Register(SigintWatchdog* watchdog); void Unregister(SigintWatchdog* watchdog); + bool HasPendingSignal(); int Start(); bool Stop(); diff --git a/test/parallel/test-util-sigint-watchdog.js b/test/parallel/test-util-sigint-watchdog.js index f9bb3ecd93..42e4048bd7 100644 --- a/test/parallel/test-util-sigint-watchdog.js +++ b/test/parallel/test-util-sigint-watchdog.js @@ -20,24 +20,24 @@ if (common.isWindows) { // Test with one call to the watchdog, one signal. binding.startSigintWatchdog(); process.kill(process.pid, 'SIGINT'); - setTimeout(common.mustCall(() => { + waitForPendingSignal(common.mustCall(() => { const hadPendingSignals = binding.stopSigintWatchdog(); assert.strictEqual(hadPendingSignals, true); next(); - }), common.platformTimeout(100)); + })); }, (next) => { // Nested calls are okay. binding.startSigintWatchdog(); binding.startSigintWatchdog(); process.kill(process.pid, 'SIGINT'); - setTimeout(common.mustCall(() => { + waitForPendingSignal(common.mustCall(() => { const hadPendingSignals1 = binding.stopSigintWatchdog(); const hadPendingSignals2 = binding.stopSigintWatchdog(); assert.strictEqual(hadPendingSignals1, true); assert.strictEqual(hadPendingSignals2, false); next(); - }), common.platformTimeout(100)); + })); }, () => { // Signal comes in after first call to stop. @@ -45,9 +45,16 @@ if (common.isWindows) { binding.startSigintWatchdog(); const hadPendingSignals1 = binding.stopSigintWatchdog(); process.kill(process.pid, 'SIGINT'); - setTimeout(common.mustCall(() => { + waitForPendingSignal(common.mustCall(() => { const hadPendingSignals2 = binding.stopSigintWatchdog(); assert.strictEqual(hadPendingSignals1, false); assert.strictEqual(hadPendingSignals2, true); - }), common.platformTimeout(100)); + })); }].reduceRight((a, b) => common.mustCall(b).bind(null, a))(); + +function waitForPendingSignal(cb) { + if (binding.watchdogHasPendingSigint()) + cb(); + else + setTimeout(waitForPendingSignal, 10, cb); +}