~ubuntu-branches/ubuntu/precise/nodejs/precise

« back to all changes in this revision

Viewing changes to test/simple/test-http-client-race-2.js

  • Committer: Bazaar Package Importer
  • Author(s): Jérémy Lal
  • Date: 2010-08-20 11:49:04 UTC
  • mfrom: (7.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100820114904-lz22w6fkth7yh179
Tags: 0.2.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require("../common");
 
1
common = require("../common");
 
2
assert = common.assert
2
3
http = require("http");
3
4
url = require("url");
4
5
 
22
23
  };
23
24
 
24
25
  res.writeHead(200, { "Content-Type": "text/plain"
25
 
                      , "Content-Length": body.length
26
 
                      });
 
26
                     , "Content-Length": body.length
 
27
                     });
27
28
  res.end(body);
28
29
});
29
 
server.listen(PORT);
30
 
 
31
 
var client = http.createClient(PORT);
 
30
server.listen(common.PORT);
32
31
 
33
32
var body1 = "";
34
33
var body2 = "";
35
34
var body3 = "";
36
35
 
37
 
//
38
 
// Client #1 is assigned Parser #1
39
 
//
40
 
var req1 = client.request("/1")
41
 
req1.addListener('response', function (res1) {
42
 
  res1.setBodyEncoding("utf8");
43
 
 
44
 
  res1.addListener('data', function (chunk) {
45
 
    body1 += chunk;
46
 
  });
47
 
 
48
 
  res1.addListener('end', function () {
49
 
    //
50
 
    // Delay execution a little to allow the "close" event to be processed
51
 
    // (required to trigger this bug!)
52
 
    //
53
 
    setTimeout(function () {
54
 
      //
55
 
      // The bug would introduce itself here: Client #2 would be allocated the
56
 
      // parser that previously belonged to Client #1. But we're not finished
57
 
      // with Client #1 yet!
58
 
      //
59
 
      var client2 = http.createClient(PORT);
60
 
 
61
 
      //
62
 
      // At this point, the bug would manifest itself and crash because the
63
 
      // internal state of the parser was no longer valid for use by Client #1.
64
 
      //
65
 
      var req2 = client.request("/2");
66
 
      req2.addListener('response', function (res2) {
67
 
        res2.setBodyEncoding("utf8");
68
 
        res2.addListener('data', function (chunk) { body2 += chunk; });
69
 
        res2.addListener('end', function () {
70
 
 
71
 
          //
72
 
          // Just to be really sure we've covered all our bases, execute a
73
 
          // request using client2.
74
 
          //
75
 
          var req3 = client2.request("/3");
76
 
          req3.addListener('response', function (res3) {
77
 
            res3.setBodyEncoding("utf8");
78
 
            res3.addListener('data', function (chunk) { body3 += chunk });
79
 
            res3.addListener('end', function() { server.close(); });
 
36
server.addListener("listening", function() {
 
37
  var client = http.createClient(common.PORT);
 
38
 
 
39
  //
 
40
  // Client #1 is assigned Parser #1
 
41
  //
 
42
  var req1 = client.request("/1")
 
43
  req1.end();
 
44
  req1.addListener('response', function (res1) {
 
45
    res1.setEncoding("utf8");
 
46
 
 
47
    res1.addListener('data', function (chunk) {
 
48
      body1 += chunk;
 
49
    });
 
50
 
 
51
    res1.addListener('end', function () {
 
52
      //
 
53
      // Delay execution a little to allow the "close" event to be processed
 
54
      // (required to trigger this bug!)
 
55
      //
 
56
      setTimeout(function () {
 
57
        //
 
58
        // The bug would introduce itself here: Client #2 would be allocated the
 
59
        // parser that previously belonged to Client #1. But we're not finished
 
60
        // with Client #1 yet!
 
61
        //
 
62
        var client2 = http.createClient(common.PORT);
 
63
 
 
64
        //
 
65
        // At this point, the bug would manifest itself and crash because the
 
66
        // internal state of the parser was no longer valid for use by Client #1.
 
67
        //
 
68
        var req2 = client.request("/2");
 
69
        req2.end();
 
70
        req2.addListener('response', function (res2) {
 
71
          res2.setEncoding("utf8");
 
72
          res2.addListener('data', function (chunk) { body2 += chunk; });
 
73
          res2.addListener('end', function () {
 
74
 
 
75
            //
 
76
            // Just to be really sure we've covered all our bases, execute a
 
77
            // request using client2.
 
78
            //
 
79
            var req3 = client2.request("/3");
 
80
            req3.end();
 
81
            req3.addListener('response', function (res3) {
 
82
              res3.setEncoding("utf8");
 
83
              res3.addListener('data', function (chunk) { body3 += chunk });
 
84
              res3.addListener('end', function() { server.close(); });
 
85
            });
80
86
          });
81
 
          req3.end();
82
87
        });
83
 
      });
84
 
      req2.end();
85
 
    }, 500);
 
88
      }, 500);
 
89
    });
86
90
  });
87
91
});
88
 
req1.end();
89
92
 
90
93
process.addListener("exit", function () {
91
94
  assert.equal(body1_s, body1);