/
relay_spec.rb
84 lines (66 loc) · 1.55 KB
/
relay_spec.rb
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
require 'lib/zdevice'
include ZMQ::Device
describe "relay device" do
it "should assemble a one-time relay ZMQ Device" do
b = Builder.new(
context: { iothreads: 5 },
main: {
type: :queue,
frontend: {
type: :SUB,
option: { hwm: 1, swap: 25},
connect: ["tcp://127.0.0.1:5555"]
},
backend: {
type: :PUB,
bind: ["tcp://127.0.0.1:5556"] }
}
)
b.main.class.should == Device
b.main.type.should == :queue
# Create producer socket
sctx = ZMQ::Context.new
pub = sctx.socket(ZMQ::PUB)
pub.bind("tcp://127.0.0.1:5555")
# Start relay device
Thread.new do
b.main.start do
msg = ZMQ::Message.new
frontend.recv msg
backend.send msg
end
end
# Start producer
Thread.new do
loop do
pub.send ZMQ::Message.new("queue test")
end
end
# Consume re-routed message from producer
sub = sctx.socket(ZMQ::SUB)
sub.setsockopt(ZMQ::SUBSCRIBE, '')
sub.connect("tcp://127.0.0.1:5556")
rmsg = ZMQ::Message.new
sub.recv rmsg
rmsg.copy_out_string.should == "queue test"
pub.close
sub.close
b.close
end
it "should have more Ruby friendly syntax" do
pending "maybe"
d = Device.new do
context iothreads: 5, verbose: false
device :queue do
socket :frontend do
type ZMQ::SUB
option hwm: 1, swap: 25
connect ['']
bind ['']
end
end
end
d.queue.start do
end
end
end