Skip to content
Merged
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
12 changes: 5 additions & 7 deletions test/async-hooks/hook-checks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');

/**
Expand All @@ -15,7 +15,7 @@ const assert = require('assert');
* @param {string} stage the name of the stage in the test at which we are
* checking the invocations
*/
exports.checkInvocations = function checkInvocations(activity, hooks, stage) {
exports.checkInvocations = common.mustCallAtLeast(function checkInvocations(activity, hooks, stage) {
const stageInfo = `Checking invocations at stage "${stage}":\n `;

assert.ok(activity != null,
Expand All @@ -24,9 +24,7 @@ exports.checkInvocations = function checkInvocations(activity, hooks, stage) {
);

// Check that actual invocations for all hooks match the expected invocations
[ 'init', 'before', 'after', 'destroy', 'promiseResolve' ].forEach(checkHook);

function checkHook(k) {
[ 'init', 'before', 'after', 'destroy', 'promiseResolve' ].forEach((k) => {
const val = hooks[k];
// Not expected ... all good
if (val == null) return;
Expand All @@ -49,5 +47,5 @@ exports.checkInvocations = function checkInvocations(activity, hooks, stage) {
`time(s), but expected ${val} invocation(s).`;
assert.strictEqual(activity[k].length, val, msg2);
}
}
};
});
}, 0);
18 changes: 9 additions & 9 deletions test/async-hooks/test-async-exec-resource-http-32060.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const {
executionAsyncResource,
Expand All @@ -22,16 +22,16 @@ const server = http.createServer((req, res) => {
}, 1000);
});

server.listen(0, () => {
server.listen(0, common.mustCallAtLeast(() => {
assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]);
http.get({ port: server.address().port }, (res) => {
http.get({ port: server.address().port }, common.mustCallAtLeast((res) => {
assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]);
res.on('data', () => {
res.on('data', common.mustCallAtLeast(() => {
assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]);
});
res.on('end', () => {
}));
res.on('end', common.mustCall(() => {
assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]);
server.close();
});
});
});
}));
}));
}));
10 changes: 5 additions & 5 deletions test/async-hooks/test-async-exec-resource-http.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

require('../common');
const common = require('../common');
const assert = require('assert');
const {
executionAsyncResource,
Expand All @@ -20,11 +20,11 @@ const server = http.createServer((req, res) => {
res.end('ok');
});

server.listen(0, () => {
server.listen(0, common.mustCall(() => {
assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]);

http.get({ port: server.address().port }, () => {
http.get({ port: server.address().port }, common.mustCall(() => {
assert.strictEqual(executionAsyncResource(), hooked[executionAsyncId()]);
server.close();
});
});
}));
}));
10 changes: 5 additions & 5 deletions test/async-hooks/test-async-local-storage-args.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();

asyncLocalStorage.run({}, (runArg) => {
asyncLocalStorage.run({}, common.mustCall((runArg) => {
assert.strictEqual(runArg, 'foo');
asyncLocalStorage.exit((exitArg) => {
asyncLocalStorage.exit(common.mustCall((exitArg) => {
assert.strictEqual(exitArg, 'bar');
}, 'bar');
}, 'foo');
}), 'bar');
}), 'foo');
14 changes: 7 additions & 7 deletions test/async-hooks/test-async-local-storage-dgram.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

require('../common');
const common = require('../common');

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

Expand All @@ -10,17 +10,17 @@ const { AsyncLocalStorage } = require('async_hooks');

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

const asyncLocalStorage = new AsyncLocalStorage();

