summaryrefslogtreecommitdiff
path: root/test/async-hooks/test-async-local-storage-http-agent.js
blob: 54c8a585a5ba1c646b0e23e062c2f8768a626d1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'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();
      }));
    });
  }
}));