Skip to content

Commit 430f9db

Browse files
committed
fix tests not draining data for http2 requests, resulting in timeouts in node v10.4 onwards
1 parent 16d9e12 commit 430f9db

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

test/compression.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,14 @@ describe('compression()', function () {
327327
})
328328
request.on('response', function (headers) {
329329
assert.equal(headers['content-encoding'], 'gzip')
330+
})
331+
request.on('error', function (error) {
332+
console.error('An error event occurred on a http2 client request.', error)
333+
})
334+
request.on('data', function (chunk) {
335+
// no-op without which the request will stay open and cause a test timeout
336+
})
337+
request.on('end', function () {
330338
closeHttp2(request, client, server, done)
331339
})
332340
request.end()
@@ -697,6 +705,12 @@ function createServer (opts, fn) {
697705
function createHttp2Server (opts, fn) {
698706
var _compression = compression(opts)
699707
var server = http2.createServer(function (req, res) {
708+
req.on('error', function (error) {
709+
console.error('An error event occurred on a http2 request.', error)
710+
})
711+
res.on('error', function (error) {
712+
console.error('An error event occurred on a http2 response.', error)
713+
})
700714
_compression(req, res, function (err) {
701715
if (err) {
702716
res.statusCode = err.status || 500
@@ -707,12 +721,21 @@ function createHttp2Server (opts, fn) {
707721
fn(req, res)
708722
})
709723
})
724+
server.on('error', function (error) {
725+
console.error('An error event occurred on the http2 server.', error)
726+
})
727+
server.on('sessionError', function (error) {
728+
console.error('A sessionError event occurred on the http2 server.', error)
729+
})
710730
server.listen(0, '127.0.0.1')
711731
return server
712732
}
713733

714734
function createHttp2Client (port) {
715735
var client = http2.connect('http://127.0.0.1:' + port)
736+
client.on('error', function (error) {
737+
console.error('An error event occurred in the http2 client stream.', error)
738+
})
716739
return client
717740
}
718741

@@ -727,9 +750,14 @@ function closeHttp2 (request, client, server, callback) {
727750
})
728751
})
729752
} else {
730-
// this is the node v9.x onwards (hopefully) way of closing the connections
753+
// this is the node v9.x onwards way of closing the connections
731754
request.close(http2.constants.NGHTTP2_NO_ERROR, function () {
732755
client.close(function () {
756+
// force existing connections to time out after 1ms.
757+
// this is done to force the server to close in some cases where it wouldn't do it otherwise.
758+
server.setTimeout(1, function () {
759+
console.warn('An http2 connection timed out instead of being closed properly.')
760+
})
733761
server.close(function () {
734762
callback()
735763
})

0 commit comments

Comments
 (0)