From 4411c845e6eea83f5b659fc829f17402b81ee434 Mon Sep 17 00:00:00 2001 From: Thorsten Lorenz Date: Wed, 9 Mar 2016 16:01:50 -0500 Subject: [PATCH 1/2] dgram: tracking unrefdness of dgram sockets - initializing `_unref` to `false` when the socket is created - updating that value each time either `unref()` or `ref()` is invoked on it - this mirrors the implementation in `lib/net.js` - adding test to check unrefdness tracking --- lib/dgram.js | 3 +++ test/parallel/test-dgram-ref-unref.js | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/parallel/test-dgram-ref-unref.js diff --git a/lib/dgram.js b/lib/dgram.js index 0c92e361c0eae7..57f010ac22870e 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -94,6 +94,7 @@ function Socket(type, listener) { this._bindState = BIND_STATE_UNBOUND; this.type = type; this.fd = null; // compatibility hack + this._unref = false; // If true - UV_UDP_REUSEADDR flag will be set this._reuseAddr = options && options.reuseAddr; @@ -532,6 +533,7 @@ function onMessage(nread, handle, buf, rinfo) { Socket.prototype.ref = function() { + this._unref = false; if (this._handle) this._handle.ref(); @@ -540,6 +542,7 @@ Socket.prototype.ref = function() { Socket.prototype.unref = function() { + this._unref = true; if (this._handle) this._handle.unref(); diff --git a/test/parallel/test-dgram-ref-unref.js b/test/parallel/test-dgram-ref-unref.js new file mode 100644 index 00000000000000..4cb7e54d2a2e5d --- /dev/null +++ b/test/parallel/test-dgram-ref-unref.js @@ -0,0 +1,24 @@ +'use strict'; +require('../common'); +const dgram = require('dgram'); +const assert = require('assert'); + +const sock4 = dgram.createSocket('udp4'); +assert.ok(sock4.hasOwnProperty('_unref')); +assert.ok(!sock4._unref); +sock4.unref(); +assert.ok(sock4._unref); +sock4.ref(); +assert.ok(!sock4._unref); +sock4.unref(); +assert.ok(sock4._unref); + +const sock6 = dgram.createSocket('udp6'); +assert.ok(sock6.hasOwnProperty('_unref')); +assert.ok(!sock6._unref); +sock6.unref(); +assert.ok(sock6._unref); +sock6.ref(); +assert.ok(!sock6._unref); +sock6.unref(); +assert.ok(sock6._unref); From 356b4d983bad754960391c8fe84c4e8a1d1f30d1 Mon Sep 17 00:00:00 2001 From: Thorsten Lorenz Date: Mon, 21 Mar 2016 14:14:00 -0500 Subject: [PATCH 2/2] fixup! dgram: tracking unrefdness of dgram sockets - using strictEquals for tests --- test/parallel/test-dgram-ref-unref.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/parallel/test-dgram-ref-unref.js b/test/parallel/test-dgram-ref-unref.js index 4cb7e54d2a2e5d..f6b9723ceca591 100644 --- a/test/parallel/test-dgram-ref-unref.js +++ b/test/parallel/test-dgram-ref-unref.js @@ -4,21 +4,21 @@ const dgram = require('dgram'); const assert = require('assert'); const sock4 = dgram.createSocket('udp4'); -assert.ok(sock4.hasOwnProperty('_unref')); -assert.ok(!sock4._unref); +assert.strictEqual(sock4.hasOwnProperty('_unref'), true); +assert.strictEqual(sock4._unref, false); sock4.unref(); -assert.ok(sock4._unref); +assert.strictEqual(sock4._unref, true); sock4.ref(); -assert.ok(!sock4._unref); +assert.strictEqual(sock4._unref, false); sock4.unref(); -assert.ok(sock4._unref); +assert.strictEqual(sock4._unref, true); const sock6 = dgram.createSocket('udp6'); -assert.ok(sock6.hasOwnProperty('_unref')); -assert.ok(!sock6._unref); +assert.strictEqual(sock6.hasOwnProperty('_unref'), true); +assert.strictEqual(sock6._unref, false); sock6.unref(); -assert.ok(sock6._unref); +assert.strictEqual(sock6._unref, true); sock6.ref(); -assert.ok(!sock6._unref); +assert.strictEqual(sock6._unref, false); sock6.unref(); -assert.ok(sock6._unref); +assert.strictEqual(sock6._unref, true);