http2: add diagnostics channel 'http2.server.stream.created' · nodejs/node@13dbbdc

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.server.stream.created' channel when

9+

// ServerHttp2Streams are created by both:

10+

// - in response to an incoming 'stream' event from the client

11+

// - the server calling ServerHttp2Stream#pushStream()

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

20+21+

dc.subscribe('http2.server.stream.created', common.mustCall(({ stream, headers }) => {

22+

// Since ServerHttp2Stream is not exported from any module, this just checks

23+

// if the stream is an instance of Duplex and the constructor name is

24+

// 'ServerHttp2Stream'.

25+

assert.ok(stream instanceof Duplex);

26+

assert.strictEqual(stream.constructor.name, 'ServerHttp2Stream');

27+

assert.ok(headers && !Array.isArray(headers) && typeof headers === 'object');

28+

}, serverHttp2StreamCreationCount));

29+30+

const server = http2.createServer();

31+

server.on('stream', common.mustCall((stream) => {

32+

stream.respond();

33+

stream.end();

34+35+

stream.pushStream({}, common.mustSucceed((pushStream) => {

36+

pushStream.respond();

37+

pushStream.end();

38+

}));

39+

}));

40+41+

server.listen(0, common.mustCall(() => {

42+

const port = server.address().port;

43+

const client = http2.connect(`http://localhost:${port}`);

44+45+

const countdown = new Countdown(serverHttp2StreamCreationCount, () => {

46+

client.close();

47+

server.close();

48+

});

49+50+

const stream = client.request({});

51+

stream.on('response', common.mustCall(() => {

52+

countdown.dec();

53+

}));

54+55+

client.on('stream', common.mustCall((pushStream) => {

56+

pushStream.on('push', common.mustCall(() => {

57+

countdown.dec();

58+

}));

59+

}));

60+

}));