node/test/async-hooks/test-async-local-storage-http-agent.js
Antoine du Hamel bfc81ca228
test: ensure assertions are reachable in test/async-hooks
PR-URL: https://github.com/nodejs/node/pull/60150
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
2025-10-09 21:57:24 +00:00

36 lines
831 B
JavaScript

'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, common.mustCall(() => {
http.get({ agent, port }, common.mustCall((res) => {
assert.strictEqual(asyncLocalStorage.getStore(), i);
if (++responses === N) {
server.close();
agent.destroy();
}
res.resume();
}));
}));
}
}));