@@ -5,46 +5,45 @@ if (!hasCrypto) |
5 | 5 | skip('missing crypto'); |
6 | 6 | const assert = require('assert'); |
7 | 7 | const { createServer, connect } = require('http2'); |
8 | | -const Countdown = require('../common/countdown'); |
| 8 | +const { once } = require('events'); |
9 | 9 |
|
10 | 10 | // This test ensures that `bufferSize` of Http2Session and Http2Stream work |
11 | 11 | // as expected. |
12 | 12 | { |
13 | | - const SOCKETS = 2; |
14 | | - const TIMES = 10; |
15 | | - const BUFFER_SIZE = 30; |
| 13 | + const kSockets = 2; |
| 14 | + const kTimes = 10; |
| 15 | + const kBufferSize = 30; |
16 | 16 | const server = createServer(); |
17 | 17 |
|
18 | 18 | let client; |
19 | | - const countdown = new Countdown(SOCKETS, () => { |
20 | | - client.close(); |
21 | | - server.close(); |
22 | | - }); |
23 | 19 |
|
24 | | - // Other `bufferSize` tests for net module and tls module |
25 | | - // don't assert `bufferSize` of server-side sockets. |
26 | | - server.on('stream', mustCall((stream) => { |
| 20 | + const getStream = async () => { |
| 21 | + const [ stream ] = await once(server, 'stream'); |
27 | 22 | stream.on('data', mustCall()); |
28 | 23 | stream.on('end', mustCall()); |
| 24 | + stream.on('close', mustCall()); |
| 25 | + return once(stream, 'close'); |
| 26 | + }; |
29 | 27 |
|
30 | | - stream.on('close', mustCall(() => { |
31 | | - countdown.dec(); |
32 | | - })); |
33 | | - }, SOCKETS)); |
| 28 | + const promises = [...new Array(kSockets)].map(getStream); |
| 29 | + Promise.all(promises).then(mustCall(() => { |
| 30 | + client.close(); |
| 31 | + server.close(); |
| 32 | + })); |
34 | 33 |
|
35 | 34 | server.listen(0, mustCall(() => { |
36 | 35 | const authority = `http://localhost:${server.address().port}`; |
37 | 36 | client = connect(authority); |
38 | 37 |
|
39 | 38 | client.once('connect', mustCall()); |
40 | 39 |
|
41 | | - for (let j = 0; j < SOCKETS; j += 1) { |
| 40 | + for (let j = 0; j < kSockets; j += 1) { |
42 | 41 | const stream = client.request({ ':method': 'POST' }); |
43 | 42 | stream.on('data', () => {}); |
44 | 43 |
|
45 | | - for (let i = 0; i < TIMES; i += 1) { |
46 | | - stream.write(Buffer.allocUnsafe(BUFFER_SIZE), mustCall()); |
47 | | - const expectedSocketBufferSize = BUFFER_SIZE * (i + 1); |
| 44 | + for (let i = 0; i < kTimes; i += 1) { |
| 45 | + stream.write(Buffer.allocUnsafe(kBufferSize), mustCall()); |
| 46 | + const expectedSocketBufferSize = kBufferSize * (i + 1); |
48 | 47 | assert.strictEqual(stream.bufferSize, expectedSocketBufferSize); |
49 | 48 | } |
50 | 49 | stream.end(); |
|