|
1 | 1 | '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'); |
9 | 5 |
|
10 | 6 | 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); |
18 | 7 |
|
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 | + })); |
29 | 12 |
|
30 | 13 | server.on('error', function(e) {
|
31 | 14 | throw e;
|
32 | 15 | });
|
33 | 16 |
|
34 | 17 | server.on('listening', function() {
|
35 |
| - console.log('server listening on ' + port + ' ' + host); |
| 18 | + console.log('server listening on ' + port); |
36 | 19 |
|
37 |
| - const buf = new Buffer('PING'); |
38 | 20 | const client = dgram.createSocket('udp4');
|
39 | 21 |
|
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')); |
55 | 24 |
|
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(); |
60 | 26 | server.close();
|
61 |
| - assert.equal(N - 1, callbacks); |
62 | 27 | });
|
63 | 28 |
|
64 | 29 | client.on('error', function(e) {
|
65 | 30 | throw e;
|
66 | 31 | });
|
67 | 32 |
|
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(); |
76 | 40 | });
|
77 | 41 | server.bind(port, host);
|
| 42 | + return server; |
78 | 43 | }
|
79 | 44 |
|
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