2010-10-12 06:19:44 +02:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
|
|
|
var net = require('net');
|
|
|
|
|
|
|
|
var tcpPort = common.PORT;
|
2011-10-15 03:10:53 +02:00
|
|
|
var dataWritten = false;
|
2010-12-16 00:47:02 +01:00
|
|
|
var connectHappened = false;
|
2010-10-12 06:19:44 +02:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
var tcp = net.Server(function(s) {
|
2010-10-12 06:19:44 +02:00
|
|
|
tcp.close();
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
console.log('tcp server connection');
|
2010-10-12 06:19:44 +02:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
var buf = '';
|
|
|
|
s.on('data', function(d) {
|
2010-10-12 06:19:44 +02:00
|
|
|
buf += d;
|
|
|
|
});
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
s.on('end', function() {
|
2012-12-13 18:51:31 +01:00
|
|
|
console.error('SERVER: end', buf.toString());
|
2011-10-15 03:10:53 +02:00
|
|
|
assert.equal(buf, "L'État, c'est moi");
|
2010-12-04 23:45:52 +01:00
|
|
|
console.log('tcp socket disconnect');
|
2010-10-12 06:19:44 +02:00
|
|
|
s.end();
|
|
|
|
});
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
s.on('error', function(e) {
|
|
|
|
console.log('tcp server-side error: ' + e.message);
|
2010-10-12 06:19:44 +02:00
|
|
|
process.exit(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2011-10-05 00:08:18 +02:00
|
|
|
tcp.listen(common.PORT, function() {
|
2012-12-13 18:51:31 +01:00
|
|
|
var socket = net.Stream({ highWaterMark: 0 });
|
2010-10-12 06:19:44 +02:00
|
|
|
|
2011-07-02 09:18:36 +02:00
|
|
|
console.log('Connecting to socket ');
|
2010-10-12 06:19:44 +02:00
|
|
|
|
2010-12-16 00:57:13 +01:00
|
|
|
socket.connect(tcpPort, function() {
|
2010-10-12 06:19:44 +02:00
|
|
|
console.log('socket connected');
|
2010-12-16 00:47:02 +01:00
|
|
|
connectHappened = true;
|
2010-10-12 06:19:44 +02:00
|
|
|
});
|
|
|
|
|
2011-07-02 09:18:36 +02:00
|
|
|
console.log('_connecting = ' + socket._connecting);
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('opening', socket.readyState);
|
2010-10-12 06:19:44 +02:00
|
|
|
|
2012-01-14 02:13:22 +01:00
|
|
|
// Make sure that anything besides a buffer or a string throws.
|
2012-02-19 00:01:35 +01:00
|
|
|
[null,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
undefined,
|
|
|
|
1,
|
|
|
|
1.0,
|
|
|
|
1 / 0,
|
|
|
|
+Infinity,
|
|
|
|
-Infinity,
|
|
|
|
[],
|
|
|
|
{}
|
2012-01-14 02:13:22 +01:00
|
|
|
].forEach(function(v) {
|
|
|
|
function f() {
|
2012-12-13 18:51:31 +01:00
|
|
|
console.error('write', v);
|
2012-01-14 02:13:22 +01:00
|
|
|
socket.write(v);
|
|
|
|
}
|
|
|
|
assert.throws(f, TypeError);
|
|
|
|
});
|
|
|
|
|
2011-10-15 03:10:53 +02:00
|
|
|
// Write a string that contains a multi-byte character sequence to test that
|
|
|
|
// `bytesWritten` is incremented with the # of bytes, not # of characters.
|
|
|
|
var a = "L'État, c'est ";
|
2012-01-17 19:43:34 +01:00
|
|
|
var b = 'moi';
|
2011-10-15 03:10:53 +02:00
|
|
|
|
|
|
|
// We're still connecting at this point so the datagram is first pushed onto
|
|
|
|
// the connect queue. Make sure that it's not added to `bytesWritten` again
|
|
|
|
// when the actual write happens.
|
2012-12-13 18:51:31 +01:00
|
|
|
var r = socket.write(a, function(er) {
|
|
|
|
console.error('write cb');
|
2011-10-15 03:10:53 +02:00
|
|
|
dataWritten = true;
|
2010-12-16 00:47:02 +01:00
|
|
|
assert.ok(connectHappened);
|
2012-12-13 18:51:31 +01:00
|
|
|
console.error('socket.bytesWritten', socket.bytesWritten);
|
|
|
|
//assert.equal(socket.bytesWritten, Buffer(a + b).length);
|
2011-10-15 03:10:53 +02:00
|
|
|
console.error('data written');
|
2010-12-16 00:47:02 +01:00
|
|
|
});
|
2012-12-13 18:51:31 +01:00
|
|
|
console.error('socket.bytesWritten', socket.bytesWritten);
|
|
|
|
console.error('write returned', r);
|
|
|
|
|
2011-10-15 03:10:53 +02:00
|
|
|
assert.equal(socket.bytesWritten, Buffer(a).length);
|
2010-12-16 00:47:02 +01:00
|
|
|
|
|
|
|
assert.equal(false, r);
|
2011-10-15 03:10:53 +02:00
|
|
|
socket.end(b);
|
2010-10-12 06:19:44 +02:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('opening', socket.readyState);
|
2010-12-16 00:47:02 +01:00
|
|
|
});
|
|
|
|
|
2011-10-05 00:08:18 +02:00
|
|
|
process.on('exit', function() {
|
2010-12-16 00:47:02 +01:00
|
|
|
assert.ok(connectHappened);
|
2011-10-15 03:10:53 +02:00
|
|
|
assert.ok(dataWritten);
|
2010-12-16 00:47:02 +01:00
|
|
|
});
|