summaryrefslogtreecommitdiff
path: root/test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js
diff options
context:
space:
mode:
authorAli Ijaz Sheikh <ofrobots@google.com>2018-02-01 15:25:41 -0800
committerAli Ijaz Sheikh <ofrobots@google.com>2018-02-09 13:03:34 -0800
commit523a1550a3d07ecc3d13e071cb0f1c732bae3bad (patch)
treefec47dc679756d17a77b57f67bdaf12ca5275144 /test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js
parent0f9efef05deb11dbbdf5adf96460839f5b332207 (diff)
downloadandroid-node-v8-523a1550a3d07ecc3d13e071cb0f1c732bae3bad.tar.gz
android-node-v8-523a1550a3d07ecc3d13e071cb0f1c732bae3bad.tar.bz2
android-node-v8-523a1550a3d07ecc3d13e071cb0f1c732bae3bad.zip
async_hooks: deprecate unsafe emit{Before,After}
The emit{Before,After} APIs in AsyncResource are problematic. * emit{Before,After} are named to suggest that the only thing they do is emit the before and after hooks. However, they in fact, mutate the current execution context. * They must be properly nested. Failure to do so by user code leads to catastrophic (unrecoverable) exceptions. It is very easy for the users to forget that they must be using a try/finally block around the code that must be surrounded by these operations. Even the example provided in the official docs makes this mistake. Failing to use a finally can lead to a catastrophic crash if the callback ends up throwing. This change provides a safer `runInAsyncScope` API as an alternative and deprecates emit{Before,After}. PR-URL: https://github.com/nodejs/node/pull/18513 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Diffstat (limited to 'test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js')
-rw-r--r--test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js20
1 files changed, 20 insertions, 0 deletions
diff --git a/test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js b/test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js
new file mode 100644
index 0000000000..bc4ac86e7f
--- /dev/null
+++ b/test/parallel/test-async-hooks-recursive-stack-runInAsyncScope.js
@@ -0,0 +1,20 @@
+'use strict';
+require('../common');
+const assert = require('assert');
+const async_hooks = require('async_hooks');
+
+// This test verifies that the async ID stack can grow indefinitely.
+
+function recurse(n) {
+ const a = new async_hooks.AsyncResource('foobar');
+ a.runInAsyncScope(() => {
+ assert.strictEqual(a.asyncId(), async_hooks.executionAsyncId());
+ assert.strictEqual(a.triggerAsyncId(), async_hooks.triggerAsyncId());
+ if (n >= 0)
+ recurse(n - 1);
+ assert.strictEqual(a.asyncId(), async_hooks.executionAsyncId());
+ assert.strictEqual(a.triggerAsyncId(), async_hooks.triggerAsyncId());
+ });
+}
+
+recurse(1000);