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

Fix socket.io race condition #16

Open
wants to merge 3 commits into
base: spike_socket_io_race_condition
Choose a base branch
from
Open
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
89 changes: 15 additions & 74 deletions src/server/_real_time_server_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,46 @@

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]
];
var SERVER_TIMEOUT = 500; // milliseconds

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();

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);
});
});

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");

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);
});
});
});
});
Expand All @@ -94,41 +62,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) {
// 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();
});
}
}

});

}());
}());