• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const { createHook } = require('async_hooks');
6const http = require('http');
7
8// Verify that resource emitted for an HTTPParser is not reused.
9// Verify that correct create/destroy events are emitted.
10
11const reused = Symbol('reused');
12
13const reusedParser = [];
14const incomingMessageParser = [];
15const clientRequestParser = [];
16const dupDestroys = [];
17const destroyed = [];
18
19createHook({
20  init(asyncId, type, triggerAsyncId, resource) {
21    switch (type) {
22      case 'HTTPINCOMINGMESSAGE':
23        incomingMessageParser.push(asyncId);
24        break;
25      case 'HTTPCLIENTREQUEST':
26        clientRequestParser.push(asyncId);
27        break;
28    }
29
30    if (resource[reused]) {
31      reusedParser.push(
32        `resource reused: ${asyncId}, ${triggerAsyncId}, ${type}`,
33      );
34    }
35    resource[reused] = true;
36  },
37  destroy(asyncId) {
38    if (destroyed.includes(asyncId)) {
39      dupDestroys.push(asyncId);
40    } else {
41      destroyed.push(asyncId);
42    }
43  },
44}).enable();
45
46const server = http.createServer((req, res) => {
47  res.end();
48});
49
50server.listen(0, common.mustCall(() => {
51  const PORT = server.address().port;
52  const url = `http://127.0.0.1:${PORT}`;
53  http.get(url, common.mustCall(() => {
54    server.close(common.mustCall(() => {
55      server.listen(PORT, common.mustCall(() => {
56        http.get(url, common.mustCall(() => {
57          server.close(common.mustCall(() => {
58            setTimeout(common.mustCall(verify), 200);
59          }));
60        }));
61      }));
62    }));
63  }));
64}));
65
66function verify() {
67  assert.strictEqual(reusedParser.length, 0);
68
69  assert.strictEqual(incomingMessageParser.length, 2);
70  assert.strictEqual(clientRequestParser.length, 2);
71
72  assert.strictEqual(dupDestroys.length, 0);
73  incomingMessageParser.forEach((id) => assert.ok(destroyed.includes(id)));
74  clientRequestParser.forEach((id) => assert.ok(destroyed.includes(id)));
75}
76