http2: add diagnostics channel 'http2.client.stream.close' · nodejs/node@0885546

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.close' channel when

9+

// ClientHttp2Streams created by these actions are closed:

10+

// - the client calling ClientHttp2Session#request()

11+

// - in response to an incoming 'push' event from the server

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 clientHttp2StreamCloseCount = 2;

20+21+

dc.subscribe('http2.client.stream.close', common.mustCall(({ stream }) => {

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+

assert.strictEqual(stream.closed, true);

28+

assert.strictEqual(stream.destroyed, false);

29+30+

assert.strictEqual(stream.rstCode, http2.constants.NGHTTP2_NO_ERROR);

31+

}, clientHttp2StreamCloseCount));

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(clientHttp2StreamCloseCount, () => {

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+

}));