http2: add diagnostics channel 'http2.server.stream.start' · nodejs/node@b4df8d3

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.start' channel only

9+

// after the 'http2.server.stream.created' channel.

10+11+

const Countdown = require('../common/countdown');

12+

const assert = require('assert');

13+

const dc = require('diagnostics_channel');

14+

const http2 = require('http2');

15+16+

const serverHttp2StreamCreationCount = 2;

17+18+

const map = {};

19+20+

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

21+

map[stream.id] = { ...map[stream.id], 'createdTime': process.hrtime.bigint() };

22+

}, serverHttp2StreamCreationCount));

23+24+

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

25+

map[stream.id] = { ...map[stream.id], 'startTime': process.hrtime.bigint() };

26+

}, serverHttp2StreamCreationCount));

27+28+

const server = http2.createServer();

29+

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

30+

stream.respond();

31+

stream.end();

32+33+

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

34+

pushStream.respond();

35+

pushStream.end();

36+

}));

37+

}));

38+39+

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

40+

const port = server.address().port;

41+

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

42+43+

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

44+

client.close();

45+

server.close();

46+47+

const timings = Object.values(map);

48+

assert.strictEqual(timings.length, serverHttp2StreamCreationCount);

49+50+

for (const { createdTime, startTime } of timings) {

51+

assert.ok(createdTime < startTime);

52+

}

53+

});

54+55+

const stream = client.request({});

56+

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

57+

countdown.dec();

58+

}));

59+60+

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

61+

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

62+

countdown.dec();

63+

}));

64+

}));

65+

}));