forked from zeromq/zeromq.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.req-rep.js
95 lines (79 loc) · 2.29 KB
/
socket.req-rep.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
var zmq = require('..')
, should = require('should');
describe('socket.req-rep', function(){
it('should support req-rep', function(done){
var rep = zmq.socket('rep')
, req = zmq.socket('req');
rep.on('message', function(msg){
msg.should.be.an.instanceof(Buffer);
msg.toString().should.equal('hello');
rep.send('world');
});
rep.bind('inproc://stuffreqrep', function (error) {
if (error) throw error;
req.connect('inproc://stuffreqrep');
req.send('hello');
req.on('message', function(msg){
msg.should.be.an.instanceof(Buffer);
msg.toString().should.equal('world');
rep.close();
req.close();
done();
});
});
});
it('should support multiple', function(done){
var n = 5;
for (var i = 0; i < n; i++) {
(function(n){
var rep = zmq.socket('rep')
, req = zmq.socket('req');
rep.on('message', function(msg){
msg.should.be.an.instanceof(Buffer);
msg.toString().should.equal('hello');
rep.send('world');
});
rep.bind('inproc://' + n, function (error) {
if (error) throw error;
req.connect('inproc://' + n);
req.send('hello');
req.on('message', function(msg){
msg.should.be.an.instanceof(Buffer);
msg.toString().should.equal('world');
req.close();
rep.close();
if (!--n) done();
});
});
})(i);
}
});
it('should support a burst', function (done) {
var rep = zmq.socket('rep');
var req = zmq.socket('req');
var n = 10;
rep.on('message', function (msg) {
msg.should.be.an.instanceof(Buffer);
msg.toString().should.equal('hello');
rep.send('world');
});
rep.bind('inproc://reqrepburst', function (error) {
if (error) throw error;
req.connect('inproc://reqrepburst');
var received = 0;
req.on('message', function(msg){
msg.should.be.an.instanceof(Buffer);
msg.toString().should.equal('world');
received += 1;
if (received === n) {
rep.close();
req.close();
done();
}
});
for (var i = 0; i < n; i += 1) {
req.send('hello');
}
});
});
});