summaryrefslogtreecommitdiff
path: root/test/sequential/test-dgram-pingpong.js
diff options
context:
space:
mode:
authorRich Trott <rtrott@gmail.com>2016-02-04 16:27:15 -0800
committerJames M Snell <jasnell@gmail.com>2016-02-07 13:01:05 -0800
commit987e9e3d64511e5f97e2285ea329daf781dc9958 (patch)
tree4e06df498007cecee0b0ccde0dacc313ea75833f /test/sequential/test-dgram-pingpong.js
parent2c55cc2d2c584a4269d5d516cbf8b45e0d1092f3 (diff)
downloadandroid-node-v8-987e9e3d64511e5f97e2285ea329daf781dc9958.tar.gz
android-node-v8-987e9e3d64511e5f97e2285ea329daf781dc9958.tar.bz2
android-node-v8-987e9e3d64511e5f97e2285ea329daf781dc9958.zip
test: fix flaky test-dgram-pingpong
There is no guarantee UDP messages will be received. Accommodate the occasional dropped message. This is a functionality test, not a performance benchmark. Speed up the test by not sending 1500 messages across three ports. Fixes: https://github.com/nodejs/node/issues/4526 PR-URL: https://github.com/nodejs/node/pull/5125 Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/sequential/test-dgram-pingpong.js')
-rw-r--r--test/sequential/test-dgram-pingpong.js85
1 files changed, 21 insertions, 64 deletions
diff --git a/test/sequential/test-dgram-pingpong.js b/test/sequential/test-dgram-pingpong.js
index 63d001d0b3..5760024c0c 100644
--- a/test/sequential/test-dgram-pingpong.js
+++ b/test/sequential/test-dgram-pingpong.js
@@ -1,89 +1,46 @@
'use strict';
-var common = require('../common');
-var assert = require('assert');
-var Buffer = require('buffer').Buffer;
-var dgram = require('dgram');
-
-var debug = false;
-var tests_run = 0;
+const common = require('../common');
+const assert = require('assert');
+const dgram = require('dgram');
function pingPongTest(port, host) {
- var callbacks = 0;
- var N = 500;
- var count = 0;
-
- var server = dgram.createSocket('udp4', function(msg, rinfo) {
- if (debug) console.log('server got: ' + msg +
- ' from ' + rinfo.address + ':' + rinfo.port);
- if (/PING/.exec(msg)) {
- var buf = new Buffer(4);
- buf.write('PONG');
- server.send(buf, 0, buf.length,
- rinfo.port, rinfo.address,
- function(err, sent) {
- callbacks++;
- });
- }
- });
+ const server = dgram.createSocket('udp4', common.mustCall((msg, rinfo) => {
+ assert.strictEqual('PING', msg.toString('ascii'));
+ server.send('PONG', 0, 4, rinfo.port, rinfo.address);
+ }));
server.on('error', function(e) {
throw e;
});
server.on('listening', function() {
- console.log('server listening on ' + port + ' ' + host);
+ console.log('server listening on ' + port);
- const buf = new Buffer('PING');
const client = dgram.createSocket('udp4');
- client.on('message', function(msg, rinfo) {
- if (debug) console.log('client got: ' + msg +
- ' from ' + rinfo.address + ':' + rinfo.port);
- assert.equal('PONG', msg.toString('ascii'));
-
- count += 1;
-
- if (count < N) {
- client.send(buf, 0, buf.length, port, 'localhost');
- } else {
- client.send(buf, 0, buf.length, port, 'localhost', function() {
- client.close();
- });
- }
- });
+ client.on('message', function(msg) {
+ assert.strictEqual('PONG', msg.toString('ascii'));
- client.on('close', function() {
- console.log('client has closed, closing server');
- assert.equal(N, count);
- tests_run += 1;
+ client.close();
server.close();
- assert.equal(N - 1, callbacks);
});
client.on('error', function(e) {
throw e;
});
- console.log('Client sending to ' + port + ', localhost ' + buf);
- client.send(buf, 0, buf.length, port, 'localhost', function(err, bytes) {
- if (err) {
- throw err;
- }
- console.log('Client sent ' + bytes + ' bytes');
- });
- count += 1;
+ console.log('Client sending to ' + port);
+
+ function clientSend() {
+ client.send('PING', 0, 4, port, 'localhost');
+ }
+
+ clientSend();
});
server.bind(port, host);
+ return server;
}
-// All are run at once, so run on different ports
-pingPongTest(common.PORT + 0, 'localhost');
-pingPongTest(common.PORT + 1, 'localhost');
-pingPongTest(common.PORT + 2);
-//pingPongTest('/tmp/pingpong.sock');
-
-process.on('exit', function() {
- assert.equal(3, tests_run);
- console.log('done');
-});
+const server = pingPongTest(common.PORT, 'localhost');
+server.on('close', common.mustCall(pingPongTest.bind(undefined, common.PORT)));