11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst dgram = require('dgram');
61cb0ef41Sopenharmony_ciconst client = dgram.createSocket('udp4');
71cb0ef41Sopenharmony_ciconst chunk = 'abc';
81cb0ef41Sopenharmony_cilet received = 0;
91cb0ef41Sopenharmony_cilet sent = 0;
101cb0ef41Sopenharmony_ciconst limit = 10;
111cb0ef41Sopenharmony_cilet async = false;
121cb0ef41Sopenharmony_cilet port;
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_cifunction onsend() {
151cb0ef41Sopenharmony_ci  if (sent++ < limit) {
161cb0ef41Sopenharmony_ci    client.send(chunk, 0, chunk.length, port, common.localhostIPv4, onsend);
171cb0ef41Sopenharmony_ci  } else {
181cb0ef41Sopenharmony_ci    assert.strictEqual(async, true);
191cb0ef41Sopenharmony_ci  }
201cb0ef41Sopenharmony_ci}
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciclient.on('listening', function() {
231cb0ef41Sopenharmony_ci  port = this.address().port;
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  process.nextTick(() => {
261cb0ef41Sopenharmony_ci    async = true;
271cb0ef41Sopenharmony_ci  });
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  onsend();
301cb0ef41Sopenharmony_ci});
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciclient.on('message', (buf, info) => {
331cb0ef41Sopenharmony_ci  received++;
341cb0ef41Sopenharmony_ci  if (received === limit) {
351cb0ef41Sopenharmony_ci    client.close();
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci});
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciclient.on('close', common.mustCall(function() {
401cb0ef41Sopenharmony_ci  assert.strictEqual(received, limit);
411cb0ef41Sopenharmony_ci}));
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciclient.bind(0);
44