|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const initHooks = require('./init-hooks'); |
| 5 | +const { checkInvocations } = require('./hook-checks'); |
| 6 | +const fixtures = require('../common/fixtures'); |
| 7 | +if (!common.hasCrypto) |
| 8 | + common.skip('missing crypto'); |
| 9 | +const http2 = require('http2'); |
| 10 | +const assert = require('assert'); |
| 11 | +const 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 | + |
| 16 | +const hooks = initHooks(); |
| 17 | +hooks.enable(); |
| 18 | + |
| 19 | +const { |
| 20 | + HTTP2_HEADER_CONTENT_TYPE |
| 21 | +} = http2.constants; |
| 22 | + |
| 23 | +// Use large fixture to get several file operations. |
| 24 | +const fname = fixtures.path('person-large.jpg'); |
| 25 | +const fd = fs.openSync(fname, 'r'); |
| 26 | + |
| 27 | +const server = http2.createServer(); |
| 28 | +server.on('stream', (stream) => { |
| 29 | + stream.respondWithFD(fd, { |
| 30 | + [HTTP2_HEADER_CONTENT_TYPE]: 'text/plain' |
| 31 | + }); |
| 32 | +}); |
| 33 | +server.on('close', common.mustCall(() => fs.closeSync(fd))); |
| 34 | +server.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 | + |
| 47 | +process.on('exit', onExit); |
| 48 | + |
| 49 | +function 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 | +} |
0 commit comments