|
20 | 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21 | 21 |
|
22 | 22 | 'use strict';
|
23 |
| -require('../common'); |
| 23 | +const common = require('../common'); |
24 | 24 | const assert = require('assert');
|
25 | 25 | const http = require('http');
|
26 | 26 |
|
27 |
| -const server = http.Server(function(req, res) { |
28 |
| - res.writeHead(200); |
29 |
| - res.end('hello world\n'); |
30 |
| -}); |
31 |
| - |
32 |
| -let responses = 0; |
33 | 27 | const N = 4;
|
34 | 28 | const M = 4;
|
| 29 | +const server = http.Server(common.mustCall(function(req, res) { |
| 30 | + res.writeHead(200); |
| 31 | + res.end('hello world\n'); |
| 32 | +}, (N * M))); // N * M = good requests (the errors will not be counted) |
35 | 33 |
|
36 |
| -server.listen(0, function() { |
37 |
| - const port = this.address().port; |
38 |
| - for (let i = 0; i < N; i++) { |
39 |
| - setTimeout(function() { |
40 |
| - for (let j = 0; j < M; j++) { |
41 |
| - http.get({ port: port, path: '/' }, function(res) { |
42 |
| - console.log('%d %d', responses, res.statusCode); |
43 |
| - if (++responses === N * M) { |
44 |
| - console.error('Received all responses, closing server'); |
45 |
| - server.close(); |
46 |
| - } |
47 |
| - res.resume(); |
48 |
| - }).on('error', function(e) { |
49 |
| - console.log('Error!', e); |
50 |
| - process.exit(1); |
51 |
| - }); |
| 34 | +function makeRequests(outCount, inCount, shouldFail) { |
| 35 | + let responseCount = outCount * inCount; |
| 36 | + let onRequest = common.mustNotCall(); // Temporary |
| 37 | + const p = new Promise((resolve) => { |
| 38 | + onRequest = common.mustCall((res) => { |
| 39 | + if (--responseCount === 0) { |
| 40 | + server.close(); |
| 41 | + resolve(); |
52 | 42 | }
|
53 |
| - }, i); |
54 |
| - } |
55 |
| -}); |
| 43 | + if (!shouldFail) |
| 44 | + res.resume(); |
| 45 | + }, outCount * inCount); |
| 46 | + }); |
| 47 | + |
| 48 | + server.listen(0, () => { |
| 49 | + const port = server.address().port; |
| 50 | + for (let i = 0; i < outCount; i++) { |
| 51 | + setTimeout(() => { |
| 52 | + for (let j = 0; j < inCount; j++) { |
| 53 | + const req = http.get({ port: port, path: '/' }, onRequest); |
| 54 | + if (shouldFail) |
| 55 | + req.on('error', common.mustCall(onRequest)); |
| 56 | + else |
| 57 | + req.on('error', (e) => assert.fail(e)); |
| 58 | + } |
| 59 | + }, i); |
| 60 | + } |
| 61 | + }); |
| 62 | + return p; |
| 63 | +} |
56 | 64 |
|
| 65 | +const test1 = makeRequests(N, M); |
57 | 66 |
|
58 |
| -process.on('exit', function() { |
59 |
| - assert.strictEqual(N * M, responses); |
60 |
| -}); |
| 67 | +const test2 = () => { |
| 68 | + // Should not explode if can not create sockets. |
| 69 | + // Ref: https://github.com/nodejs/node/issues/13045 |
| 70 | + // Ref: https://github.com/nodejs/node/issues/13831 |
| 71 | + http.Agent.prototype.createConnection = function createConnection(_, cb) { |
| 72 | + process.nextTick(cb, new Error('nothing')); |
| 73 | + }; |
| 74 | + return makeRequests(N, M, true); |
| 75 | +}; |
| 76 | + |
| 77 | +test1 |
| 78 | + .then(test2) |
| 79 | + .catch((e) => { |
| 80 | + // This is currently the way to fail a test with a Promise. |
| 81 | + console.error(e); |
| 82 | + process.exit(1); |
| 83 | + } |
| 84 | +); |
0 commit comments