From 7df896982e951cd23562a424b4d3ebe0401f9643 Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Mon, 29 Dec 2014 21:20:54 -0800 Subject: [PATCH 1/3] dgram: make 'close' event async Emit the close event asynchronously, after the close, as it is with the net/http close events. --- lib/dgram.js | 5 ++++- test/parallel/test-dgram-close.js | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/dgram.js b/lib/dgram.js index ac7a054a4c4681..75e494cbf2f0d5 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -357,7 +357,10 @@ Socket.prototype.close = function() { this._stopReceiving(); this._handle.close(); this._handle = null; - this.emit('close'); + var self = this; + process.nextTick(function() { + self.emit('close'); + }); return this; }; diff --git a/test/parallel/test-dgram-close.js b/test/parallel/test-dgram-close.js index 1eed54c91f4106..14204bf05e4fd6 100644 --- a/test/parallel/test-dgram-close.js +++ b/test/parallel/test-dgram-close.js @@ -31,8 +31,12 @@ buf.fill(42); var socket = dgram.createSocket('udp4'); var handle = socket._handle; +var closeEvents = 0; socket.send(buf, 0, buf.length, common.PORT, 'localhost'); assert.strictEqual(socket.close(), socket); +socket.on('close', function() { + ++closeEvents; +}); socket = null; // Verify that accessing handle after closure doesn't throw @@ -41,3 +45,7 @@ setImmediate(function() { console.log('Handle fd is: ', handle.fd); }); }); + +process.on('exit', function() { + assert.equal(closeEvents, 1); +}); From c07fac0c26ffc26b0d7b1900786be4a0b9ab7c08 Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Mon, 29 Dec 2014 21:30:03 -0800 Subject: [PATCH 2/3] dgram: close() should accept a callback Like net, http, and https server.close, and socket.end(), etc. --- doc/api/dgram.markdown | 7 ++++--- lib/dgram.js | 4 +++- test/parallel/test-dgram-close.js | 7 ++++++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/doc/api/dgram.markdown b/doc/api/dgram.markdown index 09bacaf098c117..ce205c0f5c6fae 100644 --- a/doc/api/dgram.markdown +++ b/doc/api/dgram.markdown @@ -82,7 +82,7 @@ are created. ### Event: 'close' -Emitted when a socket is closed with `close()`. No new `message` events will be emitted +Emitted after a socket is closed with `close()`. No new `message` events will be emitted on this socket. ### Event: 'error' @@ -228,9 +228,10 @@ shown below. }); -### socket.close() +### socket.close([callback]) -Close the underlying socket and stop listening for data on it. +Close the underlying socket and stop listening for data on it. If a callback is +provided, it is added as a listener for the ['close'](#dgram_event_close) event. ### socket.address() diff --git a/lib/dgram.js b/lib/dgram.js index 75e494cbf2f0d5..255b9fb5941372 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -352,7 +352,9 @@ function afterSend(err) { } -Socket.prototype.close = function() { +Socket.prototype.close = function(callback) { + if (callback) + this.on('close', callback); this._healthCheck(); this._stopReceiving(); this._handle.close(); diff --git a/test/parallel/test-dgram-close.js b/test/parallel/test-dgram-close.js index 14204bf05e4fd6..6ac19339e4dd42 100644 --- a/test/parallel/test-dgram-close.js +++ b/test/parallel/test-dgram-close.js @@ -32,9 +32,13 @@ buf.fill(42); var socket = dgram.createSocket('udp4'); var handle = socket._handle; var closeEvents = 0; +var closeCallbacks = 0; socket.send(buf, 0, buf.length, common.PORT, 'localhost'); -assert.strictEqual(socket.close(), socket); +assert.strictEqual(socket.close(function() { + ++closeCallbacks; +}), socket); socket.on('close', function() { + assert.equal(closeCallbacks, 1); ++closeEvents; }); socket = null; @@ -48,4 +52,5 @@ setImmediate(function() { process.on('exit', function() { assert.equal(closeEvents, 1); + assert.equal(closeCallbacks, 1); }); From 61ac7113ff1b20eb827bba7dc36f6989fd92e8d0 Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Mon, 29 Dec 2014 22:10:36 -0800 Subject: [PATCH 3/3] doc: add tls server.close() callback docs Also, tests to confirm its existence. --- doc/api/tls.markdown | 4 ++-- test/parallel/test-tls-connect-simple.js | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/doc/api/tls.markdown b/doc/api/tls.markdown index c03845e8721d5c..d63a6eba013e96 100644 --- a/doc/api/tls.markdown +++ b/doc/api/tls.markdown @@ -602,11 +602,11 @@ when the server has been bound. See `net.Server` for more information. -### server.close() +### server.close([callback]) Stops the server from accepting new connections. This function is asynchronous, the server is finally closed when the server emits a `'close'` -event. +event. Optionally, you can pass a callback to listen for the `'close'` event. ### server.address() diff --git a/test/parallel/test-tls-connect-simple.js b/test/parallel/test-tls-connect-simple.js index e896dd9e22a51f..55e15221b1b0c9 100644 --- a/test/parallel/test-tls-connect-simple.js +++ b/test/parallel/test-tls-connect-simple.js @@ -26,6 +26,8 @@ var fs = require('fs'); var clientConnected = 0; var serverConnected = 0; +var serverCloseCallbacks = 0; +var serverCloseEvents = 0; var options = { key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'), @@ -34,7 +36,12 @@ var options = { var server = tls.Server(options, function(socket) { if (++serverConnected === 2) { - server.close(); + server.close(function() { + ++serverCloseCallbacks; + }); + server.on('close', function() { + ++serverCloseEvents; + }); } }); @@ -60,4 +67,6 @@ server.listen(common.PORT, function() { process.on('exit', function() { assert.equal(clientConnected, 2); assert.equal(serverConnected, 2); + assert.equal(serverCloseCallbacks, 1); + assert.equal(serverCloseEvents, 1); });