-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmessage_metadata_access_spec.rb
175 lines (154 loc) · 6.59 KB
/
message_metadata_access_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
RSpec.describe "A consumer" do
let(:connection) { MarchHare.connect }
let(:channel) { connection.create_channel }
after :each do
channel.close
connection.close
end
it "can access message metadata (both message properties and delivery information)" do
latch = java.util.concurrent.CountDownLatch.new(1)
queue = channel.queue("", :exclusive => true)
exchange = channel.exchange("amq.fanout", :type => :fanout)
queue.bind(exchange, :routing_key => "march.hare.key")
@now = Time.now
@payload = "Hello, world!"
@meta = nil
_ = queue.subscribe(:blocking => false) do |metadata, payload|
begin
# we will run assertions on the main thread because RSpec uses exceptions
# for its purposes every once in a while. MK.
@meta = metadata
rescue Exception => e
e.print_stack_trace
ensure
latch.count_down
end
end
exchange.publish(@payload,
:properties => {
:app_id => "march.hare.tests",
:persistent => true,
:priority => 8,
:type => "kinda.checkin",
# headers table keys can be anything
:headers => {
:coordinates => {
:latitude => 59.35,
"longitude" => 18.066667
},
"time" => @now,
"participants" => 11,
:venue => "Stockholm",
"true_field" => true,
"false_field" => false,
"nil_field" => nil,
"ary_field" => ["one", 2.0, 3, [{ :abc => 123 }]]
},
:timestamp => @now,
:reply_to => "a.sender",
:correlation_id => "r-1",
:message_id => "m-1",
:content_type => "application/octet-stream",
# just an example. MK.
:content_encoding => "zip/zap"
},
:routing_key => "march.hare.key")
latch.await
expect(@meta.routing_key).to eq("march.hare.key")
expect(@meta.content_type).to eq("application/octet-stream")
expect(@meta.content_encoding).to eq("zip/zap")
expect(@meta.priority).to eq(8)
time = Time.at(@meta.headers["time"].getTime/1000)
expect(time.to_i).to eq(@now.to_i)
expect(@meta.headers["coordinates"]["latitude"]).to eq(59.35)
expect(@meta.headers["participants"]).to eq(11)
expect(@meta.headers["true_field"]).to eq(true)
expect(@meta.headers["false_field"]).to eq(false)
expect(@meta.headers["nil_field"]).to be_nil
expect(@meta.timestamp).to eq(Time.at(@now.to_i))
expect(@meta.type).to eq("kinda.checkin")
expect(@meta.consumer_tag).not_to be_nil
expect(@meta.consumer_tag).not_to be_empty
expect(@meta.delivery_tag.to_i).to eq(1)
expect(@meta.delivery_mode).to eq(2)
expect(@meta).to be_persistent
expect(@meta.reply_to).to eq("a.sender")
expect(@meta.correlation_id).to eq("r-1")
expect(@meta.message_id).to eq("m-1")
expect(@meta).not_to be_redelivered
expect(@meta.app_id).to eq("march.hare.tests")
expect(@meta.exchange).to eq("amq.fanout")
end
it "can handle properties being set at the top level" do
latch = java.util.concurrent.CountDownLatch.new(1)
queue = channel.queue("", :exclusive => true)
exchange = channel.exchange("amq.fanout", :type => :fanout)
queue.bind(exchange, :routing_key => "march.hare.key")
@now = Time.now
@payload = "Hello, world!"
@meta = nil
_ = queue.subscribe(:blocking => false) do |metadata, payload|
begin
# we will run assertions on the main thread because RSpec uses exceptions
# for its purposes every once in a while. MK.
@meta = metadata
rescue Exception => e
e.print_stack_trace
ensure
latch.count_down
end
end
exchange.publish(@payload,
:app_id => "march.hare.tests",
:persistent => true,
:priority => 8,
:type => "kinda.checkin",
# headers table keys can be anything
:headers => {
"coordinates" => {
"latitude" => 59.35,
"longitude" => 18.066667
},
"time" => @now,
"participants" => 11,
"venue" => "Stockholm",
"true_field" => true,
"false_field" => false,
"nil_field" => nil,
"ary_field" => ["one", 2.0, 3, [{ "abc" => 123 }]]
},
:timestamp => @now,
:reply_to => "a.sender",
:correlation_id => "r-1",
:message_id => "m-1",
:content_type => "application/octet-stream",
# just an example. MK.
:content_encoding => "zip/zap",
:routing_key => "march.hare.key")
latch.await
expect(@meta.routing_key).to eq("march.hare.key")
expect(@meta.content_type).to eq("application/octet-stream")
expect(@meta.content_encoding).to eq("zip/zap")
expect(@meta.priority).to eq(8)
time = Time.at(@meta.headers["time"].getTime/1000)
expect(time.to_i).to eq(@now.to_i)
expect(@meta.headers["coordinates"]["latitude"]).to eq(59.35)
expect(@meta.headers["participants"]).to eq(11)
expect(@meta.headers["true_field"]).to eq(true)
expect(@meta.headers["false_field"]).to eq(false)
expect(@meta.headers["nil_field"]).to be_nil
expect(@meta.timestamp).to eq(Time.at(@now.to_i))
expect(@meta.type).to eq("kinda.checkin")
expect(@meta.consumer_tag).not_to be_nil
expect(@meta.consumer_tag).not_to be_empty
expect(@meta.delivery_tag.to_i).to eq(1)
expect(@meta.delivery_mode).to eq(2)
expect(@meta).to be_persistent
expect(@meta.reply_to).to eq("a.sender")
expect(@meta.correlation_id).to eq("r-1")
expect(@meta.message_id).to eq("m-1")
expect(@meta).not_to be_redelivered
expect(@meta.app_id).to eq("march.hare.tests")
expect(@meta.exchange).to eq("amq.fanout")
end
end