0c4118494633303629a2d36da3cd416a03e37529
[platform/upstream/nodejs.git] / test / pummel / test-regress-GH-892.js
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
23
24
25 // Uploading a big file via HTTPS causes node to drop out of the event loop.
26 // https://github.com/joyent/node/issues/892
27 // In this test we set up an HTTPS in this process and launch a subprocess
28 // to POST a 32mb file to us. A bug in the pause/resume functionality of the
29 // TLS server causes the child process to exit cleanly before having sent
30 // the entire buffer.
31 var common = require('../common');
32 var assert = require('assert');
33 var spawn = require('child_process').spawn;
34 var https = require('https');
35 var fs = require('fs');
36
37 var PORT = 8000;
38
39
40 var bytesExpected = 1024 * 1024 * 32;
41 var gotResponse = false;
42
43 var started = false;
44
45 var childScript = require('path').join(common.fixturesDir, 'GH-892-request.js');
46
47 function makeRequest() {
48   if (started) return;
49   started = true;
50
51   var stderrBuffer = '';
52
53   // Pass along --trace-deprecation/--throw-deprecation in
54   // process.execArgv to track down nextTick recursion errors
55   // more easily.  Also, this is handy when using this test to
56   // view V8 opt/deopt behavior.
57   var args = process.execArgv.concat([ childScript,
58                                        common.PORT,
59                                        bytesExpected ]);
60
61   var child = spawn(process.execPath, args);
62
63   child.on('exit', function(code) {
64     assert.ok(/DONE/.test(stderrBuffer));
65     assert.equal(0, code);
66   });
67
68   // The following two lines forward the stdio from the child
69   // to parent process for debugging.
70   child.stderr.pipe(process.stderr);
71   child.stdout.pipe(process.stdout);
72
73
74   // Buffer the stderr so that we can check that it got 'DONE'
75   child.stderr.setEncoding('ascii');
76   child.stderr.on('data', function(d) {
77     stderrBuffer += d;
78   });
79 }
80
81
82 var serverOptions = {
83   key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
84   cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem')
85 };
86
87 var uploadCount = 0;
88
89 var server = https.Server(serverOptions, function(req, res) {
90   // Close the server immediately. This test is only doing a single upload.
91   // We need to make sure the server isn't keeping the event loop alive
92   // while the upload is in progress.
93   server.close();
94
95   req.on('data', function(d) {
96     process.stderr.write('.');
97     uploadCount += d.length;
98   });
99
100   req.on('end', function() {
101     assert.equal(bytesExpected, uploadCount);
102     res.writeHead(200, {'content-type': 'text/plain'});
103     res.end('successful upload\n');
104   });
105 });
106
107 server.listen(common.PORT, function() {
108   console.log('expecting %d bytes', bytesExpected);
109   makeRequest();
110 });
111
112 process.on('exit', function() {
113   console.error('got %d bytes', uploadCount);
114   assert.equal(uploadCount, bytesExpected);
115 });