From 32e7766688053ba939cfe8390e9c315d447743f1 Mon Sep 17 00:00:00 2001 From: Martin Grandrath Date: Sat, 27 May 2017 18:36:37 +0200 Subject: [PATCH 1/3] Wait for sockets to actually be connected before using them --- src/server/_real_time_server_test.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/server/_real_time_server_test.js b/src/server/_real_time_server_test.js index f7b654dab..d9b6c9fb9 100644 --- a/src/server/_real_time_server_test.js +++ b/src/server/_real_time_server_test.js @@ -71,11 +71,12 @@ it("counts the number of connections", function(done) { assert.equal(realTimeServer.numberOfActiveConnections(), 0, "before opening connection"); - var socket = createSocket(); - waitForConnectionCount(1, "after opening connection", function() { - assert.equal(realTimeServer.numberOfActiveConnections(), 1, "after opening connection"); - closeSocket(socket, function() { - waitForConnectionCount(0, "after closing connection", done); + createSocket(function (socket) { + waitForConnectionCount(1, "after opening connection", function() { + assert.equal(realTimeServer.numberOfActiveConnections(), 1, "after opening connection"); + closeSocket(socket, function() { + waitForConnectionCount(0, "after closing connection", done); + }); }); }); }); @@ -94,13 +95,14 @@ }); } - function createSocket() { - return io("http://localhost:" + PORT); + function createSocket(callback) { + var socket = io("http://localhost:" + PORT); + socket.on("connect", function () { callback(socket); }); } function closeSocket(socket, callback) { + socket.on("disconnect", callback); socket.disconnect(); - callback(); } function createTestFile(fileAndData, done) { From f7f6a741dc2d8b2f1d92daf834c4e0846c9c0095 Mon Sep 17 00:00:00 2001 From: Martin Grandrath Date: Sat, 27 May 2017 18:45:22 +0200 Subject: [PATCH 2/3] Minimize code -> sometimes the server does not shut down --- src/server/_real_time_server_test.js | 67 +--------------------------- 1 file changed, 1 insertion(+), 66 deletions(-) diff --git a/src/server/_real_time_server_test.js b/src/server/_real_time_server_test.js index d9b6c9fb9..64eda50f0 100644 --- a/src/server/_real_time_server_test.js +++ b/src/server/_real_time_server_test.js @@ -4,51 +4,19 @@ var RealTimeServer = require("./real_time_server.js"); var HttpServer = require("./http_server.js"); - var http = require("http"); - var fs = require("fs"); var async = require("async"); var assert = require("_assert"); var io = require("socket.io-client"); - var CONTENT_DIR = "generated/test"; - - var INDEX_PAGE = "index.html"; - var OTHER_PAGE = "other.html"; - var NOT_FOUND_PAGE = "test404.html"; - - var INDEX_PAGE_DATA = "This is index page file"; - var OTHER_PAGE_DATA = "This is another page"; - var NOT_FOUND_DATA = "This is 404 page file"; - - - var IRRELEVANT_DIR = "generated/test"; var IRRELEVANT_PAGE = "irrelevant.html"; var PORT = 5020; - var BASE_URL = "http://localhost:" + PORT; - - var TEST_FILES = [ - [ CONTENT_DIR + "/" + INDEX_PAGE, INDEX_PAGE_DATA], - [ CONTENT_DIR + "/" + OTHER_PAGE, OTHER_PAGE_DATA], - [ CONTENT_DIR + "/" + NOT_FOUND_PAGE, NOT_FOUND_DATA] - ]; describe("RealTimeServer", function() { var httpServer; var realTimeServer; - - beforeEach(function(done) { - async.each(TEST_FILES, createTestFile, done); - }); - - afterEach(function(done) { - async.each(TEST_FILES, deleteTestFile, done); - }); - - - beforeEach(function(done) { httpServer = new HttpServer(IRRELEVANT_DIR, IRRELEVANT_PAGE); realTimeServer = new RealTimeServer(); @@ -63,11 +31,6 @@ }); }); - it("delay", function(done) { - this.timeout(10000); - setTimeout(done, 0); - }); - it("counts the number of connections", function(done) { assert.equal(realTimeServer.numberOfActiveConnections(), 0, "before opening connection"); @@ -104,33 +67,5 @@ socket.on("disconnect", callback); socket.disconnect(); } - - function createTestFile(fileAndData, done) { - // Note: writeFile() MUST be called asynchronously in order for this code to work on Windows 7. - // If it's called synchronously, it fails with an EPERM error when the second test starts. This - // may be related to this Node.js issue: https://github.com/joyent/node/issues/6599 - // This issue appeared after upgrading send 0.2.0 to 0.9.3. Prior to that, writeFileSync() - // worked fine. - fs.writeFile(fileAndData[0], fileAndData[1], function(err) { - if (err) console.log("could not create test file: [" + fileAndData[0] + "]. Error: " + err); - done(); - }); - } - - function deleteTestFile(fileAndData, done) { - // Note: unlink() MUST be called asynchronously here in order for this code to work on Windows 7. - // If it's called synchronously, then it will run before the file is closed, and that is not allowed - // on Windows 7. It's possible that this is the result of a Node.js bug; see this issue for details: - // https://github.com/joyent/node/issues/6599 - var file = fileAndData[0]; - if (fs.existsSync(file)) { - fs.unlink(file, function(err) { - if (err || fs.existsSync(file)) console.log("could not delete test file: [" + file + "]. Error: " + err); - done(); - }); - } - } - }); - -}()); \ No newline at end of file +}()); From f7b295d379dd18fdc2e1e31f3ac6d67adb6dcc0d Mon Sep 17 00:00:00 2001 From: Martin Grandrath Date: Sat, 27 May 2017 18:46:07 +0200 Subject: [PATCH 3/3] Force server shutdown after timeout (default timeout is 2 minutes) --- src/server/_real_time_server_test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/server/_real_time_server_test.js b/src/server/_real_time_server_test.js index 64eda50f0..0a3b67733 100644 --- a/src/server/_real_time_server_test.js +++ b/src/server/_real_time_server_test.js @@ -12,6 +12,7 @@ var IRRELEVANT_PAGE = "irrelevant.html"; var PORT = 5020; + var SERVER_TIMEOUT = 500; // milliseconds describe("RealTimeServer", function() { var httpServer; @@ -22,10 +23,13 @@ realTimeServer = new RealTimeServer(); realTimeServer.start(httpServer.getNodeServer()); + httpServer._httpServer.setTimeout(SERVER_TIMEOUT); httpServer.start(PORT, done); }); afterEach(function(done) { + this.timeout(2 * SERVER_TIMEOUT); + waitForConnectionCount(0, "afterEach() requires all sockets to be closed", function() { httpServer.stop(done); });