@@ -101,6 +101,36 @@ defmodule Rabbit.ConsumerTest do
101101 end
102102 end
103103
104+ defmodule TroublesomeTestConsumer do
105+ use Rabbit.Consumer
106+
107+ @ impl Rabbit.Consumer
108+ def init ( :consumer , opts ) do
109+ { :ok , opts }
110+ end
111+
112+ @ impl Rabbit.Consumer
113+ def handle_setup ( state ) do
114+ attempt = Agent . get_and_update ( state . setup_opts [ :counter ] , fn n -> { n , n + 1 } end )
115+
116+ if attempt == 0 do
117+ { :error , :something_went_wrong }
118+ else
119+ AMQP.Queue . declare ( state . channel , state . queue , auto_delete: true )
120+ :ok
121+ end
122+ end
123+
124+ @ impl Rabbit.Consumer
125+ def handle_message ( _msg ) do
126+ end
127+
128+ @ impl Rabbit.Consumer
129+ def handle_error ( _ ) do
130+ :ok
131+ end
132+ end
133+
104134 setup do
105135 { :ok , connection } = Connection . start_link ( TestConnection )
106136 { :ok , producer } = Producer . start_link ( TestProducer , connection: connection )
@@ -118,6 +148,34 @@ defmodule Rabbit.ConsumerTest do
118148 end
119149 end
120150
151+ describe "start_link/3 with :sync_start" do
152+ test "starts consumer" , meta do
153+ assert { :ok , consumer } =
154+ Consumer . start_link ( TestConsumer ,
155+ connection: meta . connection ,
156+ queue: "consumer" ,
157+ sync_start: true
158+ )
159+
160+ assert % { started_mode: :sync , consuming: true } = get_state ( consumer )
161+ end
162+
163+ test "starts consumer with multiple attempts" , meta do
164+ { :ok , counter } = Agent . start ( fn -> 0 end )
165+
166+ assert { :ok , consumer } =
167+ Consumer . start_link ( TroublesomeTestConsumer ,
168+ connection: meta . connection ,
169+ queue: "consumer" ,
170+ sync_start: true ,
171+ setup_opts: [ counter: counter ]
172+ )
173+
174+ assert Agent . get ( counter , & & 1 ) == 2
175+ assert % { started_mode: :sync , consuming: true } = get_state ( consumer )
176+ end
177+ end
178+
121179 describe "stop/1" do
122180 test "stops consumer" , meta do
123181 assert { :ok , consumer , _queue } = start_consumer ( meta )
@@ -128,7 +186,7 @@ defmodule Rabbit.ConsumerTest do
128186 test "disconnects the amqp channel" , meta do
129187 assert { :ok , consumer , _queue } = start_consumer ( meta )
130188
131- state = GenServer . call ( consumer , :state )
189+ state = get_state ( consumer )
132190
133191 assert Process . alive? ( state . channel . pid )
134192 assert :ok = Consumer . stop ( consumer )
@@ -143,10 +201,10 @@ defmodule Rabbit.ConsumerTest do
143201 assert { :ok , consumer , _queue } = start_consumer ( meta )
144202
145203 connection_state = connection_state ( meta . connection )
146- consumer_state1 = GenServer . call ( consumer , :state )
204+ consumer_state1 = get_state ( consumer )
147205 AMQP.Connection . close ( connection_state . connection )
148206 await_consuming ( consumer )
149- consumer_state2 = GenServer . call ( consumer , :state )
207+ consumer_state2 = get_state ( consumer )
150208
151209 assert consumer_state1 . channel . pid != consumer_state2 . channel . pid
152210 end
@@ -256,7 +314,7 @@ defmodule Rabbit.ConsumerTest do
256314 end
257315
258316 defp await_consuming ( consumer ) do
259- state = GenServer . call ( consumer , :state )
317+ state = get_state ( consumer )
260318
261319 if state . consuming do
262320 :ok
@@ -270,7 +328,11 @@ defmodule Rabbit.ConsumerTest do
270328 :crypto . strong_rand_bytes ( 8 ) |> Base . encode64 ( )
271329 end
272330
331+ defp get_state ( consumer ) do
332+ GenServer . call ( consumer , :state )
333+ end
334+
273335 defp connection_state ( connection ) do
274- Connection . transaction ( connection , & GenServer . call ( & 1 , :state ) )
336+ Connection . transaction ( connection , & get_state / 1 )
275337 end
276338end
0 commit comments