11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst initHooks = require('./init-hooks');
51cb0ef41Sopenharmony_ciconst { checkInvocations } = require('./hook-checks');
61cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
71cb0ef41Sopenharmony_ciif (!common.hasCrypto)
81cb0ef41Sopenharmony_ci  common.skip('missing crypto');
91cb0ef41Sopenharmony_ciconst http2 = require('http2');
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ciconst fs = require('fs');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci// Checks that the async resource is not reused by FileHandle.
141cb0ef41Sopenharmony_ci// Test is based on parallel\test-http2-respond-file-fd.js.
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst hooks = initHooks();
171cb0ef41Sopenharmony_cihooks.enable();
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst {
201cb0ef41Sopenharmony_ci  HTTP2_HEADER_CONTENT_TYPE,
211cb0ef41Sopenharmony_ci} = http2.constants;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci// Use large fixture to get several file operations.
241cb0ef41Sopenharmony_ciconst fname = fixtures.path('person-large.jpg');
251cb0ef41Sopenharmony_ciconst fd = fs.openSync(fname, 'r');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst server = http2.createServer();
281cb0ef41Sopenharmony_ciserver.on('stream', (stream) => {
291cb0ef41Sopenharmony_ci  stream.respondWithFD(fd, {
301cb0ef41Sopenharmony_ci    [HTTP2_HEADER_CONTENT_TYPE]: 'text/plain',
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci});
331cb0ef41Sopenharmony_ciserver.on('close', common.mustCall(() => fs.closeSync(fd)));
341cb0ef41Sopenharmony_ciserver.listen(0, () => {
351cb0ef41Sopenharmony_ci  const client = http2.connect(`http://localhost:${server.address().port}`);
361cb0ef41Sopenharmony_ci  const req = client.request();
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  req.on('response', common.mustCall());
391cb0ef41Sopenharmony_ci  req.on('data', () => {});
401cb0ef41Sopenharmony_ci  req.on('end', common.mustCall(() => {
411cb0ef41Sopenharmony_ci    client.close();
421cb0ef41Sopenharmony_ci    server.close();
431cb0ef41Sopenharmony_ci  }));
441cb0ef41Sopenharmony_ci  req.end();
451cb0ef41Sopenharmony_ci});
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciprocess.on('exit', onExit);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_cifunction onExit() {
501cb0ef41Sopenharmony_ci  hooks.disable();
511cb0ef41Sopenharmony_ci  hooks.sanityCheck();
521cb0ef41Sopenharmony_ci  const activities = hooks.activities;
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  // Verify both invocations
551cb0ef41Sopenharmony_ci  const fsReqs = activities.filter((x) => x.type === 'FSREQCALLBACK');
561cb0ef41Sopenharmony_ci  assert.ok(fsReqs.length >= 2);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  checkInvocations(fsReqs[0], { init: 1, destroy: 1 }, 'when process exits');
591cb0ef41Sopenharmony_ci  checkInvocations(fsReqs[1], { init: 1, destroy: 1 }, 'when process exits');
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  // Verify reuse handle has been wrapped
621cb0ef41Sopenharmony_ci  assert.ok(fsReqs[0].handle !== fsReqs[1].handle, 'Resource reused');
631cb0ef41Sopenharmony_ci  assert.ok(fsReqs[0].handle === fsReqs[1].handle.handle,
641cb0ef41Sopenharmony_ci            'Resource not wrapped correctly');
651cb0ef41Sopenharmony_ci}
66