asyncLocalStorage.run(new Map(), () => {
asyncLocalStorage.run(new Map(), common.mustCall(() => {
asyncLocalStorage.getStore().set('foo', 'bar');
process.nextTick(() => {
assert.strictEqual(asyncLocalStorage.getStore().get('foo'), 'bar');
Expand All @@ -17,16 +17,16 @@ asyncLocalStorage.run(new Map(), () => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);

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

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

const asyncLocalStorage = new AsyncLocalStorage();

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

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

setTimeout(() => {
setTimeout(common.mustCall(() => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
}, 10);
}), 10);
4 changes: 2 additions & 2 deletions test/async-hooks/test-async-local-storage-gcable.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const { onGC } = require('../common/gc');

let asyncLocalStorage = new AsyncLocalStorage();

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

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

if (AsyncContextFrame.enabled) {
// This disable() is needed to remove reference form AsyncContextFrame
Expand Down
4 changes: 2 additions & 2 deletions test/async-hooks/test-async-local-storage-http-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ server.listen(0, common.mustCall(() => {
const port = server.address().port;

for (let i = 0; i < N; i++) {
asyncLocalStorage.run(i, () => {
asyncLocalStorage.run(i, common.mustCall(() => {
http.get({ agent, port }, common.mustCall((res) => {
assert.strictEqual(asyncLocalStorage.getStore(), i);
if (++responses === N) {
Expand All @@ -30,6 +30,6 @@ server.listen(0, common.mustCall(() => {
}
res.resume();
}));
});
}));
}
}));
6 changes: 3 additions & 3 deletions test/async-hooks/test-async-local-storage-http.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const { mustCall } = require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');
const http = require('http');
Expand All @@ -13,9 +13,9 @@ server.listen(0, () => {
asyncLocalStorage.run(new Map(), () => {
const store = asyncLocalStorage.getStore();
store.set('hello', 'world');
http.get({ host: 'localhost', port: server.address().port }, () => {
http.get({ host: 'localhost', port: server.address().port }, mustCall(() => {
assert.strictEqual(asyncLocalStorage.getStore().get('hello'), 'world');
server.close();
});
}));
});
});
10 changes: 5 additions & 5 deletions test/async-hooks/test-async-local-storage-misc-stores.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();

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

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

Expand All @@ -10,14 +10,14 @@ const inner = {};
function testInner() {
assert.strictEqual(asyncLocalStorage.getStore(), outer);

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

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

Expand Down
34 changes: 17 additions & 17 deletions test/async-hooks/test-async-local-storage-no-mix-contexts.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();
const asyncLocalStorage2 = new AsyncLocalStorage();

setTimeout(() => {
asyncLocalStorage.run(new Map(), () => {
asyncLocalStorage2.run(new Map(), () => {
setTimeout(common.mustCall(() => {
asyncLocalStorage.run(new Map(), common.mustCall(() => {
asyncLocalStorage2.run(new Map(), common.mustCall(() => {
const store = asyncLocalStorage.getStore();
const store2 = asyncLocalStorage2.getStore();
store.set('hello', 'world');
store2.set('hello', 'foo');
setTimeout(() => {
setTimeout(common.mustCall(() => {
assert.strictEqual(asyncLocalStorage.getStore().get('hello'), 'world');
assert.strictEqual(asyncLocalStorage2.getStore().get('hello'), 'foo');
asyncLocalStorage.exit(() => {
asyncLocalStorage.exit(common.mustCall(() => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
assert.strictEqual(asyncLocalStorage2.getStore().get('hello'), 'foo');
});
}));
assert.strictEqual(asyncLocalStorage.getStore().get('hello'), 'world');
assert.strictEqual(asyncLocalStorage2.getStore().get('hello'), 'foo');
}, 200);
});
});
}, 100);
}), 200);
}));
}));
}), 100);

setTimeout(() => {
asyncLocalStorage.run(new Map(), () => {
setTimeout(common.mustCall(() => {
asyncLocalStorage.run(new Map(), common.mustCall(() => {
const store = asyncLocalStorage.getStore();
store.set('hello', 'earth');
setTimeout(() => {
setTimeout(common.mustCall(() => {
assert.strictEqual(asyncLocalStorage.getStore().get('hello'), 'earth');
}, 100);
});
}, 100);
}), 100);
}));
}), 100);
12 changes: 4 additions & 8 deletions test/async-hooks/test-async-local-storage-promises.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');

Expand All @@ -11,18 +11,14 @@ async function main() {
assert.strictEqual(asyncLocalStorage.getStore().get('a'), 1);
throw err;
});
await new Promise((resolve, reject) => {
await assert.rejects(new Promise((resolve, reject) => {
asyncLocalStorage.run(new Map(), () => {
const store = asyncLocalStorage.getStore();
store.set('a', 1);
next().then(resolve, reject);
});
})
.catch((e) => {
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
assert.strictEqual(e, err);
});
}), err);
assert.strictEqual(asyncLocalStorage.getStore(), undefined);
}

main();
main().then(common.mustCall());
Loading
Loading