From b0040279744034845ee36805fcb2988917b68c5b Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Tue, 21 Apr 2020 05:58:28 +0530 Subject: [PATCH 1/2] http2: add `bytesWritten` test for `Http2Stream` note that this is for the `Http2Server` class. I'll soon be adding one for `Http2SecureServer` as well. Refs: https://github.com/nodejs/node/issues/29829 --- .../test-http2-byteswritten-server.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/parallel/test-http2-byteswritten-server.js diff --git a/test/parallel/test-http2-byteswritten-server.js b/test/parallel/test-http2-byteswritten-server.js new file mode 100644 index 00000000000000..4e60f0120dbcf7 --- /dev/null +++ b/test/parallel/test-http2-byteswritten-server.js @@ -0,0 +1,37 @@ +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); +const assert = require('assert'); +const http2 = require('http2'); +const { PerformanceObserver } = require('perf_hooks'); + +const http2Server = http2.createServer(common.mustCall(function(req, res) { + res.writeHead(200, { 'Content-Type': 'text/plain' }); + res.end(Buffer.from('9'.repeat(1024)), () => { + http2Server.close(); + }); +})); + +http2Server.listen(0, common.mustCall(function() { + const URL = `http://localhost:${http2Server.address().port}`; + const http2client = http2.connect(URL, { protocol: 'http:' }); + const req = http2client.request({ ':method': 'GET', ':path': '/' }); + req.on('response', common.mustCall()); + req.on('data', common.mustCall()); + req.on('end', common.mustCall(function() { + http2client.close(); + observer.disconnect(); + })); + req.end(); +})); + +const observer = new PerformanceObserver(common.mustCall(function(items) { + const entry = items.getEntries()[0]; + if (entry.name === 'Http2Stream') { + assert.strictEqual(entry.bytesWritten, 1024); + } +})); + +observer.observe({ entryTypes: ['http2'] }); From e691fd33a21752997befae24cef38165713130cc Mon Sep 17 00:00:00 2001 From: Pranshu Srivastava Date: Sat, 2 May 2020 10:36:21 +0530 Subject: [PATCH 2/2] don't use PerformanceObserver API --- .../test-http2-byteswritten-server.js | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/test/parallel/test-http2-byteswritten-server.js b/test/parallel/test-http2-byteswritten-server.js index 4e60f0120dbcf7..3077687fbf9a3b 100644 --- a/test/parallel/test-http2-byteswritten-server.js +++ b/test/parallel/test-http2-byteswritten-server.js @@ -5,33 +5,24 @@ if (!common.hasCrypto) common.skip('missing crypto'); const assert = require('assert'); const http2 = require('http2'); -const { PerformanceObserver } = require('perf_hooks'); const http2Server = http2.createServer(common.mustCall(function(req, res) { + res.socket.on('finish', common.mustCall(() => { + assert(req.socket.bytesWritten > 0); // 1094 + })); res.writeHead(200, { 'Content-Type': 'text/plain' }); - res.end(Buffer.from('9'.repeat(1024)), () => { - http2Server.close(); - }); + res.write(Buffer.from('1'.repeat(1024))); + res.end(); })); http2Server.listen(0, common.mustCall(function() { const URL = `http://localhost:${http2Server.address().port}`; const http2client = http2.connect(URL, { protocol: 'http:' }); const req = http2client.request({ ':method': 'GET', ':path': '/' }); - req.on('response', common.mustCall()); req.on('data', common.mustCall()); req.on('end', common.mustCall(function() { http2client.close(); - observer.disconnect(); + http2Server.close(); })); req.end(); })); - -const observer = new PerformanceObserver(common.mustCall(function(items) { - const entry = items.getEntries()[0]; - if (entry.name === 'Http2Stream') { - assert.strictEqual(entry.bytesWritten, 1024); - } -})); - -observer.observe({ entryTypes: ['http2'] });