Skip to content

Commit 7d3f2aa

Browse files
committed
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: #4526
1 parent 6e3bccb commit 7d3f2aa

File tree

2 files changed

+33
-56
lines changed

2 files changed

+33
-56
lines changed

test/sequential/sequential.status

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ prefix sequential
1010

1111
[$system==linux]
1212
test-vm-syntax-error-stderr : PASS,FLAKY
13-
test-dgram-pingpong : PASS,FLAKY
1413
test-http-regr-gh-2928 : PASS,FLAKY
1514

1615
[$system==macos]
Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,62 @@
11
'use strict';
2-
var common = require('../common');
3-
var assert = require('assert');
4-
var Buffer = require('buffer').Buffer;
5-
var dgram = require('dgram');
6-
7-
var debug = false;
8-
var tests_run = 0;
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const dgram = require('dgram');
95

106
function pingPongTest(port, host) {
11-
var callbacks = 0;
12-
var N = 500;
13-
var count = 0;
7+
let pingsReceived = 0;
8+
let pongsReceived = 0;
9+
let done = false;
1410

15-
var server = dgram.createSocket('udp4', function(msg, rinfo) {
16-
if (debug) console.log('server got: ' + msg +
17-
' from ' + rinfo.address + ':' + rinfo.port);
11+
const N = 5;
1812

19-
if (/PING/.exec(msg)) {
20-
var buf = new Buffer(4);
21-
buf.write('PONG');
22-
server.send(buf, 0, buf.length,
23-
rinfo.port, rinfo.address,
24-
function(err, sent) {
25-
callbacks++;
26-
});
27-
}
13+
const server = dgram.createSocket('udp4', function(msg, rinfo) {
14+
assert.strictEqual('PING', msg.toString('ascii'));
15+
pingsReceived++;
16+
server.send('PONG', 0, 4, rinfo.port, rinfo.address);
2817
});
2918

3019
server.on('error', function(e) {
3120
throw e;
3221
});
3322

3423
server.on('listening', function() {
35-
console.log('server listening on ' + port + ' ' + host);
24+
console.log('server listening on ' + port);
3625

37-
const buf = new Buffer('PING');
3826
const client = dgram.createSocket('udp4');
3927

40-
client.on('message', function(msg, rinfo) {
41-
if (debug) console.log('client got: ' + msg +
42-
' from ' + rinfo.address + ':' + rinfo.port);
43-
assert.equal('PONG', msg.toString('ascii'));
44-
45-
count += 1;
28+
client.on('message', function(msg) {
29+
assert.strictEqual('PONG', msg.toString('ascii'));
4630

47-
if (count < N) {
48-
client.send(buf, 0, buf.length, port, 'localhost');
49-
} else {
50-
client.send(buf, 0, buf.length, port, 'localhost', function() {
51-
client.close();
52-
});
31+
pongsReceived++;
32+
if (pongsReceived === N) {
33+
done = true;
34+
client.close();
5335
}
5436
});
5537

56-
client.on('close', function() {
38+
client.on('close', common.mustCall(function() {
5739
console.log('client has closed, closing server');
58-
assert.equal(N, count);
59-
tests_run += 1;
40+
assert(pingsReceived >= N);
41+
assert.strictEqual(pongsReceived, N);
6042
server.close();
61-
assert.equal(N - 1, callbacks);
62-
});
43+
}));
6344

6445
client.on('error', function(e) {
6546
throw e;
6647
});
6748

68-
console.log('Client sending to ' + port + ', localhost ' + buf);
69-
client.send(buf, 0, buf.length, port, 'localhost', function(err, bytes) {
70-
if (err) {
71-
throw err;
72-
}
73-
console.log('Client sent ' + bytes + ' bytes');
74-
});
75-
count += 1;
49+
console.log('Client sending to ' + port);
50+
51+
function clientSend() {
52+
client.send('PING', 0, 4, port, 'localhost');
53+
setImmediate(() => {
54+
if (!done)
55+
clientSend();
56+
});
57+
}
58+
59+
clientSend();
7660
});
7761
server.bind(port, host);
7862
}
@@ -81,9 +65,3 @@ function pingPongTest(port, host) {
8165
pingPongTest(common.PORT + 0, 'localhost');
8266
pingPongTest(common.PORT + 1, 'localhost');
8367
pingPongTest(common.PORT + 2);
84-
//pingPongTest('/tmp/pingpong.sock');
85-
86-
process.on('exit', function() {
87-
assert.equal(3, tests_run);
88-
console.log('done');
89-
});

0 commit comments

Comments
 (0)