Skip to content

test: split up test-async-context-frame into individual tests #54818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions test/async-hooks/test-async-local-storage-args-frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Flags: --experimental-async-context-frame
'use strict';
require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();

asyncLocalStorage.run({}, (runArg) => {
assert.strictEqual(runArg, 'foo');
asyncLocalStorage.exit((exitArg) => {
assert.strictEqual(exitArg, 'bar');
}, 'bar');
}, 'foo');
20 changes: 20 additions & 0 deletions test/async-hooks/test-async-local-storage-async-await-frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Flags: --experimental-async-context-frame
'use strict';
require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();

async function test() {
asyncLocalStorage.getStore().set('foo', 'bar');
await Promise.resolve();
assert.strictEqual(asyncLocalStorage.getStore().get('foo'), 'bar');
}

async function main() {
await asyncLocalStorage.run(new Map(), test);
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
}

main();
28 changes: 28 additions & 0 deletions test/async-hooks/test-async-local-storage-async-functions-frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Flags: --experimental-async-context-frame
'use strict';
require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');

async function foo() {}

const asyncLocalStorage = new AsyncLocalStorage();

async function testOut() {
await foo();
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
}

async function testAwait() {
await foo();
assert.notStrictEqual(asyncLocalStorage.getStore(), undefined);
assert.strictEqual(asyncLocalStorage.getStore().get('key'), 'value');
await asyncLocalStorage.exit(testOut);
}

asyncLocalStorage.run(new Map(), () => {
const store = asyncLocalStorage.getStore();
store.set('key', 'value');
testAwait(); // should not reject
});
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
27 changes: 27 additions & 0 deletions test/async-hooks/test-async-local-storage-dgram-frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Flags: --experimental-async-context-frame
'use strict';

require('../common');

// Regression tests for https://github.com/nodejs/node/issues/40693

const assert = require('assert');
const dgram = require('dgram');
const { AsyncLocalStorage } = require('async_hooks');

dgram.createSocket('udp4')
.on('message', function(msg, rinfo) { this.send(msg, rinfo.port); })
.on('listening', function() {
const asyncLocalStorage = new AsyncLocalStorage();
const store = { val: 'abcd' };
asyncLocalStorage.run(store, () => {
const client = dgram.createSocket('udp4');
client.on('message', (msg, rinfo) => {
assert.deepStrictEqual(asyncLocalStorage.getStore(), store);
client.close();
this.close();
});
client.send('Hello, world!', this.address().port);
});
})
.bind(0);
33 changes: 33 additions & 0 deletions test/async-hooks/test-async-local-storage-enable-disable-frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Flags: --experimental-async-context-frame
'use strict';
require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();

asyncLocalStorage.run(new Map(), () => {
asyncLocalStorage.getStore().set('foo', 'bar');
process.nextTick(() => {
assert.strictEqual(asyncLocalStorage.getStore().get('foo'), 'bar');
process.nextTick(() => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
});

asyncLocalStorage.disable();
assert.strictEqual(asyncLocalStorage.getStore(), undefined);

// Calls to exit() should not mess with enabled status
asyncLocalStorage.exit(() => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
});
assert.strictEqual(asyncLocalStorage.getStore(), undefined);

process.nextTick(() => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
asyncLocalStorage.run(new Map().set('bar', 'foo'), () => {
assert.strictEqual(asyncLocalStorage.getStore().get('bar'), 'foo');
});
});
});
});
21 changes: 21 additions & 0 deletions test/async-hooks/test-async-local-storage-enter-with-frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Flags: --experimental-async-context-frame
'use strict';
require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();

setImmediate(() => {
const store = { foo: 'bar' };
asyncLocalStorage.enterWith(store);

assert.strictEqual(asyncLocalStorage.getStore(), store);
setTimeout(() => {
assert.strictEqual(asyncLocalStorage.getStore(), store);
}, 10);
});

setTimeout(() => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
}, 10);
119 changes: 119 additions & 0 deletions test/async-hooks/test-async-local-storage-errors-frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Flags: --experimental-async-context-frame
'use strict';
const common = require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');
const vm = require('vm');

// err1 is emitted sync as a control - no events
// err2 is emitted after a timeout - uncaughtExceptionMonitor
// + uncaughtException
// err3 is emitted after some awaits - unhandledRejection
// err4 is emitted during handling err3 - uncaughtExceptionMonitor
// err5 is emitted after err4 from a VM lacking hooks - unhandledRejection
// + uncaughtException

const asyncLocalStorage = new AsyncLocalStorage();
const callbackToken = { callbackToken: true };
const awaitToken = { awaitToken: true };

let i = 0;

// Redefining the uncaughtExceptionHandler is a bit odd, so we just do this
// so we can track total invocations
let underlyingExceptionHandler;
const exceptionHandler = common.mustCall(function(...args) {
return underlyingExceptionHandler.call(this, ...args);
}, 2);
process.setUncaughtExceptionCaptureCallback(exceptionHandler);

const exceptionMonitor = common.mustCall((err, origin) => {
if (err.message === 'err2') {
assert.strictEqual(origin, 'uncaughtException');
assert.strictEqual(asyncLocalStorage.getStore(), callbackToken);
} else if (err.message === 'err4') {
assert.strictEqual(origin, 'unhandledRejection');
assert.strictEqual(asyncLocalStorage.getStore(), awaitToken);
} else {
assert.fail('unknown error ' + err);
}
}, 2);
process.on('uncaughtExceptionMonitor', exceptionMonitor);

