fixed bug89 and added tests: writeBuffer not flushed until nextTick'

This commit is contained in:
albertyfwu
2013-03-12 02:59:47 -04:00
parent e027e28686
commit 89de5f5975
2 changed files with 29 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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) {