From 2928afba76db8d54c6da3deaf163e40002ad7358 Mon Sep 17 00:00:00 2001 From: Gil Pedersen Date: Mon, 21 Jun 2021 16:03:22 +0200 Subject: [PATCH] Harden stream processing close test --- test/transmit.js | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/test/transmit.js b/test/transmit.js index 7b506c3d9..2bc3786ff 100755 --- a/test/transmit.js +++ b/test/transmit.js @@ -1190,7 +1190,9 @@ describe('transmission', () => { await log; }); - it('stops processing the stream when the request closes', async () => { + it('stops processing the stream when the connection closes', async () => { + + let stream; const ErrStream = class extends Stream.Readable { @@ -1198,30 +1200,35 @@ describe('transmission', () => { super(); this.request = request; + this.reads = 0; } _read(size) { - if (this.isDone) { - return; + if (this.reads === 0) { + this.push('here is the response'); + this.request.raw.res.destroy(); } + else { + // "Inifitely" push more content - this.isDone = true; - this.push('here is the response'); - process.nextTick(() => { - - this.request.raw.req.emit('close'); process.nextTick(() => { - this.push(null); + this.push('.'); }); - }); + } + + ++this.reads; } }; const server = Hapi.server(); const log = server.events.once('response'); - server.route({ method: 'GET', path: '/stream', handler: (request, h) => h.response(new ErrStream(request)).bytes(0) }); + server.route({ method: 'GET', path: '/stream', handler: (request, h) => { + + stream = new ErrStream(request); + return h.response(stream).bytes(0); + } }); const res = await server.inject({ url: '/stream', headers: { 'Accept-Encoding': 'gzip' } }); expect(res.statusCode).to.equal(499); @@ -1234,6 +1241,8 @@ describe('transmission', () => { expect(request.response.statusCode).to.equal(499); expect(request.info.completed).to.be.above(0); expect(request.info.responded).to.equal(0); + + expect(stream.reads).to.equal(2); }); it('does not truncate the response when stream finishes before response is done', async () => {