function fireErr1() {
underlyingExceptionHandler = common.mustCall(function(err) {
++i;
assert.strictEqual(err.message, 'err2');
assert.strictEqual(asyncLocalStorage.getStore(), callbackToken);
}, 1);
try {
asyncLocalStorage.run(callbackToken, () => {
setTimeout(fireErr2, 0);
throw new Error('err1');
});
} catch (e) {
assert.strictEqual(e.message, 'err1');
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
}
}

function fireErr2() {
process.nextTick(() => {
assert.strictEqual(i, 1);
fireErr3();
});
throw new Error('err2');
}

function fireErr3() {
assert.strictEqual(asyncLocalStorage.getStore(), callbackToken);
const rejectionHandler3 = common.mustCall((err) => {
assert.strictEqual(err.message, 'err3');
assert.strictEqual(asyncLocalStorage.getStore(), awaitToken);
process.off('unhandledRejection', rejectionHandler3);

fireErr4();
}, 1);
process.on('unhandledRejection', rejectionHandler3);
async function awaitTest() {
await null;
throw new Error('err3');
}
asyncLocalStorage.run(awaitToken, awaitTest);
}

const uncaughtExceptionHandler4 = common.mustCall(
function(err) {
assert.strictEqual(err.message, 'err4');
assert.strictEqual(asyncLocalStorage.getStore(), awaitToken);
fireErr5();
}, 1);
function fireErr4() {
assert.strictEqual(asyncLocalStorage.getStore(), awaitToken);
underlyingExceptionHandler = uncaughtExceptionHandler4;
// re-entrant check
Promise.reject(new Error('err4'));
}

function fireErr5() {
assert.strictEqual(asyncLocalStorage.getStore(), awaitToken);
underlyingExceptionHandler = () => {};
const rejectionHandler5 = common.mustCall((err) => {
assert.strictEqual(err.message, 'err5');
assert.strictEqual(asyncLocalStorage.getStore(), awaitToken);
process.off('unhandledRejection', rejectionHandler5);
}, 1);
process.on('unhandledRejection', rejectionHandler5);
const makeOrphan = vm.compileFunction(`(${String(() => {
async function main() {
await null;
Promise.resolve().then(() => {
throw new Error('err5');
});
}
main();
})})()`);
makeOrphan();
}

fireErr1();
20 changes: 20 additions & 0 deletions test/async-hooks/test-async-local-storage-gcable-frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Flags: --experimental-async-context-frame --expose_gc
'use strict';

// This test ensures that AsyncLocalStorage gets gced once it was disabled
// and no strong references remain in userland.

const common = require('../common');
const { AsyncLocalStorage } = require('async_hooks');
const { onGC } = require('../common/gc');

let asyncLocalStorage = new AsyncLocalStorage();

asyncLocalStorage.run({}, () => {
asyncLocalStorage.disable();

onGC(asyncLocalStorage, { ongc: common.mustCall() });
});

asyncLocalStorage = null;
global.gc();
36 changes: 36 additions & 0 deletions test/async-hooks/test-async-local-storage-http-agent-frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Flags: --experimental-async-context-frame
'use strict';
const common = require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');
const http = require('http');

const asyncLocalStorage = new AsyncLocalStorage();

const agent = new http.Agent({
maxSockets: 1,
});

const N = 3;
let responses = 0;

const server = http.createServer(common.mustCall((req, res) => {
res.end('ok');
}, N));

server.listen(0, common.mustCall(() => {
const port = server.address().port;

for (let i = 0; i < N; i++) {
asyncLocalStorage.run(i, () => {
http.get({ agent, port }, common.mustCall((res) => {
assert.strictEqual(asyncLocalStorage.getStore(), i);
if (++responses === N) {
server.close();
agent.destroy();
}
res.resume();
}));
});
}
}));
22 changes: 22 additions & 0 deletions test/async-hooks/test-async-local-storage-http-frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Flags: --experimental-async-context-frame
'use strict';
require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');
const http = require('http');

const asyncLocalStorage = new AsyncLocalStorage();
const server = http.createServer((req, res) => {
res.end('ok');
});

server.listen(0, () => {
asyncLocalStorage.run(new Map(), () => {
const store = asyncLocalStorage.getStore();
store.set('hello', 'world');
http.get({ host: 'localhost', port: server.address().port }, () => {
assert.strictEqual(asyncLocalStorage.getStore().get('hello'), 'world');
server.close();
});
});
});
16 changes: 16 additions & 0 deletions test/async-hooks/test-async-local-storage-misc-stores-frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Flags: --experimental-async-context-frame
'use strict';
require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();

asyncLocalStorage.run('hello node', () => {
assert.strictEqual(asyncLocalStorage.getStore(), 'hello node');
});

const runStore = { hello: 'node' };
asyncLocalStorage.run(runStore, () => {
assert.strictEqual(asyncLocalStorage.getStore(), runStore);
});
26 changes: 26 additions & 0 deletions test/async-hooks/test-async-local-storage-nested-frame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Flags: --experimental-async-context-frame
'use strict';
require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();
const outer = {};
const inner = {};

function testInner() {
assert.strictEqual(asyncLocalStorage.getStore(), outer);

asyncLocalStorage.run(inner, () => {
assert.strictEqual(asyncLocalStorage.getStore(), inner);
});
assert.strictEqual(asyncLocalStorage.getStore(), outer);

asyncLocalStorage.exit(() => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
});
assert.strictEqual(asyncLocalStorage.getStore(), outer);
}

asyncLocalStorage.run(outer, testInner);
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
Loading
Loading