forked from zeromq/zeromq.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzmq_proxy.xpub-xsub.js
149 lines (112 loc) · 3.5 KB
/
zmq_proxy.xpub-xsub.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var zmq = require('..')
, should = require('should')
, semver = require('semver');
var addr = 'tcp://127.0.0.1'
, frontendAddr = addr+':5507'
, backendAddr = addr+':5508'
, captureAddr = addr+':5509';
var version = semver.gte(zmq.version, '3.1.0');
var testutil = require('./util');
describe('proxy.xpub-xsub', function() {
afterEach(testutil.cleanup);
it('should proxy pub-sub connected to xpub-xsub', function (done) {
if (!version) {
done();
return console.warn('Test requires libzmq >= 3.1.0');
}
var frontend = zmq.socket('xpub');
var backend = zmq.socket('xsub');
var sub = zmq.socket('sub');
var pub = zmq.socket('pub');
testutil.push_sockets(frontend, backend, sub, pub);
sub.subscribe('');
sub.on('message',function (msg) {
msg.should.be.an.instanceof(Buffer);
msg.toString().should.equal('foo');
console.log(msg.toString());
done();
});
frontend.bind(frontendAddr, function (error) {
if (error) throw error;
backend.bind(backendAddr, function (error) {
if (error) throw error;
sub.connect(frontendAddr);
pub.connect(backendAddr);
setTimeout(function() {
pub.send('foo');
}, 500);
setTimeout(function () {
throw Error("Timeout");
}, 10000);
zmq.proxy(frontend, backend);
});
});
});
it('should proxy connections with capture', function (done) {
if (!version) {
done();
return console.warn('Test requires libzmq >= 3.1.0');
}
var frontend = zmq.socket('xpub');
var backend = zmq.socket('xsub');
var capture = zmq.socket('pub');
var capSub = zmq.socket('sub');
var sub = zmq.socket('sub');
var pub = zmq.socket('pub');
testutil.push_sockets(frontend, backend, sub, pub, capture, capSub);
var countdown = testutil.done_countdown(done, 2);
sub.subscribe('');
sub.on('message', function (msg) {
msg.should.be.an.instanceof(Buffer);
msg.toString().should.equal('foo');
countdown();
});
capSub.subscribe('');
capSub.on('message',function (msg) {
msg.should.be.an.instanceof(Buffer);
msg.toString().should.equal('foo');
countdown();
});
capture.bind(captureAddr, function (error) {
if (error) throw error;
frontend.bind(frontendAddr, function (error) {
if (error) throw error;
backend.bind(backendAddr, function (error) {
if (error) throw error;
pub.connect(backendAddr);
sub.connect(frontendAddr);
capSub.connect(captureAddr);
setTimeout(function () {
pub.send('foo');
}, 500);
setTimeout(function () {
throw Error("Timeout");
}, 10000);
zmq.proxy(frontend,backend,capture);
});
});
});
});
it('should throw an error if the order is wrong', function (done) {
if (!version) {
done();
return console.warn('Test requires libzmq >= 3.1.0');
}
var frontend = zmq.socket('xpub');
var backend = zmq.socket('xsub');
var sub = zmq.socket('sub');
var pub = zmq.socket('pub');
frontend.bindSync(frontendAddr);
backend.bindSync(backendAddr);
sub.connect(frontendAddr);
pub.connect(backendAddr);
testutil.push_sockets(frontend, backend, sub, pub);
try{
zmq.proxy(backend,frontend);
} catch(e){
e.message.should.equal('wrong socket order to proxy');
} finally{
done();
}
})
});