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

Remove race condition when server closes connection #256

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
3 changes: 2 additions & 1 deletion lib/WebSocketConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ WebSocketConnection.prototype.processReceivedData = function() {
// processed. We use setImmediate here instead of process.nextTick to
// explicitly indicate that we wish for other I/O to be handled first.
if (this.bufferList.length > 0) {
setImmediateImpl(this.receivedDataHandler);
this.receivedDataHandler();
//setImmediateImpl(this.receivedDataHandler);
}
};

Expand Down
54 changes: 54 additions & 0 deletions test/unit/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,57 @@ test('Protocol mismatch should be handled gracefully', function(t) {
t.end();
});
});

test('Server pushing and closing should not loose data', function(t) {
var wsServer;

t.test('setup', function(t) {
server.prepare(function(err, result) {
if (err) {
t.fail('Unable to start test server');
return t.end();
}

wsServer = result;
t.end();
});
});

t.test('server push and close', function(t) {
t.plan(2);
var count = 100;
var received = 0;
wsServer.on('request', handleRequest);

var client = new WebSocketClient();

var timer = setTimeout(function() {
t.fail('Timeout waiting for client event - received ' + received + ' frames out of ' + count);
}, 2000);

client.connect('ws://localhost:64321/', 'echo');
client.on('connect', function(connection) {
connection.on('message', function (msg) {
received++;
if (count === received) {
clearTimeout(timer);
t.pass('received enough frames');
}
});
});

function handleRequest(request) {
t.pass('connected');
var connection = request.accept('echo', request.origin);
for (var i = 0; i < count; i++) {
connection.sendUTF('some content');
}
connection.close();
}
});

t.test('teardown', function(t) {
stopServer();
t.end();
});
});