From f79b58a3f6dccf3022b74e98306f38b5749b3f04 Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Sat, 30 Jan 2021 06:54:18 -0800 Subject: [PATCH] Connect when the server ready ==3059== Memcheck, a memory error detector ==3059== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. ==3059== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info ==3059== Command: /root/work_space/iotjs/build/x86_64-linux/debug/bin/iotjs /root/work_space/iotjs/test/run_pass/test_net_8.js ==3059== AssertionError: {"actual":"1","expected":"12","operator":"=="} AssertionError: {"actual":1,"expected":0,"operator":"=="} ==3059== ==3059== HEAP SUMMARY: ==3059== in use at exit: 0 bytes in 0 blocks ==3059== total heap usage: 131 allocs, 131 frees, 314,345 bytes allocated ==3059== According to this, the server on close event triggered multiple times by close function wait all four connection to end IoT.js-DCO-1.0-Signed-off-by: Yonggang Luo luoyonggang@gmail.com --- test/run_pass/test_net_3.js | 70 ++++--- test/run_pass/test_net_8.js | 7 +- test/run_pass/test_net_http_get.js | 48 ++--- .../test_net_http_request_response.js | 172 ++++++++++-------- test/run_pass/test_net_http_server.js | 161 ++++++++-------- test/run_pass/test_net_https_server.js | 9 +- test/run_pass/test_tls_3.js | 75 ++++---- test/run_pass/test_tls_4.js | 79 ++++---- 8 files changed, 338 insertions(+), 283 deletions(-) diff --git a/test/run_pass/test_net_3.js b/test/run_pass/test_net_3.js index 1b34918d00..6d68c4fd67 100644 --- a/test/run_pass/test_net_3.js +++ b/test/run_pass/test_net_3.js @@ -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; @@ -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); diff --git a/test/run_pass/test_net_8.js b/test/run_pass/test_net_8.js index f83db72134..cb06caf2ea 100644 --- a/test/run_pass/test_net_8.js +++ b/test/run_pass/test_net_8.js @@ -32,7 +32,6 @@ server.on('connection', function(socket) { socket.end(data); }); socket.on('close', function() { - server.close(); }); }); @@ -46,6 +45,7 @@ sock2.connect(port, 'localhost'); sock1.on('data', function(data) { msg += data; + sendSock2Data(); }); sock1.on('end', function() { @@ -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); diff --git a/test/run_pass/test_net_http_get.js b/test/run_pass/test_net_http_get.js index 7f9ff4d040..be64727dec 100644 --- a/test/run_pass/test_net_http_get.js +++ b/test/run_pass/test_net_http_get.js @@ -42,7 +42,7 @@ var server = http.createServer(function (req, res) { }); -server.listen(3005,5); +server.listen(3005,5, startTesting); // 1. GET req @@ -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(); +} diff --git a/test/run_pass/test_net_http_request_response.js b/test/run_pass/test_net_http_request_response.js index e7615b7bf9..b388ec47d4 100644 --- a/test/run_pass/test_net_http_request_response.js +++ b/test/run_pass/test_net_http_request_response.js @@ -21,12 +21,15 @@ var net = require('net'); var message = 'Hello IoT.js'; // Options for further requests. -var options = { - method: 'POST', - port: 3005, - path: '/', - headers : {'Content-Length': message.length} -}; +function createOptions(port) { + return { + method: 'POST', + port: port, + path: '/', + headers : {'Content-Length': message.length} + }; + +} var server1 = http.createServer(function(request, response) { var str = ''; @@ -40,38 +43,41 @@ var server1 = http.createServer(function(request, response) { response.end(); }); }); -server1.listen(3005, 5); +server1.listen(3005, 5, startRequest1); // Simple request with valid utf-8 message. var isRequest1Finished = false; -var request1 = http.request(options, function(response) { - var str = ''; - - response.on('data', function(chunk) { - str += chunk.toString(); - }); - - response.on('end', function() { - isRequest1Finished = true; - server1.close(); +function startRequest1() { + var request1 = http.request(createOptions(3005), function(response) { + var str = ''; + + response.on('data', function(chunk) { + str += chunk.toString(); + }); + + response.on('end', function() { + isRequest1Finished = true; + server1.close(); + }); }); -}); -request1.end(message, 'utf-8'); + request1.end(message, 'utf-8'); +} var server2 = http.createServer(function(request, response) { response.end(); }); -server2.listen(3006, 5); +server2.listen(3006, 5, startRequest2); // Simple request with end callback. var isRequest2Finished = false; -options.port = 3006; -var request2 = http.request(options); -request2.end(message, function() { - server2.close(); - isRequest2Finished = true; -}); +function startRequest2() { + var request2 = http.request(createOptions(3006)); + request2.end(message, function() { + server2.close(); + isRequest2Finished = true; + }); +} var server3 = http.createServer(function(request, response) { @@ -87,24 +93,25 @@ var server3 = http.createServer(function(request, response) { response.end(); }); }); -server3.listen(3007, 5); +server3.listen(3007, 5, startRequest3); // Simple request with buffer chunk as message parameter. -options.port = 3007; var isRequest3Finished = false; -var request3 = http.request(options, function(response) { - var str = ''; - - response.on('data', function(chunk) { - str += chunk; +function startRequest3() { + var request3 = http.request(createOptions(3007), function(response) { + var str = ''; + + response.on('data', function(chunk) { + str += chunk; + }); + + response.on('end', function() { + isRequest3Finished = true; + server3.close(); + }); }); - - response.on('end', function() { - isRequest3Finished = true; - server3.close(); - }); -}); -request3.end(new Buffer(message)); + request3.end(new Buffer(message)); +} // This test is to make sure that when the HTTP server @@ -113,21 +120,23 @@ var server4 = http.createServer(function(request, response) { response.writeHead(200); response.end(); }); -server4.listen(3008, 5); +server4.listen(3008, 5, startRequest4); var isRequest4Finished = false; -var request4 = http.request({ - method: 'HEAD', - port: 3008, - path: '/' -}, function(response) { - response.on('end', function() { - isRequest4Finished = true; - assert.equal(response.statusCode, 200); - server4.close(); +function startRequest4() { + var request4 = http.request({ + method: 'HEAD', + port: 3008, + path: '/' + }, function(response) { + response.on('end', function() { + isRequest4Finished = true; + assert.equal(response.statusCode, 200); + server4.close(); + }); }); -}); -request4.end(); + request4.end(); +} // Write a header twice in the server response. @@ -147,20 +156,22 @@ var server5 = http.createServer(function(request, response) { response.end(); }); }); -server5.listen(3009, 5); +server5.listen(3009, 5, startRequest5); -options.port = 3009; -options.headers = null; var isRequest5Finished = false; -var request5 = http.request(options, function(response) { - response.on('end', function() { - isRequest5Finished = true; - assert.equal(response.headers['Connection'], 'close2'); - assert.equal(response.headers['Head'], 'Value'); - server5.close(); +function startRequest5() { + var options = createOptions(3009) + options.headers = null; + var request5 = http.request(options, function(response) { + response.on('end', function() { + isRequest5Finished = true; + assert.equal(response.headers['Connection'], 'close2'); + assert.equal(response.headers['Head'], 'Value'); + server5.close(); + }); }); -}); -request5.end(); + request5.end(); +} // Test the IncomingMessage read function. @@ -169,24 +180,25 @@ var server6 = http.createServer(function(request, response) { response.end('ok'); server6.close(); }); -}).listen(8080, 5); - -var readRequest = http.request({ - host: '127.0.0.1', - port: 8080, - path: '/', - method: 'GET' -}); -readRequest.end(); - -readRequest.on('response', function(incomingMessage) { - incomingMessage.on('readable', function() { - var inc = incomingMessage.read(); - assert.equal(inc instanceof Buffer, true); - assert.assert(inc.toString('utf8').length > 0); +}).listen(8080, 5, startRequest6); + +function startRequest6() { + var readRequest = http.request({ + host: '127.0.0.1', + port: 8080, + path: '/', + method: 'GET' }); -}); - + readRequest.end(); + + readRequest.on('response', function(incomingMessage) { + incomingMessage.on('readable', function() { + var inc = incomingMessage.read(); + assert.equal(inc instanceof Buffer, true); + assert.assert(inc.toString('utf8').length > 0); + }); + }); +} process.on('exit', function() { assert.equal(isRequest1Finished, true); diff --git a/test/run_pass/test_net_http_server.js b/test/run_pass/test_net_http_server.js index 2033f115af..0945c87ea3 100644 --- a/test/run_pass/test_net_http_server.js +++ b/test/run_pass/test_net_http_server.js @@ -64,7 +64,7 @@ server.on('close', function() { serverCloseEvent++; }); -server.listen(3001, 3); +server.listen(3001, 3, startTestingPost); // client side code @@ -82,101 +82,106 @@ var options = { }; -var postResponseHandler = function (res) { - var res_body = ''; +function startTestingPost() { + var postResponseHandler = function (res) { + var res_body = ''; - assert.equal(200, res.statusCode); - var endHandler = function(){ - assert.equal(msg, res_body); - responseCheck += '1'; + assert.equal(200, res.statusCode); + var endHandler = function(){ + assert.equal(msg, res_body); + responseCheck += '1'; + startTestingGet(); + }; + res.on('end', endHandler); + + res.on('data', function(chunk){ + res_body += chunk.toString(); + }); }; - res.on('end', endHandler); - res.on('data', function(chunk){ - res_body += chunk.toString(); + var req = http.request(options, postResponseHandler); + req.on('response', function() { + responseEvent++; }); -}; - -var req = http.request(options, postResponseHandler); -req.on('response', function() { - responseEvent++; -}); -req.on('socket', function() { - socketEvent++; -}); -req.write(msg); -req.end(); - + req.on('socket', function() { + socketEvent++; + }); + req.write(msg); + req.end(); +} + +function startTestingGet() { + // 2. GET req + options = { + method : 'GET', + port : 3001 + }; -// 2. GET req -options = { - method : 'GET', - port : 3001 -}; + var getResponseHandler = function (res) { + var res_body = ''; -var getResponseHandler = function (res) { - var res_body = ''; + assert.equal(200, res.statusCode); - assert.equal(200, res.statusCode); + var endHandler = function(){ + // GET msg, no received body + assert.equal('', res_body); + responseCheck += '2'; + startTestingFinal(); + }; + res.on('end', endHandler); - var endHandler = function(){ - // GET msg, no received body - assert.equal('', res_body); - responseCheck += '2'; + res.on('data', function(chunk){ + res_body += chunk.toString(); + }); }; - res.on('end', endHandler); - - res.on('data', function(chunk){ - res_body += chunk.toString(); - }); -}; -var getReq = http.request(options, getResponseHandler); -getReq.on('response', function() { - responseEvent++; -}); -getReq.on('socket', function() { - socketEvent++; -}); -getReq.end(); - + var getReq = http.request(options, getResponseHandler); + getReq.on('response', function() { + responseEvent++; + }); + getReq.on('socket', function() { + socketEvent++; + }); + getReq.end(); +} + +function startTestingFinal() { + // 3. close server req + var finalMsg = 'close server'; + var finalOptions = { + method : 'POST', + port : 3001, + rejectUnauthorized: false, + headers : {'Content-Length': finalMsg.length} + }; + var finalResponseHandler = function (res) { + var res_body = ''; -// 3. close server req -var finalMsg = 'close server'; -var finalOptions = { - method : 'POST', - port : 3001, - rejectUnauthorized: false, - headers : {'Content-Length': finalMsg.length} -}; + assert.equal(200, res.statusCode); -var finalResponseHandler = function (res) { - var res_body = ''; + var endHandler = function(){ + assert.equal(finalMsg, res_body); + responseCheck += '3'; + }; + res.on('end', endHandler); - assert.equal(200, res.statusCode); - - var endHandler = function(){ - assert.equal(finalMsg, res_body); - responseCheck += '3'; + res.on('data', function(chunk){ + res_body += chunk.toString(); + }); }; - res.on('end', endHandler); - res.on('data', function(chunk){ - res_body += chunk.toString(); + var finalReq = http.request(finalOptions, finalResponseHandler); + finalReq.on('response', function() { + responseEvent++; }); -}; - -var finalReq = http.request(finalOptions, finalResponseHandler); -finalReq.on('response', function() { - responseEvent++; -}); -finalReq.on('socket', function() { - socketEvent++; -}); -finalReq.write(finalMsg); -finalReq.end(); + finalReq.on('socket', function() { + socketEvent++; + }); + finalReq.write(finalMsg); + finalReq.end(); +} // Create server without requestListener. var server2 = http.createServer(); diff --git a/test/run_pass/test_net_https_server.js b/test/run_pass/test_net_https_server.js index d1120088e9..d161f8b9c5 100644 --- a/test/run_pass/test_net_https_server.js +++ b/test/run_pass/test_net_https_server.js @@ -25,7 +25,7 @@ var server_options = { var server = https.createServer(server_options, function(req, res) { res.writeHead(200); res.end('hello world\n'); -}).listen(8000); +}).listen(8000, startTesting); var client_options = { @@ -41,14 +41,15 @@ var responseHandler = function (res) { var endHandler = function(){ assert.equal(res_body, 'hello world\n'); + server.close(); }; res.on('end', endHandler); res.on('data', function(chunk){ res_body += chunk.toString(); }); - - server.close(); } -https.get(client_options, responseHandler); +function startTesting() { + https.get(client_options, responseHandler); +} diff --git a/test/run_pass/test_tls_3.js b/test/run_pass/test_tls_3.js index f96768a052..e92567a500 100644 --- a/test/run_pass/test_tls_3.js +++ b/test/run_pass/test_tls_3.js @@ -24,6 +24,7 @@ var server_message = ''; var server_closed = false; var server_handshake_done = false; var handshake_done = false; +var endCount = 0; var port = 8080; @@ -31,7 +32,7 @@ var server_options = { key: fs.readFileSync(process.cwd() + '/resources/my_key.key').toString(), cert: fs.readFileSync(process.cwd() + '/resources/my_crt.crt'), rejectUnauthorized: false, - isServer: true + isServer: true, }; var server = tls.createServer(server_options, function(socket) { @@ -41,7 +42,7 @@ var server = tls.createServer(server_options, function(socket) { client_message = data.toString(); }); -}).listen(port, function() {}); +}).listen(port, startTesting); server.on('secureConnection', function() { server_handshake_done = true; @@ -51,47 +52,57 @@ server.on('close', function() { server_closed = true; }); -var socket1 = tls.connect(port); +function endCallback() { + endCount += 1; + if (endCount === 3) { + server.close(); + } +} -socket1.on('secureConnect', function() { - handshake_done = true; -}); +function startTesting() { + var socket1 = tls.connect(port); -socket1.on('data', function(data) { - server_message = data.toString(); - socket1.write('Client hello'); - socket1.end(); -}); + socket1.on('secureConnect', function() { + handshake_done = true; + }); -var socket2 = tls.connect(port, 'localhost'); + socket1.on('data', function(data) { + server_message = data.toString(); + socket1.write('Client hello'); + socket1.end(); + }); + socket1.on('end', endCallback); -socket2.on('secureConnect', function() { - handshake_done = true; -}); + var socket2 = tls.connect(port, 'localhost'); -socket2.on('data', function(data) { - server_message = data.toString(); - socket2.write('Client hello'); - socket2.end(); -}); + socket2.on('secureConnect', function() { + handshake_done = true; + }); -var socket3 = tls.connect(port, function() {}); + socket2.on('data', function(data) { + server_message = data.toString(); + socket2.write('Client hello'); + socket2.end(); + }); + socket2.on('end', endCallback); -socket3.on('secureConnect', function() { - handshake_done = true; -}); + var socket3 = tls.connect(port, function() {}); -socket3.on('data', function(data) { - server_message = data.toString(); - socket3.write('Client hello'); - socket3.end(); -}); + socket3.on('secureConnect', function() { + handshake_done = true; + }); -socket3.on('end', function() { - server.close(); -}); + socket3.on('data', function(data) { + server_message = data.toString(); + socket3.write('Client hello'); + socket3.end(); + }); + + socket3.on('end', endCallback); +} process.on('exit', function() { + assert.equal(endCount, 3); assert.equal(handshake_done, true); assert.equal(server_handshake_done, true); assert.equal(client_message === expected_client_msg, true); diff --git a/test/run_pass/test_tls_4.js b/test/run_pass/test_tls_4.js index 18e7df3411..916f0808b3 100644 --- a/test/run_pass/test_tls_4.js +++ b/test/run_pass/test_tls_4.js @@ -24,6 +24,7 @@ var server_message = ''; var server_closed = false; var server_handshake_done = false; var handshake_done = false; +var endCount = 0; var port = 8080; @@ -31,7 +32,7 @@ var server_options = { key: fs.readFileSync(process.cwd() + '/resources/my_key.key').toString(), cert: fs.readFileSync(process.cwd() + '/resources/my_crt.crt'), rejectUnauthorized: false, - isServer: true + isServer: true, }; var server = tls.createServer(server_options, function(socket) { @@ -41,7 +42,7 @@ var server = tls.createServer(server_options, function(socket) { client_message = data.toString(); }); -}).listen(port, function() { }); +}).listen(port, startTesting); server.on('secureConnection', function() { server_handshake_done = true; @@ -51,51 +52,61 @@ server.on('close', function() { server_closed = true; }); -var socket_options = { - rejectUnauthorized: false, +function endCallback() { + endCount += 1; + if (endCount === 3) { + server.close(); + } } -var socket1 = tls.connect(port, 'localhost', socket_options); +function startTesting() { + var socket_options = { + rejectUnauthorized: false, + }; -socket1.on('secureConnect', function() { - handshake_done = true; -}); + var socket1 = tls.connect(port, 'localhost', socket_options); -socket1.on('data', function(data) { - server_message = data.toString(); - socket1.write('Client hello'); - socket1.end(); -}); + socket1.on('secureConnect', function() { + handshake_done = true; + }); -var socket2 = tls.connect(port, 'localhost', socket_options, function() {}); + socket1.on('data', function(data) { + server_message = data.toString(); + socket1.write('Client hello'); + socket1.end(); + }); + socket1.on('end', endCallback); -socket1.on('secureConnect', function() { - handshake_done = true; -}); + var socket2 = tls.connect(port, 'localhost', socket_options, function() {}); -socket2.on('data', function(data) { - server_message = data.toString(); - socket2.write('Client hello'); - socket2.end(); -}); + socket1.on('secureConnect', function() { + handshake_done = true; + }); -var socket3 = tls.connect(port, 'localhost', function() {}); + socket2.on('data', function(data) { + server_message = data.toString(); + socket2.write('Client hello'); + socket2.end(); + }); + socket2.on('end', endCallback); -socket3.on('secureConnect', function(){ - handshake_done = true; -}); + var socket3 = tls.connect(port, 'localhost', function() {}); -socket3.on('data', function(data) { - server_message = data.toString(); - socket3.write('Client hello'); - socket3.end(); -}); + socket3.on('secureConnect', function() { + handshake_done = true; + }); -socket3.on('end', function() { - server.close(); -}); + socket3.on('data', function(data) { + server_message = data.toString(); + socket3.write('Client hello'); + socket3.end(); + }); + + socket3.on('end', endCallback); +} process.on('exit', function() { + assert.equal(endCount, 3); assert.equal(handshake_done, true); assert.equal(server_handshake_done, true); assert.equal(client_message === expected_client_msg, true);