1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4 5const dgram = require('dgram'); 6const client = dgram.createSocket('udp4'); 7const chunk = 'abc'; 8let received = 0; 9let sent = 0; 10const limit = 10; 11let async = false; 12let port; 13 14function onsend() { 15 if (sent++ < limit) { 16 client.send(chunk, 0, chunk.length, port, common.localhostIPv4, onsend); 17 } else { 18 assert.strictEqual(async, true); 19 } 20} 21 22client.on('listening', function() { 23 port = this.address().port; 24 25 process.nextTick(() => { 26 async = true; 27 }); 28 29 onsend(); 30}); 31 32client.on('message', (buf, info) => { 33 received++; 34 if (received === limit) { 35 client.close(); 36 } 37}); 38 39client.on('close', common.mustCall(function() { 40 assert.strictEqual(received, limit); 41})); 42 43client.bind(0); 44