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

58 lines
1.2 KiB
JavaScript
Raw Normal View History

2010-12-05 07:20:34 +08:00
var common = require('../common');
var assert = require('assert');;
2010-12-03 09:03:18 +08:00
net = require('net');
N = 160 * 1024; // 30kb
2010-08-13 23:54:40 +08:00
chars_recved = 0;
npauses = 0;
2010-12-03 09:03:18 +08:00
console.log('build big string');
var body = '';
for (var i = 0; i < N; i++) {
2010-12-03 09:03:18 +08:00
body += 'C';
}
2010-12-03 09:03:18 +08:00
console.log('start server on port ' + common.PORT);
2010-12-03 09:03:18 +08:00
server = net.createServer(function(connection) {
connection.addListener('connect', function() {
assert.equal(false, connection.write(body));
connection.end();
});
});
2010-12-03 09:03:18 +08:00
server.listen(common.PORT, function() {
2010-08-13 23:54:40 +08:00
var paused = false;
client = net.createConnection(common.PORT);
2010-12-03 09:03:18 +08:00
client.setEncoding('ascii');
client.addListener('data', function(d) {
2010-08-13 23:54:40 +08:00
chars_recved += d.length;
2010-12-03 09:03:18 +08:00
console.log('got ' + chars_recved);
2010-08-13 23:54:40 +08:00
if (!paused) {
client.pause();
npauses += 1;
paused = true;
2010-12-03 09:03:18 +08:00
console.log('pause');
2010-08-13 23:54:40 +08:00
x = chars_recved;
2010-12-03 09:03:18 +08:00
setTimeout(function() {
2010-08-13 23:54:40 +08:00
assert.equal(chars_recved, x);
client.resume();
2010-12-03 09:03:18 +08:00
console.log('resume');
2010-08-13 23:54:40 +08:00
paused = false;
}, 100);
}
});
2010-12-03 09:03:18 +08:00
client.addListener('end', function() {
2010-08-13 23:54:40 +08:00
server.close();
client.end();
});
});
2009-08-27 00:22:00 +08:00
2010-08-13 23:54:40 +08:00
2010-12-03 09:03:18 +08:00
process.addListener('exit', function() {
assert.equal(N, chars_recved);
assert.equal(true, npauses > 2);
});