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
168 lines (127 loc) · 5.11 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
defmodule GenRMQ.PublisherTest do
use ExUnit.Case, async: false
use GenRMQ.RabbitCase
alias GenRMQ.Publisher
alias GenRMQ.Test.Assert
@uri "amqp://guest:guest@localhost:5672"
@exchange "gen_rmq_out_exchange"
@out_queue "gen_rmq_out_queue"
defmodule TestPublisher do
@behaviour GenRMQ.Publisher
def init() do
[
exchange: "gen_rmq_out_exchange",
uri: "amqp://guest:guest@localhost:5672",
app_id: :my_app_id
]
end
end
setup_all do
{:ok, conn} = rmq_open(@uri)
:ok = setup_out_queue(conn, @out_queue, @exchange)
{:ok, rabbit_conn: conn, out_queue: @out_queue}
end
setup do
purge_queues(@uri, [@out_queue])
end
describe "start_link/2" do
test "should start a new publisher" do
{:ok, pid} = GenRMQ.Publisher.start_link(TestPublisher)
assert Process.alive?(pid)
end
test "should start a new publisher registered by name" do
{:ok, pid} = GenRMQ.Publisher.start_link(TestPublisher, name: TestPublisher)
assert Process.whereis(TestPublisher) == pid
end
end
describe "GenRMQ.Publisher" do
setup do
with_test_publisher()
end
test "should publish message", %{publisher: publisher_pid} = context do
message = %{"msg" => "msg"}
GenRMQ.Publisher.publish(publisher_pid, Poison.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, Poison.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"}
GenRMQ.Publisher.publish(publisher_pid, Poison.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"}
GenRMQ.Publisher.publish(
publisher_pid,
Poison.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 publish a message with priority", %{publisher: publisher_pid} = context do
message = %{"msg" => "with prio"}
GenRMQ.Publisher.publish(
publisher_pid,
Poison.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)
GenRMQ.Publisher.publish(publisher_pid, Poison.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
defp with_test_publisher(module \\ TestPublisher) 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
end