From 7d3f2aa8095d6f5882bc91ebee8f5769a7fba35c Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Thu, 4 Feb 2016 16:27:15 -0800 Subject: [PATCH] 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 sending 5 messages per server rather than 500. Fixes: https://github.com/nodejs/node/issues/4526 --- test/sequential/sequential.status | 1 - test/sequential/test-dgram-pingpong.js | 88 ++++++++++---------------- 2 files changed, 33 insertions(+), 56 deletions(-) diff --git a/test/sequential/sequential.status b/test/sequential/sequential.status index f872223d370ccd..84760b9c720653 100644 --- a/test/sequential/sequential.status +++ b/test/sequential/sequential.status @@ -10,7 +10,6 @@ prefix sequential [$system==linux] test-vm-syntax-error-stderr : PASS,FLAKY -test-dgram-pingpong : PASS,FLAKY test-http-regr-gh-2928 : PASS,FLAKY [$system==macos] diff --git a/test/sequential/test-dgram-pingpong.js b/test/sequential/test-dgram-pingpong.js index 63d001d0b376a4..6e9e17e87a4d21 100644 --- a/test/sequential/test-dgram-pingpong.js +++ b/test/sequential/test-dgram-pingpong.js @@ -1,30 +1,19 @@ '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; + let pingsReceived = 0; + let pongsReceived = 0; + let done = false; - var server = dgram.createSocket('udp4', function(msg, rinfo) { - if (debug) console.log('server got: ' + msg + - ' from ' + rinfo.address + ':' + rinfo.port); + const N = 5; - 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', function(msg, rinfo) { + assert.strictEqual('PING', msg.toString('ascii')); + pingsReceived++; + server.send('PONG', 0, 4, rinfo.port, rinfo.address); }); server.on('error', function(e) { @@ -32,47 +21,42 @@ function pingPongTest(port, host) { }); 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; + client.on('message', function(msg) { + assert.strictEqual('PONG', msg.toString('ascii')); - if (count < N) { - client.send(buf, 0, buf.length, port, 'localhost'); - } else { - client.send(buf, 0, buf.length, port, 'localhost', function() { - client.close(); - }); + pongsReceived++; + if (pongsReceived === N) { + done = true; + client.close(); } }); - client.on('close', function() { + client.on('close', common.mustCall(function() { console.log('client has closed, closing server'); - assert.equal(N, count); - tests_run += 1; + assert(pingsReceived >= N); + assert.strictEqual(pongsReceived, N); 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'); + setImmediate(() => { + if (!done) + clientSend(); + }); + } + + clientSend(); }); server.bind(port, host); } @@ -81,9 +65,3 @@ function pingPongTest(port, host) { 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'); -});