Skip to content

Commit fc3c32b

Browse files
Trottrvagg
authored andcommitted
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: #4526 PR-URL: #5125 Reviewed-By: Brian White <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 853d73b commit fc3c32b

File tree

2 files changed

+21
-65
lines changed

2 files changed

+21
-65
lines changed

test/sequential/sequential.status

-1
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]
+21-64
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,46 @@
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;
14-
15-
var server = dgram.createSocket('udp4', function(msg, rinfo) {
16-
if (debug) console.log('server got: ' + msg +
17-
' from ' + rinfo.address + ':' + rinfo.port);
187

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-
}
28-
});
8+
const server = dgram.createSocket('udp4', common.mustCall((msg, rinfo) => {
9+
assert.strictEqual('PING', msg.toString('ascii'));
10+
server.send('PONG', 0, 4, rinfo.port, rinfo.address);
11+
}));
2912

3013
server.on('error', function(e) {
3114
throw e;
3215
});
3316

3417
server.on('listening', function() {
35-
console.log('server listening on ' + port + ' ' + host);
18+
console.log('server listening on ' + port);
3619

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

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;
46-
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-
});
53-
}
54-
});
22+
client.on('message', function(msg) {
23+
assert.strictEqual('PONG', msg.toString('ascii'));
5524

56-
client.on('close', function() {
57-
console.log('client has closed, closing server');
58-
assert.equal(N, count);
59-
tests_run += 1;
25+
client.close();
6026
server.close();
61-
assert.equal(N - 1, callbacks);
6227
});
6328

6429
client.on('error', function(e) {
6530
throw e;
6631
});
6732

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;
33+
console.log('Client sending to ' + port);
34+
35+
function clientSend() {
36+
client.send('PING', 0, 4, port, 'localhost');
37+
}
38+
39+
clientSend();
7640
});
7741
server.bind(port, host);
42+
return server;
7843
}
7944

80-
// All are run at once, so run on different ports
81-
pingPongTest(common.PORT + 0, 'localhost');
82-
pingPongTest(common.PORT + 1, 'localhost');
83-
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-
});
45+
const server = pingPongTest(common.PORT, 'localhost');
46+
server.on('close', common.mustCall(pingPongTest.bind(undefined, common.PORT)));

0 commit comments

Comments
 (0)