Skip to content

E380: Suggestion how to clean up socket.io server test a bit #11

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

Open
wants to merge 1 commit into
base: master
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
18 changes: 10 additions & 8 deletions src/server/_server_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,14 @@
it("broadcasts mouse message from one client to all others", function(done) {
var EXPECTED_DATA = "mouse data";

var emitter = createSocket();
var receiver1 = createSocket();
var receiver2 = createSocket();
var clients = createSockets(3);
var emitter = clients[0];
var receivers = clients.slice().splice(1);

emitter.on("mouse", function() {
assert.fail("emitter should not receive its own events");
});

async.each([ receiver1, receiver2 ], function(client, next) {
async.each(receivers, function(client, next) {
client.on("mouse", function(data) {
assert.equal(data, EXPECTED_DATA);
next();
Expand All @@ -177,12 +176,15 @@
emitter.emit("mouse", EXPECTED_DATA);

function end() {
async.each([ emitter, receiver1, receiver2 ], function(socket, next) {
closeSocket(socket, next);
}, done);
async.each(clients, closeSocket, done);
}
});

function createSockets(count){
return Array.from({length: count}, createSocket);
//Or without ES6: return Array.apply(null, Array(count)).map(createSocket);
}

function createSocket() {
return io("http://localhost:" + PORT);
}
Expand Down