Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect when the server ready #1963

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 41 additions & 29 deletions test/run_pass/test_net_3.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ var assert = require('assert');


var port = 22703;
var limit = 200;
var limit = 233;
var server = net.createServer();

server.listen({ port: port });
server.listen({ port: port }, startTesting);

server.on('connection', function(socket) {
var i = 0;
var writing = function() {
var ok;
do {
ok = socket.write("" + (i % 10));
ok = socket.write('' + (i % 10));
if (++i == limit) {
socket.end();
ok = false;
Expand All @@ -40,47 +40,59 @@ server.on('connection', function(socket) {
writing();
});


var msg1 = '';
var socket1 = net.createConnection(port);
var msg2 = '';
var msg3 = '';
var msg4 = '';
var endCount = 0;
var connectListenerCheck = false;

socket1.on('data', function(data) {
msg1 += data;
});
function endCallback() {
endCount += 1;
if (endCount === 4) {
server.close();
}
}

function startTesting() {
var socket1 = net.createConnection(port);

var msg2 = '';
var socket2 = net.createConnection({port: port});
socket1.on('data', function(data) {
msg1 += data;
});
socket1.on('end', endCallback);

socket2.on('data', function(data) {
msg2 += data;
});

var socket2 = net.createConnection({port: port});

var msg3 = '';
var socket3 = net.createConnection({port: port, host: '127.0.0.1'});
socket2.on('data', function(data) {
msg2 += data;
});
socket2.on('end', endCallback);

socket3.on('data', function(data) {
msg3 += data;
});

var socket3 = net.createConnection({port: port, host: '127.0.0.1'});

var msg4 = '';
var connectListenerCheck = false;
var socket4 = net.createConnection({port: port}, function() {
connectListenerCheck = true;
});
socket3.on('data', function(data) {
msg3 += data;
});
socket3.on('end', endCallback);

socket4.on('data', function(data) {
msg4 += data;
});

socket4.on('end', function() {
server.close();
});
var socket4 = net.createConnection({port: port}, function() {
connectListenerCheck = true;
});

socket4.on('data', function(data) {
msg4 += data;
});

socket4.on('end', endCallback);
}

process.on('exit', function(code) {
assert.equal(code, 0);
assert.equal(endCount, 4);
assert.equal(msg1.length, limit);
assert.equal(msg2.length, limit);
assert.equal(msg3.length, limit);
Expand Down
7 changes: 4 additions & 3 deletions test/run_pass/test_net_8.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ server.on('connection', function(socket) {
socket.end(data);
});
socket.on('close', function() {
server.close();
});
});

Expand All @@ -46,6 +45,7 @@ sock2.connect(port, 'localhost');

sock1.on('data', function(data) {
msg += data;
sendSock2Data();
});

sock1.on('end', function() {
Expand All @@ -58,15 +58,16 @@ sock2.on('data', function(data) {

sock2.on('end', function() {
sock2.end();
server.close();
});

timers.setTimeout(function() {
sock1.write('1');
}, 1000);

timers.setTimeout(function() {
function sendSock2Data() {
sock2.write('2');
}, 2000);
}

process.on('exit', function(code) {
assert.equal(code, 0);
Expand Down
48 changes: 25 additions & 23 deletions test/run_pass/test_net_http_get.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var server = http.createServer(function (req, res) {

});

server.listen(3005,5);
server.listen(3005,5, startTesting);


// 1. GET req
Expand All @@ -67,32 +67,34 @@ var getResponseHandler = function (res) {
});
};

http.get(options, getResponseHandler);
function startTesting() {
http.get(options, getResponseHandler);


// 2. close server req
var finalMsg = 'close server';
var finalOptions = {
method : 'POST',
port : 3005,
headers : {'Content-Length': finalMsg.length}
};
// 2. close server req
var finalMsg = 'close server';
var finalOptions = {
method : 'POST',
port : 3005,
headers : {'Content-Length': finalMsg.length}
};

var finalResponseHandler = function (res) {
var res_body = '';
var finalResponseHandler = function (res) {
var res_body = '';

assert.equal(200, res.statusCode);
assert.equal(200, res.statusCode);

var endHandler = function(){
assert.equal(finalMsg, res_body);
};
res.on('end', endHandler);
var endHandler = function(){
assert.equal(finalMsg, res_body);
};
res.on('end', endHandler);

res.on('data', function(chunk){
res_body += chunk.toString();
});
};
res.on('data', function(chunk){
res_body += chunk.toString();
});
};

var finalReq = http.request(finalOptions, finalResponseHandler);
finalReq.write(finalMsg);
finalReq.end();
var finalReq = http.request(finalOptions, finalResponseHandler);
finalReq.write(finalMsg);
finalReq.end();
}
Loading