This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
gen_rmq_publisher_test.exs
420 lines (324 loc) · 14.2 KB
/
gen_rmq_publisher_test.exs
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
defmodule GenRMQ.PublisherTest do
use ExUnit.Case, async: false
use GenRMQ.RabbitCase
alias GenRMQ.Publisher
alias GenRMQ.Test.Assert
alias TestPublisher.{
Default,
RedeclaringExistingExchange,
WithConfirmations,
WithDefaultExchange
}
@connection "amqp://guest:guest@localhost:5672"
@exchange "gen_rmq_out_exchange"
@out_queue "gen_rmq_out_queue"
@invalid_queue "invalid_queue"
setup_all do
{:ok, conn} = rmq_open(@connection)
:ok = setup_out_queue(conn, @out_queue, @exchange)
{:ok, rabbit_conn: conn, out_queue: @out_queue}
end
setup do
purge_queues(@connection, [@out_queue])
end
describe "start_link/2" do
test "should start a new publisher" do
{:ok, pid} = GenRMQ.Publisher.start_link(Default)
assert Process.alive?(pid)
end
test "should start a new publisher registered by name" do
{:ok, pid} = GenRMQ.Publisher.start_link(Default, name: Default)
assert Process.whereis(Default) == pid
end
test "should fail when try to redeclare an exchange with different type", %{rabbit_conn: conn} do
Process.flag(:trap_exit, true)
{:ok, chan} = open_channel(conn)
GenRMQ.Binding.declare_exchange(chan, {:direct, RedeclaringExistingExchange.existing_exchange()})
{:ok, pid} = GenRMQ.Publisher.start_link(RedeclaringExistingExchange, name: RedeclaringExistingExchange)
assert_receive {:EXIT, ^pid, {{:shutdown, {:server_initiated_close, _, _}}, _}}
end
end
describe "TestPublisher.Default" do
setup do
with_test_publisher(Default)
end
test "should publish message", %{publisher: publisher_pid} = context do
message = %{"msg" => "msg"}
:ok = GenRMQ.Publisher.publish(publisher_pid, Jason.encode!(%{"msg" => "msg"}))
Assert.repeatedly(fn -> assert out_queue_count(context) >= 1 end)
{:ok, received_message, meta} = get_message_from_queue(context)
assert message == received_message
assert "" == meta[:routing_key]
assert [] == meta[:headers]
end
test "should publish message with custom routing key", %{publisher: publisher_pid} = context do
message = %{"msg" => "msg"}
GenRMQ.Publisher.publish(publisher_pid, Jason.encode!(message), "some.routing.key")
Assert.repeatedly(fn -> assert out_queue_count(context) >= 1 end)
{:ok, received_message, meta} = get_message_from_queue(context)
assert message == received_message
assert "some.routing.key" == meta[:routing_key]
assert [] == meta[:headers]
end
test "should publish message with headers", %{publisher: publisher_pid} = context do
message = %{"msg" => "msg"}
:ok = GenRMQ.Publisher.publish(publisher_pid, Jason.encode!(message), "some.routing.key", header1: "value")
Assert.repeatedly(fn -> assert out_queue_count(context) >= 1 end)
{:ok, received_message, meta} = get_message_from_queue(context)
assert message == received_message
assert [{"header1", :longstr, "value"}] == meta[:headers]
end
test "should override standard metadata fields from headers", %{publisher: publisher_pid} = context do
message = %{"msg" => "msg"}
:ok =
GenRMQ.Publisher.publish(
publisher_pid,
Jason.encode!(message),
"some.routing.key",
message_id: "message_id_1",
correlation_id: "correlation_id_1",
header1: "value"
)
Assert.repeatedly(fn -> assert out_queue_count(context) >= 1 end)
{:ok, received_message, meta} = get_message_from_queue(context)
assert message == received_message
assert meta[:message_id] == "message_id_1"
assert meta[:correlation_id] == "correlation_id_1"
refute meta[:header1]
assert [{"header1", :longstr, "value"}] == meta[:headers]
end
test "should override default metadata fields", %{publisher: publisher_pid} = context do
message = %{"msg" => "msg"}
sent_metadata = [app_id: "custom_id", content_type: "text/plain", timestamp: 1]
:ok = GenRMQ.Publisher.publish(publisher_pid, Jason.encode!(message), "some.routing.key", sent_metadata)
Assert.repeatedly(fn -> assert out_queue_count(context) >= 1 end)
{:ok, _received_message, received_metadata} = get_message_from_queue(context)
assert received_metadata[:app_id] == sent_metadata[:app_id]
assert received_metadata[:content_type] == sent_metadata[:content_type]
assert received_metadata[:timestamp] == sent_metadata[:timestamp]
end
test "should publish a message with priority", %{publisher: publisher_pid} = context do
message = %{"msg" => "with prio"}
:ok =
GenRMQ.Publisher.publish(
publisher_pid,
Jason.encode!(message),
"some.routing.key",
priority: 100
)
Assert.repeatedly(fn -> assert out_queue_count(context) >= 1 end)
{:ok, received_message, meta} = get_message_from_queue(context)
assert message == received_message
assert meta[:priority] == 100
end
test "should reconnect after connection failure", %{publisher: publisher_pid, state: state} = context do
message = %{"msg" => "pub_disc"}
Process.exit(state.channel.conn.pid, :kill)
Assert.repeatedly(fn ->
new_state = :sys.get_state(publisher_pid)
assert new_state.channel.conn.pid != state.channel.conn.pid
end)
:ok = GenRMQ.Publisher.publish(publisher_pid, Jason.encode!(message))
Assert.repeatedly(fn -> assert out_queue_count(context) >= 1 end)
{:ok, received_message, meta} = get_message_from_queue(context)
assert message == received_message
assert "" == meta[:routing_key]
assert [] == meta[:headers]
end
test "should close connection and channel on termination", %{publisher: publisher_pid, state: state} do
Process.exit(publisher_pid, :shutdown)
Assert.repeatedly(fn ->
assert Process.alive?(state.conn.pid) == false
assert Process.alive?(state.channel.pid) == false
end)
end
end
describe "consumer_count/2" do
setup do
with_test_publisher(Default)
end
test "should return the correct number of consumers when a valid queue is provided", %{publisher: publisher_pid} do
assert 0 == GenRMQ.Publisher.consumer_count(publisher_pid, @out_queue)
end
test "should raise an error when an invalid queue is provided", %{publisher: publisher_pid} do
catch_exit do
GenRMQ.Publisher.consumer_count(publisher_pid, @invalid_queue)
end
assert_receive {:EXIT, ^publisher_pid, _error_details}
end
end
describe "empty?/2" do
setup do
with_test_publisher(Default)
end
test "should return true if the provided queue is empty", %{publisher: publisher_pid} do
assert true == GenRMQ.Publisher.empty?(publisher_pid, @out_queue)
end
test "should return false if the provided queue is not empty", %{publisher: publisher_pid} = context do
messages = generate_messages(5)
Enum.each(messages, fn message ->
:ok = GenRMQ.Publisher.publish(publisher_pid, Jason.encode!(message))
end)
Assert.repeatedly(fn -> assert out_queue_count(context) == 5 end)
assert false == GenRMQ.Publisher.empty?(publisher_pid, @out_queue)
end
test "should raise an error if the provided queue is invalid", %{publisher: publisher_pid} do
catch_exit do
GenRMQ.Publisher.empty?(publisher_pid, @invalid_queue)
end
assert_receive {:EXIT, ^publisher_pid, _error_details}
end
end
describe "message_count/2" do
setup do
with_test_publisher(Default)
end
test "should return the correct number of messages when a valid empty queue is provided", %{
publisher: publisher_pid
} do
assert 0 == GenRMQ.Publisher.message_count(publisher_pid, @out_queue)
end
test "should return the correct number of messages when a valid queue is provided",
%{publisher: publisher_pid} = context do
messages = generate_messages(5)
Enum.each(messages, fn message ->
:ok = GenRMQ.Publisher.publish(publisher_pid, Jason.encode!(message))
end)
Assert.repeatedly(fn -> assert out_queue_count(context) == 5 end)
assert length(messages) == GenRMQ.Publisher.message_count(publisher_pid, @out_queue)
end
test "should raise an error when an invalid queue is provided", %{publisher: publisher_pid} do
catch_exit do
GenRMQ.Publisher.message_count(publisher_pid, @invalid_queue)
end
assert_receive {:EXIT, ^publisher_pid, _error_details}
end
end
describe "purge/2" do
setup do
with_test_publisher(Default)
end
test "should return an ok tuple when a valid empty queue is successfully purged", %{publisher: publisher_pid} do
assert {:ok, %{message_count: 0}} == GenRMQ.Publisher.purge(publisher_pid, @out_queue)
end
test "should return an ok tuple when a valid queue with messages is successfully purged",
%{publisher: publisher_pid} = context do
messages = generate_messages(5)
Enum.each(messages, fn message ->
:ok = GenRMQ.Publisher.publish(publisher_pid, Jason.encode!(message))
end)
Assert.repeatedly(fn -> assert out_queue_count(context) == 5 end)
assert {:ok, %{message_count: 5}} == GenRMQ.Publisher.purge(publisher_pid, @out_queue)
assert {:ok, %{message_count: 0}} == GenRMQ.Publisher.purge(publisher_pid, @out_queue)
end
test "should raise an error when an invalid queue is provided", %{publisher: publisher_pid} do
catch_exit do
GenRMQ.Publisher.purge(publisher_pid, @invalid_queue)
end
assert_receive {:EXIT, ^publisher_pid, _error_details}
end
end
describe "status/2" do
setup do
with_test_publisher(Default)
end
test "should return an ok tuple when a valid empty queue is successfully purged", %{publisher: publisher_pid} do
assert {:ok, %{message_count: 0, consumer_count: 0, queue: "gen_rmq_out_queue"}} ==
GenRMQ.Publisher.status(publisher_pid, @out_queue)
end
test "should return an ok tuple when a valid queue with messages is successfully purged",
%{publisher: publisher_pid} = context do
messages = generate_messages(5)
Enum.each(messages, fn message ->
:ok = GenRMQ.Publisher.publish(publisher_pid, Jason.encode!(message))
end)
Assert.repeatedly(fn -> assert out_queue_count(context) == 5 end)
assert {:ok, %{message_count: 5, consumer_count: 0, queue: "gen_rmq_out_queue"}} ==
GenRMQ.Publisher.status(publisher_pid, @out_queue)
end
test "should raise an error when an invalid queue is provided", %{publisher: publisher_pid} do
catch_exit do
GenRMQ.Publisher.status(publisher_pid, @invalid_queue)
end
assert_receive {:EXIT, ^publisher_pid, _error_details}
end
end
describe "TestPublisher.WithConfirmations" do
setup do
with_test_publisher(WithConfirmations)
end
test "should publish a message and wait for a confirmation", %{publisher: publisher_pid} = context do
message = %{"msg" => "with confirmation"}
publish_result = GenRMQ.Publisher.publish(publisher_pid, Jason.encode!(message), "some.routing.key")
Assert.repeatedly(fn -> assert out_queue_count(context) >= 1 end)
assert match?({:ok, ^message, _}, get_message_from_queue(context))
assert {:ok, :confirmed} == publish_result
end
end
describe "TestPublisher.WithDefaultExchange" do
setup do
with_test_publisher(WithDefaultExchange)
end
test "should publish a message to the default exchange", %{publisher: publisher_pid} = context do
message = %{"msg" => "with default exchange"}
# https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchange-default
# all queues are bound to the default exchange with a queue name as a binding key
routing_key = context.out_queue
publish_result = GenRMQ.Publisher.publish(publisher_pid, Jason.encode!(message), routing_key)
Assert.repeatedly(fn -> assert out_queue_count(context) >= 1 end)
assert match?({:ok, ^message, _}, get_message_from_queue(context))
assert {:ok, :confirmed} == publish_result
end
end
describe "Telemetry events" do
setup :attach_telemetry_handlers
setup do
with_test_publisher(Default)
end
test "should be emitted when the publisher start and completes setup" do
assert_receive {:telemetry_event, [:gen_rmq, :publisher, :connection, :start], %{system_time: _}, %{exchange: _}}
assert_receive {:telemetry_event, [:gen_rmq, :publisher, :connection, :stop], %{duration: _}, %{exchange: _}}
end
test "should be emitted when the publisher starts and completes the publishing of a message",
%{publisher: publisher_pid} = context do
message = %{"msg" => "msg"}
:ok = GenRMQ.Publisher.publish(publisher_pid, Jason.encode!(message))
Assert.repeatedly(fn -> assert out_queue_count(context) >= 1 end)
{:ok, _received_message, _meta} = get_message_from_queue(context)
assert_receive {:telemetry_event, [:gen_rmq, :publisher, :message, :start], %{system_time: _},
%{exchange: _, message: _}}
assert_receive {:telemetry_event, [:gen_rmq, :publisher, :message, :stop], %{duration: _},
%{exchange: _, message: _}}
end
end
defp attach_telemetry_handlers(%{test: test}) do
self = self()
:ok =
:telemetry.attach_many(
"#{test}",
[
[:gen_rmq, :publisher, :connection, :start],
[:gen_rmq, :publisher, :connection, :stop],
[:gen_rmq, :publisher, :message, :start],
[:gen_rmq, :publisher, :message, :stop]
],
fn name, measurements, metadata, _ ->
send(self, {:telemetry_event, name, measurements, metadata})
end,
nil
)
end
defp with_test_publisher(module) do
Process.flag(:trap_exit, true)
{:ok, publisher_pid} = Publisher.start_link(module)
state = :sys.get_state(publisher_pid)
on_exit(fn -> Process.exit(publisher_pid, :normal) end)
{:ok, %{publisher: publisher_pid, state: state}}
end
defp generate_messages(num_messages) do
1..num_messages
|> Enum.map(fn num ->
%{"message_#{num}" => "message_#{num}"}
end)
end
end