|
| 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 | +if (!process.versions.openssl) { |
| 23 | + console.error('Skipping because node compiled without OpenSSL.'); |
| 24 | + process.exit(0); |
| 25 | +} |
| 26 | + |
| 27 | +var common = require('../common'); |
| 28 | +var assert = require('assert'); |
| 29 | +var cluster = require('cluster'); |
| 30 | +var tls = require('tls'); |
| 31 | +var fs = require('fs'); |
| 32 | +var join = require('path').join; |
| 33 | + |
| 34 | +var workerCount = 4; |
| 35 | +var expectedReqCount = 16; |
| 36 | + |
| 37 | +if (cluster.isMaster) { |
| 38 | + var reusedCount = 0; |
| 39 | + var reqCount = 0; |
| 40 | + var lastSession = null; |
| 41 | + var shootOnce = false; |
| 42 | + |
| 43 | + function shoot() { |
| 44 | + console.error('[master] connecting'); |
| 45 | + var c = tls.connect(common.PORT, { |
| 46 | + session: lastSession, |
| 47 | + rejectUnauthorized: false |
| 48 | + }, function() { |
| 49 | + lastSession = c.getSession(); |
| 50 | + c.end(); |
| 51 | + |
| 52 | + if (++reqCount === expectedReqCount) { |
| 53 | + Object.keys(cluster.workers).forEach(function(id) { |
| 54 | + cluster.workers[id].send('die'); |
| 55 | + }); |
| 56 | + } else { |
| 57 | + shoot(); |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + function fork() { |
| 63 | + var worker = cluster.fork(); |
| 64 | + var workerReqCount = 0; |
| 65 | + worker.on('message', function(msg) { |
| 66 | + console.error('[master] got %j', msg); |
| 67 | + if (msg === 'reused') { |
| 68 | + ++reusedCount; |
| 69 | + } else if (msg === 'listening' && !shootOnce) { |
| 70 | + shootOnce = true; |
| 71 | + shoot(); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + worker.on('exit', function() { |
| 76 | + console.error('[master] worker died'); |
| 77 | + }); |
| 78 | + } |
| 79 | + for (var i = 0; i < workerCount; i++) { |
| 80 | + fork(); |
| 81 | + } |
| 82 | + |
| 83 | + process.on('exit', function() { |
| 84 | + assert.equal(reqCount, expectedReqCount); |
| 85 | + assert.equal(reusedCount + 1, reqCount); |
| 86 | + }); |
| 87 | + return; |
| 88 | +} |
| 89 | + |
| 90 | +var keyFile = join(common.fixturesDir, 'agent.key'); |
| 91 | +var certFile = join(common.fixturesDir, 'agent.crt'); |
| 92 | +var key = fs.readFileSync(keyFile); |
| 93 | +var cert = fs.readFileSync(certFile); |
| 94 | +var options = { |
| 95 | + key: key, |
| 96 | + cert: cert |
| 97 | +}; |
| 98 | + |
| 99 | +var server = tls.createServer(options, function(c) { |
| 100 | + if (c.isSessionReused()) { |
| 101 | + process.send('reused'); |
| 102 | + } else { |
| 103 | + process.send('not-reused'); |
| 104 | + } |
| 105 | + c.end(); |
| 106 | +}); |
| 107 | + |
| 108 | +server.listen(common.PORT, function() { |
| 109 | + process.send('listening'); |
| 110 | +}); |
| 111 | + |
| 112 | +process.on('message', function listener(msg) { |
| 113 | + console.error('[worker] got %j', msg); |
| 114 | + if (msg === 'die') { |
| 115 | + server.close(function() { |
| 116 | + console.error('[worker] server close'); |
| 117 | + |
| 118 | + process.exit(); |
| 119 | + }); |
| 120 | + } |
| 121 | +}); |
| 122 | + |
| 123 | +process.on('exit', function() { |
| 124 | + console.error('[worker] exit'); |
| 125 | +}); |
0 commit comments