Browse Source

dgram: make 'close' event async

Emit the close event asynchronously, after the close, as it is with the
net/http close events.

PR-URL: https://github.com/iojs/io.js/pull/217
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
archived-io.js-v0.12
Sam Roberts 10 years ago
committed by Ben Noordhuis
parent
commit
7349d7fd99
  1. 5
      lib/dgram.js
  2. 8
      test/parallel/test-dgram-close.js

5
lib/dgram.js

@ -357,7 +357,10 @@ Socket.prototype.close = function() {
this._stopReceiving(); this._stopReceiving();
this._handle.close(); this._handle.close();
this._handle = null; this._handle = null;
this.emit('close'); var self = this;
process.nextTick(function() {
self.emit('close');
});
return this; return this;
}; };

8
test/parallel/test-dgram-close.js

@ -31,8 +31,12 @@ buf.fill(42);
var socket = dgram.createSocket('udp4'); var socket = dgram.createSocket('udp4');
var handle = socket._handle; var handle = socket._handle;
var closeEvents = 0;
socket.send(buf, 0, buf.length, common.PORT, 'localhost'); socket.send(buf, 0, buf.length, common.PORT, 'localhost');
assert.strictEqual(socket.close(), socket); assert.strictEqual(socket.close(), socket);
socket.on('close', function() {
++closeEvents;
});
socket = null; socket = null;
// Verify that accessing handle after closure doesn't throw // Verify that accessing handle after closure doesn't throw
@ -41,3 +45,7 @@ setImmediate(function() {
console.log('Handle fd is: ', handle.fd); console.log('Handle fd is: ', handle.fd);
}); });
}); });
process.on('exit', function() {
assert.equal(closeEvents, 1);
});

Loading…
Cancel
Save