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

69 lines
1.4 KiB
JavaScript
Raw Normal View History

2010-12-03 09:03:18 +08:00
var common = require('../common');
var assert = require('assert');
2010-12-03 09:03:18 +08:00
var net = require('net');
2010-12-05 08:11:57 +08:00
2010-08-11 08:37:30 +08:00
var N = 200;
2010-12-03 09:03:18 +08:00
var recv = '', chars_recved = 0;
2010-12-05 08:11:57 +08:00
var server = net.createServer(function(connection) {
2010-12-03 09:03:18 +08:00
function write(j) {
if (j >= N) {
connection.end();
return;
}
2010-12-03 09:03:18 +08:00
setTimeout(function() {
connection.write('C');
write(j + 1);
}, 10);
}
write(0);
});
2010-12-05 08:11:57 +08:00
2010-12-03 09:03:18 +08:00
server.on('listening', function() {
2010-12-05 08:11:57 +08:00
var client = net.createConnection(common.PORT);
2010-12-03 09:03:18 +08:00
client.setEncoding('ascii');
client.on('data', function(d) {
2010-12-03 09:03:18 +08:00
common.print(d);
recv += d;
2010-08-11 08:37:30 +08:00
});
2010-12-03 09:03:18 +08:00
setTimeout(function() {
2010-08-11 08:37:30 +08:00
chars_recved = recv.length;
2010-12-03 09:03:18 +08:00
console.log('pause at: ' + chars_recved);
2010-08-11 08:37:30 +08:00
assert.equal(true, chars_recved > 1);
client.pause();
2010-12-03 09:03:18 +08:00
setTimeout(function() {
console.log('resume at: ' + chars_recved);
2010-08-11 08:37:30 +08:00
assert.equal(chars_recved, recv.length);
client.resume();
2010-12-03 09:03:18 +08:00
setTimeout(function() {
2010-08-11 08:37:30 +08:00
chars_recved = recv.length;
2010-12-03 09:03:18 +08:00
console.log('pause at: ' + chars_recved);
2010-08-11 08:37:30 +08:00
client.pause();
2010-12-03 09:03:18 +08:00
setTimeout(function() {
console.log('resume at: ' + chars_recved);
2010-08-11 08:37:30 +08:00
assert.equal(chars_recved, recv.length);
client.resume();
}, 500);
}, 500);
}, 500);
}, 500);
client.on('end', function() {
2010-08-11 08:37:30 +08:00
server.close();
client.end();
});
});
2010-08-11 08:37:30 +08:00
server.listen(common.PORT);
process.on('exit', function() {
assert.equal(N, recv.length);
2010-12-03 09:03:18 +08:00
common.debug('Exit');
});