From 89de5f5975ac9ee28ab2c10b97efe2b8e8a93998 Mon Sep 17 00:00:00 2001 From: albertyfwu Date: Tue, 12 Mar 2013 02:59:47 -0400 Subject: [PATCH] fixed bug89 and added tests: writeBuffer not flushed until nextTick' --- lib/socket.js | 11 ++++++++++- test/server.js | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/socket.js b/lib/socket.js index 316eabbd..2cd6f518 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -226,6 +226,12 @@ Socket.prototype.clearTransport = function () { Socket.prototype.onClose = function (reason, description) { if ('closed' != this.readyState) { + var self = this; + // clean writeBuffer in next tick, so developers can still + // grab the writeBuffer on 'close' event + process.nextTick(function() { + self.writeBuffer = []; + }); this.packetsFn = []; this.sentCallbackFn = []; this.clearTransport(); @@ -316,7 +322,10 @@ Socket.prototype.flush = function () { this.emit('flush', this.writeBuffer); this.server.emit('flush', this, this.writeBuffer); var wbuf = this.writeBuffer; - this.writeBuffer = []; + // Let developer have time to read the writeBuffer + if ('closing' != this.readyState) { + this.writeBuffer = []; + } if (!this.transport.supportsFraming) { this.sentCallbackFn.push(this.packetsFn) } else { diff --git a/test/server.js b/test/server.js index babbfa03..47a69c31 100644 --- a/test/server.js +++ b/test/server.js @@ -227,6 +227,25 @@ 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 }; + 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(2); // has close packet + setTimeout(function () { + expect(conn.writeBuffer.length).to.be(0); // writeBuffer has been cleared + }, 10); + }); + conn.writeBuffer.push({ type: 'message', data: 'foo'}); + conn.close(); + }); + }); + done(); + }); + 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) {