|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +var common = require('../common'); |
| 23 | +var assert = require('assert'); |
| 24 | + |
| 25 | +switch (process.argv[2]) { |
| 26 | + case undefined: |
| 27 | + return parent(); |
| 28 | + case 'child': |
| 29 | + return child(); |
| 30 | + default: |
| 31 | + throw new Error('wtf'); |
| 32 | +} |
| 33 | + |
| 34 | +function parent() { |
| 35 | + var http = require('http'); |
| 36 | + var bigResponse = new Buffer(10240) |
| 37 | + bigResponse.fill('x'); |
| 38 | + var gotTimeout = false; |
| 39 | + var childClosed = false; |
| 40 | + var requests = 0; |
| 41 | + var connections = 0; |
| 42 | + |
| 43 | + var server = http.createServer(function(req, res) { |
| 44 | + requests++; |
| 45 | + res.setHeader('content-length', bigResponse.length); |
| 46 | + res.end(bigResponse); |
| 47 | + }); |
| 48 | + |
| 49 | + server.on('connection', function(conn) { |
| 50 | + connections++; |
| 51 | + }); |
| 52 | + |
| 53 | + // kill the connection after a bit, verifying that the |
| 54 | + // flood of requests was eventually halted. |
| 55 | + server.setTimeout(200, function(conn) { |
| 56 | + gotTimeout = true; |
| 57 | + conn.destroy(); |
| 58 | + }); |
| 59 | + |
| 60 | + server.listen(common.PORT, function() { |
| 61 | + var spawn = require('child_process').spawn; |
| 62 | + var args = [__filename, 'child']; |
| 63 | + var child = spawn(process.execPath, args, { stdio: 'inherit' }); |
| 64 | + child.on('close', function(code) { |
| 65 | + assert(!code); |
| 66 | + childClosed = true; |
| 67 | + server.close(); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + process.on('exit', function() { |
| 72 | + assert(gotTimeout); |
| 73 | + assert(childClosed); |
| 74 | + assert.equal(connections, 1); |
| 75 | + // 1213 works out to be the number of requests we end up processing |
| 76 | + // before the outgoing connection backs up and requires a drain. |
| 77 | + // however, to avoid being unnecessarily tied to a specific magic number, |
| 78 | + // and making the test brittle, just assert that it's "a lot", which we |
| 79 | + // can safely assume is more than 500. |
| 80 | + assert(requests >= 500); |
| 81 | + console.log('ok'); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +function child() { |
| 86 | + var net = require('net'); |
| 87 | + |
| 88 | + var gotEpipe = false; |
| 89 | + var conn = net.connect({ port: common.PORT }); |
| 90 | + |
| 91 | + var req = 'GET / HTTP/1.1\r\nHost: localhost:' + |
| 92 | + common.PORT + '\r\nAccept: */*\r\n\r\n'; |
| 93 | + |
| 94 | + req = new Array(10241).join(req); |
| 95 | + |
| 96 | + conn.on('connect', function() { |
| 97 | + write(); |
| 98 | + }); |
| 99 | + |
| 100 | + conn.on('drain', write); |
| 101 | + |
| 102 | + conn.on('error', function(er) { |
| 103 | + gotEpipe = true; |
| 104 | + }); |
| 105 | + |
| 106 | + process.on('exit', function() { |
| 107 | + assert(gotEpipe); |
| 108 | + console.log('ok - child'); |
| 109 | + }); |
| 110 | + |
| 111 | + function write() { |
| 112 | + while (false !== conn.write(req, 'ascii')); |
| 113 | + } |
| 114 | +} |
0 commit comments