From 28af6bd7773839dd51913116ee6650978c767d15 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sun, 17 Mar 2013 12:49:30 -0400 Subject: [PATCH 1/7] added tests for callbacks and buffering --- test/server.js | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/test/server.js b/test/server.js index bc533627..21254963 100644 --- a/test/server.js +++ b/test/server.js @@ -803,7 +803,93 @@ describe('server', function () { }); describe('send', function() { + describe('writeBuffer', function() { + it('should not empty until `drain` event (polling)', function (done) { + var engine = listen({ allowUpgrades: false }, function (port) { + var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['polling'] }); + var totalEvents = 2; + socket.on('open', function() { + socket.send('a'); + socket.send('b'); + // writeBuffer should be nonempty, with 'a' still in it + expect(socket.writeBuffer.length).to.eql(2); + }); + socket.transport.on('drain', function() { + expect(socket.writeBuffer.length).to.eql(--totalEvents); + totalEvents || done(); + }); + }); + }); + + it('should not empty until `drain` event (websocket)', function (done) { + var engine = listen({ allowUpgrades: false }, function (port) { + var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['websocket'] }); + var totalEvents = 2; + socket.on('open', function() { + socket.send('a'); + socket.send('b'); + // writeBuffer should be nonempty, with 'a' still in it + expect(socket.writeBuffer.length).to.eql(2); + }); + socket.transport.on('drain', function() { + expect(socket.writeBuffer.length).to.eql(--totalEvents); + totalEvents || done(); + }); + }); + }); + }); + describe('callback', function() { + it('should execute when message sent (client) (polling)', function (done) { + var engine = listen({ allowUpgrades: false }, function (port) { + var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['polling'] }); + var i = 0; + var j = 0; + + engine.on('connection', function(conn) { + conn.on('message', function (msg) { + i++; + }); + }); + + socket.on('open', function () { + socket.send('a', function () { + j++ + }); + }); + + setTimeout(function() { + expect(i).to.be(j); + done(); + }, 10); + }); + }); + + it('should execute when message sent (client) (websocket)', function (done) { + var engine = listen({ allowUpgrades: false }, function (port) { + var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['websocket'] }); + var i = 0; + var j = 0; + + engine.on('connection', function(conn) { + conn.on('message', function (msg) { + i++; + }); + }); + + socket.on('open', function () { + socket.send('a', function () { + j++ + }); + }); + + setTimeout(function() { + expect(i).to.be(j); + done(); + }, 10); + }); + }); + it('should execute when message sent (polling)', function (done) { var engine = listen({ allowUpgrades: false }, function (port) { var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['polling'] }); From 8c6a03d9ad46d86bb1f771af26228f10d7c2bdc8 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sun, 17 Mar 2013 13:38:57 -0400 Subject: [PATCH 2/7] changed test to test callbacks in order --- test/server.js | 64 +++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/test/server.js b/test/server.js index 21254963..f99b2b1b 100644 --- a/test/server.js +++ b/test/server.js @@ -840,53 +840,57 @@ describe('server', function () { }); describe('callback', function() { - it('should execute when message sent (client) (polling)', function (done) { + it('should execute in order when message sent (client) (polling)', function (done) { var engine = listen({ allowUpgrades: false }, function (port) { var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['polling'] }); - var i = 0; var j = 0; - engine.on('connection', function(conn) { - conn.on('message', function (msg) { - i++; - }); - }); + function check(temp) { + expect(temp).to.be(j); + j++; + } socket.on('open', function () { - socket.send('a', function () { - j++ - }); + function sendFn(iter) { + socket.send('a', function() { + check(iter); + // send another packet until we've sent 3 total + if (j < 3) { + sendFn(iter+1); + } else { + done(); + } + }); + } + sendFn(0); }); - - setTimeout(function() { - expect(i).to.be(j); - done(); - }, 10); }); }); - it('should execute when message sent (client) (websocket)', function (done) { + it('should execute in order when message sent (client) (websocket)', function (done) { var engine = listen({ allowUpgrades: false }, function (port) { var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['websocket'] }); - var i = 0; var j = 0; - engine.on('connection', function(conn) { - conn.on('message', function (msg) { - i++; - }); - }); + function check(temp) { + expect(temp).to.be(j); + j++; + } socket.on('open', function () { - socket.send('a', function () { - j++ - }); + function sendFn(iter) { + socket.send('a', function() { + check(iter); + // send another packet until we've sent 3 total + if (j < 3) { + sendFn(iter+1); + } else { + done(); + } + }); + } + sendFn(0); }); - - setTimeout(function() { - expect(i).to.be(j); - done(); - }, 10); }); }); From 6d51d73d143b28a31163048d605170fcd8e3cb98 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sun, 17 Mar 2013 20:55:30 -0400 Subject: [PATCH 3/7] more extensive tests for executing callbacks in order --- test/server.js | 142 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 112 insertions(+), 30 deletions(-) diff --git a/test/server.js b/test/server.js index f99b2b1b..47c757cd 100644 --- a/test/server.js +++ b/test/server.js @@ -843,26 +843,33 @@ describe('server', function () { it('should execute in order when message sent (client) (polling)', function (done) { var engine = listen({ allowUpgrades: false }, function (port) { var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['polling'] }); + var i = 0; var j = 0; - function check(temp) { - expect(temp).to.be(j); - j++; - } + engine.on('connection', function(conn) { + conn.on('message', function(msg) { + conn.send(msg); + }); + }); socket.on('open', function () { - function sendFn(iter) { - socket.send('a', function() { - check(iter); - // send another packet until we've sent 3 total - if (j < 3) { - sendFn(iter+1); - } else { - done(); - } - }); + socket.on('message', function(msg) { + // send another packet until we've sent 3 total + if (++i < 3) { + expect(i).to.eql(j); + sendFn(); + } else { + done(); + } + }); + + function sendFn() { + socket.send(j, (function(value) { + j++; + })(j)); } - sendFn(0); + + sendFn(); }); }); }); @@ -870,26 +877,101 @@ describe('server', function () { it('should execute in order when message sent (client) (websocket)', function (done) { var engine = listen({ allowUpgrades: false }, function (port) { var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['websocket'] }); + var i = 0; var j = 0; - function check(temp) { - expect(temp).to.be(j); - j++; - } + engine.on('connection', function(conn) { + conn.on('message', function(msg) { + conn.send(msg); + }); + }); socket.on('open', function () { - function sendFn(iter) { - socket.send('a', function() { - check(iter); - // send another packet until we've sent 3 total - if (j < 3) { - sendFn(iter+1); - } else { - done(); - } - }); + socket.on('message', function(msg) { + // send another packet until we've sent 3 total + if (++i < 3) { + expect(i).to.eql(j); + sendFn(); + } else { + done(); + } + }); + + function sendFn() { + socket.send(j, (function(value) { + j++; + })(j)); } - sendFn(0); + + sendFn(); + }); + }); + }); + + it('should execute in order with payloads (client) (polling)', function (done) { + var engine = listen({ allowUpgrades: false }, function (port) { + var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['polling'] }); + var i = 0; + var lastCbFired = 0; + + engine.on('connection', function(conn) { + conn.on('message', function(msg) { + conn.send(msg); + }); + }); + + socket.on('open', function () { + socket.on('message', function(msg) { + expect(msg).to.eql(i + 1); + i++; + }); + + function cb(value) { + expect(value).to.eql(lastCbFired + 1); + lastCbFired = value; + if (value == 3) { + done(); + } + } + + socket.send(1, function() { cb(1); }); + // 2 and 3 will be in the same payload + socket.send(2, function() { cb(2); }); + socket.send(3, function() { cb(3); }); + }); + }); + }); + + it('should execute in order with payloads (client) (websocket)', function (done) { + var engine = listen({ allowUpgrades: false }, function (port) { + var socket = new eioc.Socket('ws://localhost:%d'.s(port), { transports: ['websocket'] }); + var i = 0; + var lastCbFired = 0; + + engine.on('connection', function(conn) { + conn.on('message', function(msg) { + conn.send(msg); + }); + }); + + socket.on('open', function () { + socket.on('message', function(msg) { + expect(msg).to.eql(i + 1); + i++; + }); + + function cb(value) { + expect(value).to.eql(lastCbFired + 1); + lastCbFired = value; + if (value == 3) { + done(); + } + } + + socket.send(1, function() { cb(1); }); + // 2 and 3 will be in the same payload + socket.send(2, function() { cb(2); }); + socket.send(3, function() { cb(3); }); }); }); }); From 671d2fe6dd6a7bd440ef818b9c890e421fd7d9e4 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sun, 17 Mar 2013 21:02:54 -0400 Subject: [PATCH 4/7] changed test to listen to `flushComplete` --- test/server.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/test/server.js b/test/server.js index 47c757cd..213ec1f1 100644 --- a/test/server.js +++ b/test/server.js @@ -934,10 +934,13 @@ describe('server', function () { } } + // 2 and 3 will be in the same payload + socket.once('flushComplete', function() { + socket.send(2, function() { cb(2); }); + socket.send(3, function() { cb(3); }); + }); + socket.send(1, function() { cb(1); }); - // 2 and 3 will be in the same payload - socket.send(2, function() { cb(2); }); - socket.send(3, function() { cb(3); }); }); }); }); @@ -968,10 +971,13 @@ describe('server', function () { } } + // 2 and 3 will be in the same payload + socket.once('flushComplete', function() { + socket.send(2, function() { cb(2); }); + socket.send(3, function() { cb(3); }); + }); + socket.send(1, function() { cb(1); }); - // 2 and 3 will be in the same payload - socket.send(2, function() { cb(2); }); - socket.send(3, function() { cb(3); }); }); }); }); From 15912c22c44b4c5532a46e976e17cb6ca8390dbb Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Sun, 17 Mar 2013 22:18:16 -0400 Subject: [PATCH 5/7] changed flushComplete to flush --- test/server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/server.js b/test/server.js index 213ec1f1..d4c39004 100644 --- a/test/server.js +++ b/test/server.js @@ -935,7 +935,7 @@ describe('server', function () { } // 2 and 3 will be in the same payload - socket.once('flushComplete', function() { + socket.once('flush', function() { socket.send(2, function() { cb(2); }); socket.send(3, function() { cb(3); }); }); @@ -972,7 +972,7 @@ describe('server', function () { } // 2 and 3 will be in the same payload - socket.once('flushComplete', function() { + socket.once('flush', function() { socket.send(2, function() { cb(2); }); socket.send(3, function() { cb(3); }); }); From 237499cdfb7e88eb609256f7507331bdb34668e0 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Tue, 19 Mar 2013 12:09:40 -0400 Subject: [PATCH 6/7] added test for client-side buffer cleanup --- test/server.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/test/server.js b/test/server.js index d4c39004..485ddd9f 100644 --- a/test/server.js +++ b/test/server.js @@ -225,12 +225,11 @@ describe('server', function () { }); }); - describe('close', function () { - it('should be able to access non-empty writeBuffer at closing', function(done) { - var opts = {allowUpgrades: false, pingInterval: 10, pingTimeout: 10 }; + describe.only('close', function () { + it('should be able to access non-empty writeBuffer at closing (server)', function(done) { + var opts = {allowUpgrades: false}; var engine = listen(opts, function (port) { var socket = new eioc.Socket('http://localhost:%d'.s(port)); - socket.sendPacket = function (){}; engine.on('connection', function (conn) { conn.on('close', function (reason) { expect(conn.writeBuffer.length).to.be(1); @@ -245,6 +244,27 @@ describe('server', function () { }); }); + it('should be able to access non-empty writeBuffer at closing (client)', function(done) { + var opts = {allowUpgrades: false}; + var engine = listen(opts, function (port) { + var socket = new eioc.Socket('http://localhost:%d'.s(port)); + socket.on('open', function() { + socket.on('close', function (reason) { + expect(socket.writeBuffer.length).to.be(1); + expect(socket.callbackBuffer.length).to.be(1); + setTimeout(function() { + expect(socket.writeBuffer.length).to.be(0); + expect(socket.callbackBuffer.length).to.be(0); + }, 10); + done(); + }); + socket.writeBuffer.push({ type: 'message', data: 'foo'}); + socket.callbackBuffer.push(function() {}); + socket.onError(''); + }); + }); + }); + it('should trigger on server if the client does not pong', function (done) { var opts = { allowUpgrades: false, pingInterval: 5, pingTimeout: 5 }; var engine = listen(opts, function (port) { From 6dd979cc13cd7857fd351616ab89e25cc28c9775 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Tue, 19 Mar 2013 12:12:32 -0400 Subject: [PATCH 7/7] remove from tests --- test/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/server.js b/test/server.js index 485ddd9f..09c739a4 100644 --- a/test/server.js +++ b/test/server.js @@ -225,7 +225,7 @@ describe('server', function () { }); }); - describe.only('close', function () { + describe('close', function () { it('should be able to access non-empty writeBuffer at closing (server)', function(done) { var opts = {allowUpgrades: false}; var engine = listen(opts, function (port) {