From c208f9d51fe2dc5903cb36f167f5ceb639a3124a Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 1 Jun 2017 06:13:58 -0600 Subject: [PATCH] test: check destroy hooks are called before exit Verify that the destroy callback for a TCP server is called before exit if it is closed in another destroy callback. Fixes: https://github.com/nodejs/node/issues/13262 PR-URL: https://github.com/nodejs/node/pull/13369 Reviewed-By: Trevor Norris Reviewed-By: Andreas Madsen --- .../test-async-hooks-close-during-destroy.js | 37 +++++++++++++++++++ ...st-async-hooks-top-level-clearimmediate.js | 27 ++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 test/parallel/test-async-hooks-close-during-destroy.js create mode 100644 test/parallel/test-async-hooks-top-level-clearimmediate.js diff --git a/test/parallel/test-async-hooks-close-during-destroy.js b/test/parallel/test-async-hooks-close-during-destroy.js new file mode 100644 index 00000000000000..98e12e77fbcce1 --- /dev/null +++ b/test/parallel/test-async-hooks-close-during-destroy.js @@ -0,0 +1,37 @@ +'use strict'; +// Test that async ids that are added to the destroy queue while running a +// `destroy` callback are handled correctly. + +const common = require('../common'); +const assert = require('assert'); +const async_hooks = require('async_hooks'); + +const initCalls = new Set(); +let destroyResCallCount = 0; +let res2; + +async_hooks.createHook({ + init: common.mustCallAtLeast((id, provider, triggerId) => { + if (provider === 'foobar') + initCalls.add(id); + }, 2), + destroy: common.mustCallAtLeast((id) => { + if (!initCalls.has(id)) return; + + switch (destroyResCallCount++) { + case 0: + // Trigger the second `destroy` call. + res2.emitDestroy(); + break; + case 2: + assert.fail('More than 2 destroy() invocations'); + break; + } + }, 2) +}).enable(); + +const res1 = new async_hooks.AsyncResource('foobar'); +res2 = new async_hooks.AsyncResource('foobar'); +res1.emitDestroy(); + +process.on('exit', () => assert.strictEqual(destroyResCallCount, 2)); diff --git a/test/parallel/test-async-hooks-top-level-clearimmediate.js b/test/parallel/test-async-hooks-top-level-clearimmediate.js new file mode 100644 index 00000000000000..f667c3ca337816 --- /dev/null +++ b/test/parallel/test-async-hooks-top-level-clearimmediate.js @@ -0,0 +1,27 @@ +'use strict'; + +// Regression test for https://github.com/nodejs/node/issues/13262 + +const common = require('../common'); +const assert = require('assert'); +const async_hooks = require('async_hooks'); + +let seenId, seenResource; + +async_hooks.createHook({ + init: common.mustCall((id, provider, triggerId, resource) => { + seenId = id; + seenResource = resource; + assert.strictEqual(provider, 'Immediate'); + assert.strictEqual(triggerId, 1); + }), + before: common.mustNotCall(), + after: common.mustNotCall(), + destroy: common.mustCall((id) => { + assert.strictEqual(seenId, id); + }) +}).enable(); + +const immediate = setImmediate(common.mustNotCall()); +assert.strictEqual(immediate, seenResource); +clearImmediate(immediate);