This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
forked from ruby-amqp/march_hare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merry_go_round_spec.rb
132 lines (103 loc) · 2.27 KB
/
merry_go_round_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
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
RSpec.describe "A message that is proxied by multiple intermediate consumers" do
let(:c1) do
MarchHare.connect
end
let(:c2) do
MarchHare.connect
end
let(:c3) do
MarchHare.connect
end
let(:c4) do
MarchHare.connect
end
let(:c5) do
MarchHare.connect
end
after :each do
[c1, c2, c3, c4, c5].each do |c|
c.close if c.open?
end
end
# the message flow is as follows:
#
# x => q4 => q3 => q2 => q1 => xs (results)
it "reaches its final destination" do
n = 10000
xs = []
ch1 = c1.create_channel
q1 = ch1.queue("", :exclusive => true)
cn1 = q1.subscribe do |_, payload|
xs << payload
end
ch2 = c2.create_channel
q2 = ch2.queue("", :exclusive => true)
cn2 = q2.subscribe do |_, payload|
q1.publish(payload)
end
ch3 = c3.create_channel
q3 = ch2.queue("", :exclusive => true)
cn3 = q3.subscribe do |_, payload|
q2.publish(payload)
end
ch4 = c4.create_channel
q4 = ch2.queue("", :exclusive => true)
cn4 = q4.subscribe do |_, payload|
q3.publish(payload)
end
ch5 = c5.create_channel
x = ch5.default_exchange
n.times do |i|
x.publish("msg #{i}", :routing_key => q4.name)
end
until xs.size == n
sleep 0.5
end
expect(xs.size).to eq(n)
expect(xs.last).to eq("msg #{n - 1}")
[cn1, cn2, cn3, cn4].each do |cons|
cons.cancel
end
[ch1, ch2, ch3, ch4, ch5].each do |ch|
ch.close
end
end
it "can stretch tens of stages" do
s = 32
n = 1000
xs = []
chs = []
qs = []
cs = []
s.times do |i|
ch = c1.create_channel
chs << ch
next_q = qs.last
q = ch.queue("", :exclusive => true)
qs << q
cs << q.subscribe do |_, payload|
if next_q
next_q.publish(payload)
else
xs << payload
end
end
end
pch = c2.create_channel
x = pch.default_exchange
n.times do |i|
x.publish("msg #{i}", :routing_key => qs.last.name)
end
until xs.size == n
sleep 0.5
end
expect(xs.size).to eq(n)
expect(xs.last).to eq("msg #{n - 1}")
cs.each do |cons|
cons.cancel
end
chs.each do |ch|
ch.close
end
end
end