summaryrefslogtreecommitdiff
path: root/test/pummel/test-http-upload-timeout.js
blob: 06b8e744966846e14005d91cf3ec7cbe85a72366 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// This tests setTimeout() by having multiple clients connecting and sending
// data in random intervals. Clients are also randomly disconnecting until there
// are no more clients left. If no false timeout occurs, this test has passed.
var common = require('../common'),
    assert = require('assert'),
    http = require('http'),
    server = http.createServer(),
    connections = 0;

server.on('request', function(req, res) {
  req.socket.setTimeout(1000);
  req.socket.on('timeout', function() {
    throw new Error('Unexpected timeout');
  });
  req.on('end', function() {
    connections--;
    res.writeHead(200);
    res.end("done\n");
    if (connections == 0) {
      server.close();
    }
  });
});

server.listen(common.PORT, '127.0.0.1', function() {
  for (var i = 0; i < 10; i++) {
    connections++;

    setTimeout(function() {
      var client = http.createClient(common.PORT),
          request = client.request('POST', '/');

      function ping() {
        var nextPing = (Math.random() * 900).toFixed();
        if (nextPing > 600) {
          request.end();
          return;
        }
        request.write('ping');
        setTimeout(ping, nextPing);
      }
      ping();
    }, i * 50);
  }
});