• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const initHooks = require('./init-hooks');
5const { checkInvocations } = require('./hook-checks');
6const fixtures = require('../common/fixtures');
7if (!common.hasCrypto)
8  common.skip('missing crypto');
9const http2 = require('http2');
10const assert = require('assert');
11const fs = require('fs');
12
13// Checks that the async resource is not reused by FileHandle.
14// Test is based on parallel\test-http2-respond-file-fd.js.
15
16const hooks = initHooks();
17hooks.enable();
18
19const {
20  HTTP2_HEADER_CONTENT_TYPE
21} = http2.constants;
22
23// Use large fixture to get several file operations.
24const fname = fixtures.path('person-large.jpg');
25const fd = fs.openSync(fname, 'r');
26
27const server = http2.createServer();
28server.on('stream', (stream) => {
29  stream.respondWithFD(fd, {
30    [HTTP2_HEADER_CONTENT_TYPE]: 'text/plain'
31  });
32});
33server.on('close', common.mustCall(() => fs.closeSync(fd)));
34server.listen(0, () => {
35  const client = http2.connect(`http://localhost:${server.address().port}`);
36  const req = client.request();
37
38  req.on('response', common.mustCall());
39  req.on('data', () => {});
40  req.on('end', common.mustCall(() => {
41    client.close();
42    server.close();
43  }));
44  req.end();
45});
46
47process.on('exit', onExit);
48
49function onExit() {
50  hooks.disable();
51  hooks.sanityCheck();
52  const activities = hooks.activities;
53
54  // Verify both invocations
55  const fsReqs = activities.filter((x) => x.type === 'FSREQCALLBACK');
56  assert.ok(fsReqs.length >= 2);
57
58  checkInvocations(fsReqs[0], { init: 1, destroy: 1 }, 'when process exits');
59  checkInvocations(fsReqs[1], { init: 1, destroy: 1 }, 'when process exits');
60
61  // Verify reuse handle has been wrapped
62  assert.ok(fsReqs[0].handle !== fsReqs[1].handle, 'Resource reused');
63  assert.ok(fsReqs[0].handle === fsReqs[1].handle.handle,
64            'Resource not wrapped correctly');
65}
66