|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +if (!common.hasCrypto) |
| 5 | + common.skip('missing crypto'); |
| 6 | + |
| 7 | +// This test ensures that the built-in HTTP/2 diagnostics channels are reporting |
| 8 | +// the diagnostics messages for the 'http2.client.stream.finish' channel when |
| 9 | +// ClientHttp2Streams are received by both: |
| 10 | +// - the 'response' event |
| 11 | +// - the 'push' event |
| 12 | + |
| 13 | +const Countdown = require('../common/countdown'); |
| 14 | +const assert = require('assert'); |
| 15 | +const dc = require('diagnostics_channel'); |
| 16 | +const http2 = require('http2'); |
| 17 | +const { Duplex } = require('stream'); |
| 18 | + |
| 19 | +const clientHttp2StreamFinishCount = 2; |
| 20 | + |
| 21 | +dc.subscribe('http2.client.stream.finish', common.mustCall(({ stream, headers, flags }) => { |
| 22 | + // Since ClientHttp2Stream is not exported from any module, this just checks |
| 23 | + // if the stream is an instance of Duplex and the constructor name is |
| 24 | + // 'ClientHttp2Stream'. |
| 25 | + assert.ok(stream instanceof Duplex); |
| 26 | + assert.strictEqual(stream.constructor.name, 'ClientHttp2Stream'); |
| 27 | + |
| 28 | + assert.ok(headers && !Array.isArray(headers) && typeof headers === 'object'); |
| 29 | + |
| 30 | + assert.strictEqual(typeof flags, 'number'); |
| 31 | +}, clientHttp2StreamFinishCount)); |
| 32 | + |
| 33 | +const server = http2.createServer(); |
| 34 | +server.on('stream', common.mustCall((stream) => { |
| 35 | + stream.respond(); |
| 36 | + stream.end(); |
| 37 | + |
| 38 | + stream.pushStream({}, common.mustSucceed((pushStream) => { |
| 39 | + pushStream.respond(); |
| 40 | + pushStream.end(); |
| 41 | + })); |
| 42 | +})); |
| 43 | + |
| 44 | +server.listen(0, common.mustCall(() => { |
| 45 | + const port = server.address().port; |
| 46 | + const client = http2.connect(`http://localhost:${port}`); |
| 47 | + |
| 48 | + const countdown = new Countdown(clientHttp2StreamFinishCount, () => { |
| 49 | + client.close(); |
| 50 | + server.close(); |
| 51 | + }); |
| 52 | + |
| 53 | + const stream = client.request({}); |
| 54 | + stream.on('response', common.mustCall(() => { |
| 55 | + countdown.dec(); |
| 56 | + })); |
| 57 | + |
| 58 | + client.on('stream', common.mustCall((pushStream) => { |
| 59 | + pushStream.on('push', common.mustCall(() => { |
| 60 | + countdown.dec(); |
| 61 | + })); |
| 62 | + })); |
| 63 | +})); |
0 commit comments