forked from espruino/Espruino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dgram_multi.js
More file actions
65 lines (57 loc) · 1.55 KB
/
test_dgram_multi.js
File metadata and controls
65 lines (57 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Socket server and client test
var success = 0;
var result = 0;
var port1 = 41234;
var port2 = 51234;
let dgram = require('dgram');
let srv1 = dgram.createSocket('udp4');
srv1.bind(port1, function(bsrv) {
bsrv.on('message', function(msg, info) {
console.log("<"+JSON.stringify(msg));
console.log("<"+JSON.stringify(info));
bsrv.send(msg+'!', info.port, info.address);
});
});
srv1.on('close', function() {
console.log('server 1 disconnected');
});
let srv2 = dgram.createSocket('udp4');
srv2.bind(port2, function(bsrv) {
bsrv.on('message', function(msg, info) {
console.log("<"+JSON.stringify(msg));
console.log("<"+JSON.stringify(info));
bsrv.send(msg+'!', info.port, info.address);
});
});
srv2.on('close', function() {
console.log('server 2 disconnected');
});
let client = dgram.createSocket('udp4');
client.on('message', function(msg, info) {
console.log(">"+JSON.stringify(msg));
console.log(">"+JSON.stringify(info));
if (msg=="42!" && info.address=="127.0.0.1" && info.port==port1) {
success++;
srv1.close();
}
if (msg=="24!" && info.address=="127.0.0.1" && info.port==port2) {
success++;
srv2.close();
}
result = success == 2 ? 1 : 0;
if (result == 1) {
clearTimeout(); // stop the fail fast
client.close();
}
});
client.on('close', function() {
console.log('client disconnected');
});
// fail the test fast if broken
setTimeout(function() {
client.close();
srv1.close();
srv2.close();
}, 100);
client.send('42', port1, 'localhost');
client.send('___24___', 3, 2, port2, 'localhost');