diff --git a/test/parallel/test-dgram-empty-packet.js b/test/parallel/test-dgram-empty-packet.js deleted file mode 100644 index 34bce4e07aede7..00000000000000 --- a/test/parallel/test-dgram-empty-packet.js +++ /dev/null @@ -1,40 +0,0 @@ -'use strict'; -const common = require('../common'); -const dgram = require('dgram'); - -let callbacks = 0; -let timer; - -if (common.isOSX) { - common.skip('because of 17894467 Apple bug'); - return; -} - -const client = dgram.createSocket('udp4'); - -client.bind(0, function() { - - function callback() { - callbacks++; - if (callbacks === 2) { - clearTimeout(timer); - client.close(); - } else if (callbacks > 2) { - throw new Error('the callbacks should be called only two times'); - } - } - - client.on('message', function(buffer, bytes) { - callback(); - }); - - const port = this.address().port; - client.send( - Buffer.allocUnsafe(1), 0, 0, port, '127.0.0.1', (err, len) => { - callback(); - }); - - timer = setTimeout(function() { - throw new Error('Timeout'); - }, 200); -}); diff --git a/test/parallel/test-dgram-send-empty-array.js b/test/parallel/test-dgram-send-empty-array.js index f9de345f112254..442803e6db8632 100644 --- a/test/parallel/test-dgram-send-empty-array.js +++ b/test/parallel/test-dgram-send-empty-array.js @@ -1,24 +1,30 @@ 'use strict'; const common = require('../common'); -const assert = require('assert'); -const dgram = require('dgram'); if (common.isOSX) { common.skip('because of 17894467 Apple bug'); return; } +const assert = require('assert'); +const dgram = require('dgram'); + const client = dgram.createSocket('udp4'); +var interval; + client.on('message', common.mustCall(function onMessage(buf, info) { const expected = Buffer.alloc(0); assert.ok(buf.equals(expected), 'message was received correctly'); + clearInterval(interval); client.close(); })); -client.on('listening', function() { - client.send([], this.address().port, common.localhostIPv4); -}); +client.on('listening', common.mustCall(function() { + interval = setInterval(function() { + client.send([], client.address().port, common.localhostIPv4); + }, 10); +})); client.bind(0); diff --git a/test/parallel/test-dgram-send-empty-buffer.js b/test/parallel/test-dgram-send-empty-buffer.js index ac541a9c243947..70e7e46e0a327e 100644 --- a/test/parallel/test-dgram-send-empty-buffer.js +++ b/test/parallel/test-dgram-send-empty-buffer.js @@ -1,26 +1,27 @@ 'use strict'; const common = require('../common'); -const dgram = require('dgram'); +const assert = require('assert'); if (common.isOSX) { common.skip('because of 17894467 Apple bug'); return; } +const dgram = require('dgram'); + const client = dgram.createSocket('udp4'); -client.bind(0, function() { +client.bind(0, common.mustCall(function() { const port = this.address().port; - client.on('message', common.mustCall(function onMessage(buffer, bytes) { - clearTimeout(timer); + client.on('message', common.mustCall(function onMessage(buffer) { + assert.strictEqual(buffer.length, 0); + clearInterval(interval); client.close(); })); const buf = Buffer.alloc(0); - client.send(buf, 0, 0, port, '127.0.0.1', function(err, len) { }); - - const timer = setTimeout(function() { - throw new Error('Timeout'); - }, common.platformTimeout(200)); -}); + var interval = setInterval(function() { + client.send(buf, 0, 0, port, '127.0.0.1', common.mustCall(function() {})); + }, 10); +})); diff --git a/test/parallel/test-dgram-send-empty-packet.js b/test/parallel/test-dgram-send-empty-packet.js new file mode 100644 index 00000000000000..131e808aec0b72 --- /dev/null +++ b/test/parallel/test-dgram-send-empty-packet.js @@ -0,0 +1,34 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); + +if (common.isOSX) { + common.skip('because of 17894467 Apple bug'); + return; +} + +const dgram = require('dgram'); + +const client = dgram.createSocket('udp4'); + +client.bind(0, common.mustCall(function() { + + client.on('message', common.mustCall(callback)); + + const port = this.address().port; + const buf = Buffer.alloc(1); + + const interval = setInterval(function() { + client.send(buf, 0, 0, port, '127.0.0.1', common.mustCall(callback)); + }, 10); + + function callback(firstArg) { + // If client.send() callback, firstArg should be null. + // If client.on('message') listener, firstArg should be a 0-length buffer. + if (firstArg instanceof Buffer) { + assert.strictEqual(firstArg.length, 0); + clearInterval(interval); + client.close(); + } + } +}));