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

68 lines
1.3 KiB
JavaScript
Raw Normal View History

2010-02-25 14:36:17 +08:00
process.mixin(require("../common"));
tcp = require("tcp");
2009-08-24 22:51:37 +08:00
N = 200;
2009-09-28 22:08:09 +08:00
server = tcp.createServer(function (connection) {
function write (j) {
2009-08-27 04:03:19 +08:00
if (j >= N) {
2009-08-19 23:28:41 +08:00
connection.close();
return;
}
setTimeout(function () {
connection.write("C");
write(j+1);
}, 10);
}
write(0);
});
server.listen(PORT);
recv = "";
chars_recved = 0;
2009-09-28 22:08:09 +08:00
client = tcp.createConnection(PORT);
2009-08-27 00:22:00 +08:00
client.setEncoding("ascii");
client.addListener("data", function (d) {
2009-08-27 00:22:00 +08:00
print(d);
recv += d;
});
2009-08-27 00:22:00 +08:00
setTimeout(function () {
2009-08-27 04:03:19 +08:00
chars_recved = recv.length;
2009-08-27 00:22:00 +08:00
puts("pause at: " + chars_recved);
assert.equal(true, chars_recved > 1);
client.pause();
setTimeout(function () {
2009-08-27 00:22:00 +08:00
puts("resume at: " + chars_recved);
assert.equal(chars_recved, recv.length);
client.resume();
2009-08-27 00:22:00 +08:00
setTimeout(function () {
2009-08-27 04:03:19 +08:00
chars_recved = recv.length;
2009-08-27 00:22:00 +08:00
puts("pause at: " + chars_recved);
client.pause();
setTimeout(function () {
2009-08-27 00:22:00 +08:00
puts("resume at: " + chars_recved);
assert.equal(chars_recved, recv.length);
client.resume();
}, 500);
}, 500);
}, 500);
2009-08-27 00:22:00 +08:00
}, 500);
client.addListener("end", function () {
2009-08-27 00:22:00 +08:00
server.close();
client.close();
});
process.addListener("exit", function () {
assert.equal(N, recv.length);
debug("Exit");
});