11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst dgram = require('dgram');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst client = dgram.createSocket('udp4');
81cb0ef41Sopenharmony_ciconst server = dgram.createSocket('udp4');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst toSend = [Buffer.alloc(256, 'x'),
111cb0ef41Sopenharmony_ci                Buffer.alloc(256, 'y'),
121cb0ef41Sopenharmony_ci                Buffer.alloc(256, 'z'),
131cb0ef41Sopenharmony_ci                'hello'];
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst received = [];
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciserver.on('listening', common.mustCall(() => {
181cb0ef41Sopenharmony_ci  const port = server.address().port;
191cb0ef41Sopenharmony_ci  client.connect(port, (err) => {
201cb0ef41Sopenharmony_ci    assert.ifError(err);
211cb0ef41Sopenharmony_ci    client.send(toSend[0], 0, toSend[0].length);
221cb0ef41Sopenharmony_ci    client.send(toSend[1]);
231cb0ef41Sopenharmony_ci    client.send([toSend[2]]);
241cb0ef41Sopenharmony_ci    client.send(toSend[3], 0, toSend[3].length);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci    client.send(new Uint8Array(toSend[0]), 0, toSend[0].length);
271cb0ef41Sopenharmony_ci    client.send(new Uint8Array(toSend[1]));
281cb0ef41Sopenharmony_ci    client.send([new Uint8Array(toSend[2])]);
291cb0ef41Sopenharmony_ci    client.send(new Uint8Array(Buffer.from(toSend[3])),
301cb0ef41Sopenharmony_ci                0, toSend[3].length);
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci}));
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciserver.on('message', common.mustCall((buf, info) => {
351cb0ef41Sopenharmony_ci  received.push(buf.toString());
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  if (received.length === toSend.length * 2) {
381cb0ef41Sopenharmony_ci    // The replies may arrive out of order -> sort them before checking.
391cb0ef41Sopenharmony_ci    received.sort();
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci    const expected = toSend.concat(toSend).map(String).sort();
421cb0ef41Sopenharmony_ci    assert.deepStrictEqual(received, expected);
431cb0ef41Sopenharmony_ci    client.close();
441cb0ef41Sopenharmony_ci    server.close();
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci}, toSend.length * 2));
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ciserver.bind(0);
49