0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-dgram-send-callback-recursive.js
Anna Henningsen afdc3d0d18 dgram: use uv_udp_try_send()
This improves dgram performance by avoiding unnecessary async
operations.

One issue with this commit is that it seems hard to actually create
conditions under which the fallback path to the async case is
actually taken, for all supported OS, so an internal CLI option
is used for testing that path.

Another caveat is that the lack of an async operation means
that there are slight timing differences (essentially `nextTick()`
rather than `setImmediate()` for the send callback).

PR-URL: https://github.com/nodejs/node/pull/29832
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2019-10-05 17:48:10 -07:00

44 lines
792 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const dgram = require('dgram');
const client = dgram.createSocket('udp4');
const chunk = 'abc';
let received = 0;
let sent = 0;
const limit = 10;
let async = false;
let port;
function onsend() {
if (sent++ < limit) {
client.send(chunk, 0, chunk.length, port, common.localhostIPv4, onsend);
} else {
assert.strictEqual(async, true);
}
}
client.on('listening', function() {
port = this.address().port;
process.nextTick(() => {
async = true;
});
onsend();
});
client.on('message', (buf, info) => {
received++;
if (received === limit) {
client.close();
}
});
client.on('close', common.mustCall(function() {
assert.strictEqual(received, limit);
}));
client.bind(0);