2010-10-12 06:19:44 +02:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
|
|
|
var net = require('net');
|
|
|
|
|
|
|
|
var tcpPort = common.PORT;
|
2010-12-16 00:47:02 +01:00
|
|
|
var fooWritten = false;
|
|
|
|
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() {
|
|
|
|
assert.equal('foobar', buf);
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2010-12-16 00:47:02 +01:00
|
|
|
tcp.listen(common.PORT, function () {
|
2010-10-12 06:19:44 +02:00
|
|
|
var socket = net.Stream();
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
console.log('Connecting to socket');
|
2010-10-12 06:19:44 +02:00
|
|
|
|
|
|
|
socket.connect(tcpPort);
|
|
|
|
|
2010-12-16 00:47:02 +01:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
socket.on('connect', 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
|
|
|
});
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('opening', socket.readyState);
|
2010-10-12 06:19:44 +02:00
|
|
|
|
2010-12-16 00:47:02 +01:00
|
|
|
var r = socket.write('foo', function () {
|
|
|
|
fooWritten = true;
|
|
|
|
assert.ok(connectHappened);
|
|
|
|
console.error("foo written");
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(false, r);
|
2010-12-04 23:45:52 +01:00
|
|
|
socket.end('bar');
|
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
|
|
|
});
|
|
|
|
|
|
|
|
process.on('exit', function () {
|
|
|
|
assert.ok(connectHappened);
|
|
|
|
assert.ok(fooWritten);
|
|
|
|
});
|