@@ -94,43 +94,112 @@ defmodule Spotter.Testing.AmqpBlockingClient do
9494
9595 @ doc """
9696 Sends a new message without waiting for a response.
97+
98+ The `data` parameter represents a payload, added to the message body.
99+ The `ops` parameter represented as a keyword, that can contain keys:
100+
101+ * `:request_exchange` - Exchange key, through which will be published message.
102+ * `:request_routing_key` - Routing key, used for pushing message to the certain queue.
103+ * `:mandatory` - If set, returns an error if the broker can't route the message to a queue (default `false`);
104+ * `:immediate` - If set, returns an error if the broker can't deliver te message to a consumer immediately (default `false`);
105+ * `:content_type` - MIME Content type;
106+ * `:content_encoding` - MIME Content encoding;
107+ * `:headers` - Custom message headers;
108+ * `:persistent` - Determines delivery mode. Messages marked as `persistent` and delivered to `durable` \
109+ queues will be logged to disk;
110+ * `:correlation_id` - Application correlation identifier;
111+ * `:priority` - Message priority, ranging from 0 to 9;
112+ * `:reply_to` - Name of the reply queue;
113+ * `:expiration` - How long the message is valid (in milliseconds);
114+ * `:message_id` - Message identifier;
115+ * `:timestamp` - Timestamp associated with this message (epoch time);
116+ * `:type` - Message type (as a string);
117+ * `:user_id` - User ID. Validated by RabbitMQ against the active connection user;
118+ * `:app_id` - Publishing application ID.
119+
120+ The `call_timeout` parameter determines maximum amount time in milliseconds before exit from the method by timeout.
97121 """
98122 def send ( pid , data , opts , call_timeout \\ 5000 ) do
99123 GenServer . call ( pid , { :send , data , opts } , call_timeout )
100124 end
101125
102126 @ doc """
103127 Sends a new message and wait for result.
128+
129+ The `data` parameter represents a payload, added to the message body.
130+ The `ops` parameter represented as a keyword, that can contain keys:
131+
132+ * `:request_exchange` - Exchange key, through which will be published message. Required.
133+ * `:request_routing_key` - Routing key, used for pushing message to the certain queue. Required.
134+ * `:response_queue` - The name of the queue which will be used for tracking responses. Required.
135+ * `:channel_opts` - Keyword list which is used for creating response queue and linking it with the exchange. Required.
136+ * `:mandatory` - If set, returns an error if the broker can't route the message to a queue (default `false`);
137+ * `:immediate` - If set, returns an error if the broker can't deliver te message to a consumer immediately (default `false`);
138+ * `:content_type` - MIME Content type;
139+ * `:content_encoding` - MIME Content encoding;
140+ * `:headers` - Custom message headers;
141+ * `:persistent` - Determines delivery mode. Messages marked as `persistent` and delivered to `durable` \
142+ queues will be logged to disk;
143+ * `:correlation_id` - Application correlation identifier;
144+ * `:priority` - Message priority, ranging from 0 to 9;
145+ * `:reply_to` - Name of the reply queue;
146+ * `:expiration` - How long the message is valid (in milliseconds);
147+ * `:message_id` - Message identifier;
148+ * `:timestamp` - Timestamp associated with this message (epoch time);
149+ * `:type` - Message type (as a string);
150+ * `:user_id` - User ID. Validated by RabbitMQ against the active connection user;
151+ * `:app_id` - Publishing application ID.
152+
153+ The `timeout` parameter determines amout of time before doing next attempt to extract the message.
154+ The `attemps` parameter determines the general amount of attempts to extract the message.
155+ The `call_timeout` parameter determines maximum amount time in milliseconds before exit from the method by timeout.
104156 """
105157 def send_and_wait ( pid , data , opts , timeout \\ 1000 , attempts \\ 5 , call_timeout \\ 5000 ) do
106- GenServer . call ( pid , { :send_and_wait , data , opts , timeout , attempts } , call_timeout )
158+ try do
159+ GenServer . call ( pid , { :send_and_wait , data , opts , timeout , attempts } , call_timeout )
160+ catch
161+ :exit , _reason -> { :empty , nil }
162+ end
107163 end
108164
109165 @ doc """
110166 Returns the message from the certain queue if it exists.
167+
168+ The `queue` parameter represents the name of the queue which will be used for tracking responses.
169+ The `timeout` parameter determines amout of time before doing next attempt to extract the message.
170+ The `attemps` parameter determines the general amount of attempts to extract the message.
171+ The `call_timeout` parameter determines maximum amount time in milliseconds before exit from the method by timeout.
111172 """
112173 def consume ( pid , queue , timeout \\ 1000 , attempts \\ 5 , call_timeout \\ 500 ) do
113- GenServer . call ( pid , { :consume_response , queue , timeout , attempts } , call_timeout )
174+ try do
175+ GenServer . call ( pid , { :consume_response , queue , timeout , attempts } , call_timeout )
176+ catch
177+ :exit , _reason -> { :empty , nil }
178+ end
114179 end
115180
116181 @ doc """
117182 Initializes QoS, a queue and an exchanges for the channel.
183+
184+ The `channel_opts` parameters stores generic information about the response queue and the linked exchange.
185+ The `call_timeout` parameter determines maximum amount time in milliseconds before exit from the method by timeout.
118186 """
119187 def configure_channel ( pid , channel_opts , call_timeout \\ 500 ) do
120188 GenServer . call ( pid , { :configure_channel , channel_opts } , call_timeout )
121189 end
122190
123191 # Internal stuff
124192
125- defp send_message ( channel , routing_key , data , opts ) do
126- exchange_request = Keyword . get ( opts , :exchange_request , "" )
127- queue_request = Keyword . get ( opts , :queue_request , "" )
193+ defp send_message ( channel , data , opts ) do
194+ request_exchange = Keyword . get ( opts , :request_exchange , "" )
195+ request_routing_key = Keyword . get ( opts , :request_routing_key , "" )
196+ response_queue = Keyword . get ( opts , :response_queue , "" )
128197 publish_options = Keyword . merge ( opts , [
129198 persistent: Keyword . get ( opts , :persistent , true ) ,
130- reply_to: routing_key ,
199+ reply_to: response_queue ,
131200 content_type: Keyword . get ( opts , :content_type , "application/json" )
132201 ] )
133- AMQP.Basic . publish ( channel , exchange_request , queue_request , data , publish_options )
202+ AMQP.Basic . publish ( channel , request_exchange , request_routing_key , data , publish_options )
134203 end
135204
136205 defp consume_response ( channel , queue_name , timeout , attempts ) do
@@ -158,20 +227,22 @@ defmodule Spotter.Testing.AmqpBlockingClient do
158227 # Private API
159228
160229 def handle_call ( { :send , data , opts } , _from , state ) do
161- { :reply , send_message ( state [ :channel ] , :undefined , data , opts ) , state }
230+ { :reply , send_message ( state [ :channel ] , data , opts ) , state }
162231 end
163232
164233 def handle_call ( { :send_and_wait , data , opts , timeout , attempts } , _from , state ) do
165234 channel = state [ :channel ]
166235 channel_opts = state [ :channel_opts ]
167- queue_name = Keyword . get ( channel_opts [ :queue ] || [ ] , :name , :undefined )
168- routing_key = Keyword . get ( channel_opts [ :queue ] || [ ] , :routing_key , :undefined )
236+ response_queue = Keyword . get ( opts , :response_queue , "" )
169237
170238 configure ( channel , channel_opts )
171- send_message ( channel , routing_key , data , opts )
172- response = consume_response ( state [ :channel ] , queue_name , timeout , attempts )
239+ send_message ( channel , data , opts )
240+ response = consume_response ( state [ :channel ] , response_queue , timeout , attempts )
241+
242+ if response_queue != "" do
243+ AMQP.Queue . delete ( channel , response_queue )
244+ end
173245
174- AMQP.Queue . delete ( channel , queue_name )
175246 { :reply , response , state }
176247 end
177248
0 commit comments