1'use strict'; 2const common = require('../common'); 3if (!common.hasCrypto) 4 common.skip('missing crypto'); 5const assert = require('assert'); 6const http2 = require('http2'); 7const makeDuplexPair = require('../common/duplexpair'); 8const { parentPort, Worker } = require('worker_threads'); 9 10// This test ensures that workers can be terminated without error while 11// stream activity is ongoing, in particular the C++ function 12// ReportWritesToJSStreamListener::OnStreamAfterReqFinished. 13 14const MAX_ITERATIONS = 5; 15const MAX_THREADS = 6; 16 17// Do not use isMainThread so that this test itself can be run inside a Worker. 18if (!process.env.HAS_STARTED_WORKER) { 19 process.env.HAS_STARTED_WORKER = 1; 20 21 function spinWorker(iter) { 22 const w = new Worker(__filename); 23 w.on('message', common.mustCall((msg) => { 24 assert.strictEqual(msg, 'terminate'); 25 w.terminate(); 26 })); 27 28 w.on('exit', common.mustCall(() => { 29 if (iter < MAX_ITERATIONS) 30 spinWorker(++iter); 31 })); 32 } 33 34 for (let i = 0; i < MAX_THREADS; i++) { 35 spinWorker(0); 36 } 37} else { 38 const server = http2.createServer(); 39 let i = 0; 40 server.on('stream', (stream, headers) => { 41 if (i === 1) { 42 parentPort.postMessage('terminate'); 43 } 44 i++; 45 46 stream.end(''); 47 }); 48 49 const { clientSide, serverSide } = makeDuplexPair(); 50 server.emit('connection', serverSide); 51 52 const client = http2.connect('http://localhost:80', { 53 createConnection: () => clientSide, 54 }); 55 56 function makeRequests() { 57 for (let i = 0; i < 3; i++) { 58 client.request().end(); 59 } 60 setImmediate(makeRequests); 61 } 62 makeRequests(); 63} 64