forked from pkrumins/dnode
/
bidirectional.js
41 lines (37 loc) · 1.15 KB
/
bidirectional.js
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
var dnode = require('../');
var test = require('tap').test;
test('bidirectional', function (t) {
t.plan(5);
var port = Math.floor(Math.random() * 40000 + 10000);
var counts = { timesX : 0, clientX : 0, x : 0 };
var server = dnode(function (client) {
this.timesX = function (n,f) {
t.equal(n, 3, "timesX's n == 3");
counts.timesX ++;
client.x(function (x) {
t.equal(x, 20, 'client.x == 20');
counts.clientX ++;
f(n * x);
});
};
}).listen(port);
server.on('ready', function () {
dnode({
x : function (f) {
counts.x ++;
f(20);
}
}).connect(port, function (remote, conn) {
remote.timesX(3, function (res) {
t.equal(res, 60, 'result of 20 * 3 == 60');
conn.end();
server.close();
});
});
});
server.once('close', function () {
t.equal(counts.timesX, 1, 'timesX called once');
t.equal(counts.clientX, 1, 'clientX called once');
t.end();
});
});