|
| 1 | +var common = require('../common'); |
| 2 | +var assert = require('assert'); |
| 3 | +var http = require('http'); |
| 4 | + |
| 5 | +var clientRequest = null; |
| 6 | +var eventCount = 0; |
| 7 | +var testTickCount = 2; |
| 8 | + |
| 9 | +var server = http.createServer(function(req, res) { |
| 10 | + console.log('server: request'); |
| 11 | + |
| 12 | + res.on('finish', function() { |
| 13 | + console.log('server: response finish'); |
| 14 | + eventCount++; |
| 15 | + }); |
| 16 | + res.on('close', function() { |
| 17 | + console.log('server: response close'); |
| 18 | + eventCount++; |
| 19 | + }); |
| 20 | + |
| 21 | + console.log('client: aborting request'); |
| 22 | + clientRequest.abort(); |
| 23 | + |
| 24 | + var ticks = 0; |
| 25 | + function tick() { |
| 26 | + console.log('server: tick ' + ticks + |
| 27 | + (req.connection.destroyed ? ' (connection destroyed!)' : '')); |
| 28 | + |
| 29 | + if (ticks < testTickCount) { |
| 30 | + ticks++; |
| 31 | + setImmediate(tick); |
| 32 | + } else { |
| 33 | + sendResponse(); |
| 34 | + } |
| 35 | + } |
| 36 | + tick(); |
| 37 | + |
| 38 | + function sendResponse() { |
| 39 | + console.log('server: sending response'); |
| 40 | + res.writeHead(200, {'Content-Type': 'text/plain'}); |
| 41 | + res.end('Response\n'); |
| 42 | + console.log('server: res.end() returned'); |
| 43 | + |
| 44 | + handleResponseEnd(); |
| 45 | + } |
| 46 | +}); |
| 47 | + |
| 48 | +server.on('listening', function() { |
| 49 | + console.log('server: listening on port ' + common.PORT); |
| 50 | + console.log('-----------------------------------------------------'); |
| 51 | + startRequest(); |
| 52 | +}); |
| 53 | + |
| 54 | +server.on('connection', function(connection) { |
| 55 | + console.log('server: connection'); |
| 56 | + connection.on('close', function() { |
| 57 | + console.log('server: connection close'); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +server.on('close', function() { |
| 62 | + console.log('server: close'); |
| 63 | +}); |
| 64 | + |
| 65 | +server.listen(common.PORT); |
| 66 | + |
| 67 | +function startRequest() { |
| 68 | + console.log('client: starting request - testing with %d ticks after abort()', |
| 69 | + testTickCount); |
| 70 | + eventCount = 0; |
| 71 | + |
| 72 | + var options = {port: common.PORT, path: '/'}; |
| 73 | + clientRequest = http.get(options, function() {}); |
| 74 | + clientRequest.on('error', function() {}); |
| 75 | +} |
| 76 | + |
| 77 | +function handleResponseEnd() { |
| 78 | + setImmediate(function() { |
| 79 | + setImmediate(function() { |
| 80 | + assert.equal(eventCount, 1); |
| 81 | + |
| 82 | + if (testTickCount > 0) { |
| 83 | + testTickCount--; |
| 84 | + startRequest(); |
| 85 | + } else { |
| 86 | + server.close(); |
| 87 | + } |
| 88 | + }); |
| 89 | + }); |
| 90 | +} |
0 commit comments