node/test/pummel/test-tls-throttle.js

68 lines
1.4 KiB
JavaScript
Raw Normal View History

'use strict';
2011-02-01 00:38:05 +00:00
// Server sends a large string. Client counts bytes and pauses every few
// seconds. Makes sure that pause and resume work properly.
const common = require('../common');
const assert = require('assert');
if (!common.hasCrypto) {
common.skip('missing crypto');
return;
}
const tls = require('tls');
const fs = require('fs');
2011-02-01 00:38:05 +00:00
process.stdout.write('build body...');
const body = 'hello world\n'.repeat(1024 * 1024);
2011-02-01 00:38:05 +00:00
process.stdout.write('done\n');
const options = {
2011-02-01 00:38:05 +00:00
key: fs.readFileSync(common.fixturesDir + '/keys/agent2-key.pem'),
cert: fs.readFileSync(common.fixturesDir + '/keys/agent2-cert.pem')
};
const server = tls.Server(options, common.mustCall(function(socket) {
2011-02-01 00:38:05 +00:00
socket.end(body);
}));
2011-02-01 00:38:05 +00:00
let recvCount = 0;
2011-02-01 00:38:05 +00:00
server.listen(common.PORT, function() {
const client = tls.connect({
port: common.PORT,
rejectUnauthorized: false
});
2011-02-01 00:38:05 +00:00
client.on('data', function(d) {
process.stdout.write('.');
2011-02-01 00:38:05 +00:00
recvCount += d.length;
client.pause();
process.nextTick(function() {
2011-02-01 00:38:05 +00:00
client.resume();
});
});
client.on('close', function() {
console.error('close');
2011-02-01 00:38:05 +00:00
server.close();
clearTimeout(timeout);
});
});
function displayCounts() {
console.log('body.length: %d', body.length);
console.log(' recvCount: %d', recvCount);
}
const timeout = setTimeout(displayCounts, 10 * 1000);
2011-02-01 00:38:05 +00:00
process.on('exit', function() {
displayCounts();
assert.strictEqual(body.length, recvCount);
2011-02-01 00:38:05 +00:00
});