bunny-mock 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
1
2
  require 'bunny_mock/version'
2
- require 'amq/protocol/client'
3
3
 
4
- require 'bunny_mock/exceptions'
4
+ require 'bunny/exceptions'
5
+ require 'amq/protocol/client'
5
6
 
6
7
  require 'bunny_mock/session'
7
8
  require 'bunny_mock/channel'
@@ -20,31 +21,31 @@ require 'bunny_mock/exchanges/headers'
20
21
  #
21
22
  module BunnyMock
22
23
 
23
- # AMQP protocol version
24
- PROTOCOL_VERSION = AMQ::Protocol::PROTOCOL_VERSION
25
-
26
- #
27
- # API
28
- #
29
-
30
- ##
31
- # Instantiate a new mock Bunny session
32
- #
33
- # @return [BunnyMock::Session] Session instance
34
- # @api public
35
- def self.new(*args)
36
-
37
- # return new mock session
38
- BunnyMock::Session.new
39
- end
40
-
41
- # @return [String] Bunny mock version
42
- def self.version
43
- VERSION
44
- end
45
-
46
- # @return [String] AMQP protocol version
47
- def self.protocol_version
48
- AMQ::Protocol::PROTOCOL_VERSION
49
- end
24
+ # AMQP protocol version
25
+ PROTOCOL_VERSION = AMQ::Protocol::PROTOCOL_VERSION
26
+
27
+ #
28
+ # API
29
+ #
30
+
31
+ ##
32
+ # Instantiate a new mock Bunny session
33
+ #
34
+ # @return [BunnyMock::Session] Session instance
35
+ # @api public
36
+ def self.new(*)
37
+
38
+ # return new mock session
39
+ BunnyMock::Session.new
40
+ end
41
+
42
+ # @return [String] Bunny mock version
43
+ def self.version
44
+ VERSION
45
+ end
46
+
47
+ # @return [String] AMQP protocol version
48
+ def self.protocol_version
49
+ AMQ::Protocol::PROTOCOL_VERSION
50
+ end
50
51
  end
@@ -1,296 +1,297 @@
1
+ # frozen_string_literal: true
1
2
  module BunnyMock
2
- class Channel
3
-
4
- #
5
- # API
6
- #
7
-
8
- # @return [Integer] Channel identifier
9
- attr_reader :id
10
-
11
- # @return [BunnyMock::Session] Session this channel belongs to
12
- attr_reader :connection
13
-
14
- # @return [Symbol] Current channel state
15
- attr_reader :status
16
-
17
- ##
18
- # Create a new {BunnyMock::Channel} instance
19
- #
20
- # @param [BunnyMock::Session] connection Mocked session instance
21
- # @param [Integer] id Channel identifier
22
- #
23
- # @api public
24
- #
25
- def initialize(connection = nil, id = nil)
26
-
27
- # store channel id
28
- @id = id
29
-
30
- # store connection information
31
- @connection = connection
32
-
33
- # initialize exchange and queue storage
34
- @exchanges = Hash.new
35
- @queues = Hash.new
36
-
37
- # set status to opening
38
- @status = :opening
39
- end
40
-
41
- # @return [Boolean] true if status is open, false otherwise
42
- # @api public
43
- def open?
44
- @status == :open
45
- end
46
-
47
- # @return [Boolean] true if status is closed, false otherwise
48
- # @api public
49
- def closed?
50
- @status == :closed
51
- end
52
-
53
- ##
54
- # Sets status to open
55
- #
56
- # @return [BunnyMock::Channel] self
57
- # @api public
58
- #
59
- def open
60
-
61
- @status = :open
62
-
63
- self
64
- end
65
-
66
- ##
67
- # Sets status to closed
68
- #
69
- # @return [BunnyMock::Channel] self
70
- # @api public
71
- #
72
- def close
73
-
74
- @status = :closed
75
-
76
- self
77
- end
78
-
79
- # @return [String] Object representation
80
- def to_s
81
- "#<#{self.class.name}:#{self.object_id} @id=#{@id} @open=#{open?}>"
82
- end
83
- alias inspect to_s
84
-
85
- # @group Exchange API
86
-
87
- ##
88
- # Mocks an exchange
89
- #
90
- # @param [String] name Exchange name
91
- # @param [Hash] opts Exchange parameters
92
- #
93
- # @option opts [Symbol,String] :type Type of exchange
94
- # @option opts [Boolean] :durable
95
- # @option opts [Boolean] :auto_delete
96
- # @option opts [Hash] :arguments
97
- #
98
- # @return [BunnyMock::Exchange] Mocked exchange instance
99
- # @api public
100
- #
101
- def exchange(name, opts = {})
102
-
103
- xchg = @connection.find_exchange(name) || Exchange.declare(self, name, opts)
104
-
105
- @connection.register_exchange xchg
106
- end
107
-
108
- ##
109
- # Mocks a fanout exchange
110
- #
111
- # @param [String] name Exchange name
112
- # @param [Hash] opts Exchange parameters
113
- #
114
- # @option opts [Boolean] :durable
115
- # @option opts [Boolean] :auto_delete
116
- # @option opts [Hash] :arguments
117
- #
118
- # @return [BunnyMock::Exchange] Mocked exchange instance
119
- # @api public
120
- #
121
- def fanout(name, opts = {})
122
- self.exchange name, opts.merge(type: :fanout)
123
- end
124
-
125
- ##
126
- # Mocks a direct exchange
127
- #
128
- # @param [String] name Exchange name
129
- # @param [Hash] opts Exchange parameters
130
- #
131
- # @option opts [Boolean] :durable
132
- # @option opts [Boolean] :auto_delete
133
- # @option opts [Hash] :arguments
134
- #
135
- # @return [BunnyMock::Exchange] Mocked exchange instance
136
- # @api public
137
- #
138
- def direct(name, opts = {})
139
- self.exchange name, opts.merge(type: :direct)
140
- end
141
-
142
- ##
143
- # Mocks a topic exchange
144
- #
145
- # @param [String] name Exchange name
146
- # @param [Hash] opts Exchange parameters
147
- #
148
- # @option opts [Boolean] :durable
149
- # @option opts [Boolean] :auto_delete
150
- # @option opts [Hash] :arguments
151
- #
152
- # @return [BunnyMock::Exchange] Mocked exchange instance
153
- # @api public
154
- #
155
- def topic(name, opts = {})
156
- self.exchange name, opts.merge(type: :topic)
157
- end
158
-
159
- ##
160
- # Mocks a headers exchange
161
- #
162
- # @param [String] name Exchange name
163
- # @param [Hash] opts Exchange parameters
164
- #
165
- # @option opts [Boolean] :durable
166
- # @option opts [Boolean] :auto_delete
167
- # @option opts [Hash] :arguments
168
- #
169
- # @return [BunnyMock::Exchange] Mocked exchange instance
170
- # @api public
171
- #
172
- def header(name, opts = {})
173
- self.exchange name, opts.merge(type: :header)
174
- end
175
-
176
- ##
177
- # Mocks RabbitMQ default exchange
178
- #
179
- # @return [BunnyMock::Exchange] Mocked default exchange instance
180
- # @api public
181
- #
182
- def default_exchange
183
- self.direct '', no_declare: true
184
- end
185
-
186
- # @endgroup
187
-
188
- # @group Queue API
189
-
190
- ##
191
- # Create a new {BunnyMock::Queue} instance, or find in channel cache
192
- #
193
- # @param [String] name Name of queue
194
- # @param [Hash] opts Queue creation options
195
- #
196
- # @return [BunnyMock::Queue] Queue that was mocked or looked up
197
- # @api public
198
- #
199
- def queue(name = '', opts = {})
200
-
201
- queue = @connection.find_queue(name) || Queue.new(self, name, opts)
202
-
203
- @connection.register_queue queue
204
- end
205
-
206
- ##
207
- # Create a new {BunnyMock::Queue} instance with no name
208
- #
209
- # @param [Hash] opts Queue creation options
210
- #
211
- # @return [BunnyMock::Queue] Queue that was mocked or looked up
212
- # @see #queue
213
- # @api public
214
- #
215
- def temporary_queue(opts = {})
216
-
217
- queue '', opts.merge(exclusive: true)
218
- end
219
-
220
- # @endgroup
221
-
222
- #
223
- # Implementation
224
- #
225
-
226
- # @private
227
- def deregister_queue(queue)
228
- @connection.deregister_queue queue.name
229
- end
230
-
231
- # @private
232
- def deregister_exchange(xchg)
233
- @connection.deregister_exchange xchg.name
234
- end
235
-
236
- # @private
237
- def queue_bind(queue, key, xchg)
238
-
239
- exchange = @connection.find_exchange xchg
240
-
241
- raise NotFound.new "Exchange '#{xchg}' was not found" unless exchange
242
-
243
- exchange.add_route key, queue
244
- end
3
+ class Channel
4
+
5
+ #
6
+ # API
7
+ #
8
+
9
+ # @return [Integer] Channel identifier
10
+ attr_reader :id
11
+
12
+ # @return [BunnyMock::Session] Session this channel belongs to
13
+ attr_reader :connection
14
+
15
+ # @return [Symbol] Current channel state
16
+ attr_reader :status
17
+
18
+ ##
19
+ # Create a new {BunnyMock::Channel} instance
20
+ #
21
+ # @param [BunnyMock::Session] connection Mocked session instance
22
+ # @param [Integer] id Channel identifier
23
+ #
24
+ # @api public
25
+ #
26
+ def initialize(connection = nil, id = nil)
27
+
28
+ # store channel id
29
+ @id = id
30
+
31
+ # store connection information
32
+ @connection = connection
33
+
34
+ # initialize exchange and queue storage
35
+ @exchanges = {}
36
+ @queues = {}
37
+
38
+ # set status to opening
39
+ @status = :opening
40
+ end
41
+
42
+ # @return [Boolean] true if status is open, false otherwise
43
+ # @api public
44
+ def open?
45
+ @status == :open
46
+ end
47
+
48
+ # @return [Boolean] true if status is closed, false otherwise
49
+ # @api public
50
+ def closed?
51
+ @status == :closed
52
+ end
53
+
54
+ ##
55
+ # Sets status to open
56
+ #
57
+ # @return [BunnyMock::Channel] self
58
+ # @api public
59
+ #
60
+ def open
61
+
62
+ @status = :open
63
+
64
+ self
65
+ end
66
+
67
+ ##
68
+ # Sets status to closed
69
+ #
70
+ # @return [BunnyMock::Channel] self
71
+ # @api public
72
+ #
73
+ def close
74
+
75
+ @status = :closed
76
+
77
+ self
78
+ end
79
+
80
+ # @return [String] Object representation
81
+ def to_s
82
+ "#<#{self.class.name}:#{object_id} @id=#{@id} @open=#{open?}>"
83
+ end
84
+ alias inspect to_s
85
+
86
+ # @group Exchange API
87
+
88
+ ##
89
+ # Mocks an exchange
90
+ #
91
+ # @param [String] name Exchange name
92
+ # @param [Hash] opts Exchange parameters
93
+ #
94
+ # @option opts [Symbol,String] :type Type of exchange
95
+ # @option opts [Boolean] :durable
96
+ # @option opts [Boolean] :auto_delete
97
+ # @option opts [Hash] :arguments
98
+ #
99
+ # @return [BunnyMock::Exchange] Mocked exchange instance
100
+ # @api public
101
+ #
102
+ def exchange(name, opts = {})
103
+
104
+ xchg = @connection.find_exchange(name) || Exchange.declare(self, name, opts)
105
+
106
+ @connection.register_exchange xchg
107
+ end
108
+
109
+ ##
110
+ # Mocks a fanout exchange
111
+ #
112
+ # @param [String] name Exchange name
113
+ # @param [Hash] opts Exchange parameters
114
+ #
115
+ # @option opts [Boolean] :durable
116
+ # @option opts [Boolean] :auto_delete
117
+ # @option opts [Hash] :arguments
118
+ #
119
+ # @return [BunnyMock::Exchange] Mocked exchange instance
120
+ # @api public
121
+ #
122
+ def fanout(name, opts = {})
123
+ exchange name, opts.merge(type: :fanout)
124
+ end
125
+
126
+ ##
127
+ # Mocks a direct exchange
128
+ #
129
+ # @param [String] name Exchange name
130
+ # @param [Hash] opts Exchange parameters
131
+ #
132
+ # @option opts [Boolean] :durable
133
+ # @option opts [Boolean] :auto_delete
134
+ # @option opts [Hash] :arguments
135
+ #
136
+ # @return [BunnyMock::Exchange] Mocked exchange instance
137
+ # @api public
138
+ #
139
+ def direct(name, opts = {})
140
+ exchange name, opts.merge(type: :direct)
141
+ end
142
+
143
+ ##
144
+ # Mocks a topic exchange
145
+ #
146
+ # @param [String] name Exchange name
147
+ # @param [Hash] opts Exchange parameters
148
+ #
149
+ # @option opts [Boolean] :durable
150
+ # @option opts [Boolean] :auto_delete
151
+ # @option opts [Hash] :arguments
152
+ #
153
+ # @return [BunnyMock::Exchange] Mocked exchange instance
154
+ # @api public
155
+ #
156
+ def topic(name, opts = {})
157
+ exchange name, opts.merge(type: :topic)
158
+ end
159
+
160
+ ##
161
+ # Mocks a headers exchange
162
+ #
163
+ # @param [String] name Exchange name
164
+ # @param [Hash] opts Exchange parameters
165
+ #
166
+ # @option opts [Boolean] :durable
167
+ # @option opts [Boolean] :auto_delete
168
+ # @option opts [Hash] :arguments
169
+ #
170
+ # @return [BunnyMock::Exchange] Mocked exchange instance
171
+ # @api public
172
+ #
173
+ def header(name, opts = {})
174
+ exchange name, opts.merge(type: :header)
175
+ end
176
+
177
+ ##
178
+ # Mocks RabbitMQ default exchange
179
+ #
180
+ # @return [BunnyMock::Exchange] Mocked default exchange instance
181
+ # @api public
182
+ #
183
+ def default_exchange
184
+ direct '', no_declare: true
185
+ end
186
+
187
+ # @endgroup
188
+
189
+ # @group Queue API
190
+
191
+ ##
192
+ # Create a new {BunnyMock::Queue} instance, or find in channel cache
193
+ #
194
+ # @param [String] name Name of queue
195
+ # @param [Hash] opts Queue creation options
196
+ #
197
+ # @return [BunnyMock::Queue] Queue that was mocked or looked up
198
+ # @api public
199
+ #
200
+ def queue(name = '', opts = {})
201
+
202
+ queue = @connection.find_queue(name) || Queue.new(self, name, opts)
203
+
204
+ @connection.register_queue queue
205
+ end
206
+
207
+ ##
208
+ # Create a new {BunnyMock::Queue} instance with no name
209
+ #
210
+ # @param [Hash] opts Queue creation options
211
+ #
212
+ # @return [BunnyMock::Queue] Queue that was mocked or looked up
213
+ # @see #queue
214
+ # @api public
215
+ #
216
+ def temporary_queue(opts = {})
217
+
218
+ queue '', opts.merge(exclusive: true)
219
+ end
220
+
221
+ # @endgroup
222
+
223
+ #
224
+ # Implementation
225
+ #
226
+
227
+ # @private
228
+ def deregister_queue(queue)
229
+ @connection.deregister_queue queue.name
230
+ end
231
+
232
+ # @private
233
+ def deregister_exchange(xchg)
234
+ @connection.deregister_exchange xchg.name
235
+ end
236
+
237
+ # @private
238
+ def queue_bind(queue, key, xchg)
239
+
240
+ exchange = @connection.find_exchange xchg
241
+
242
+ raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange
243
+
244
+ exchange.add_route key, queue
245
+ end
245
246
 
246
- # @private
247
- def queue_unbind(key, xchg)
247
+ # @private
248
+ def queue_unbind(key, xchg)
248
249
 
249
- exchange = @connection.find_exchange xchg
250
+ exchange = @connection.find_exchange xchg
250
251
 
251
- raise NotFound.new "Exchange '#{xchg}' was not found" unless exchange
252
+ raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange
252
253
 
253
- exchange.remove_route key
254
- end
254
+ exchange.remove_route key
255
+ end
255
256
 
256
- # @private
257
- def xchg_bound_to?(receiver, key, name)
257
+ # @private
258
+ def xchg_bound_to?(receiver, key, name)
258
259
 
259
- source = @connection.find_exchange name
260
+ source = @connection.find_exchange name
260
261
 
261
- raise NotFound.new "Exchange '#{name}' was not found" unless source
262
+ raise Bunny::NotFound.new("Exchange '#{name}' was not found", self, false) unless source
262
263
 
263
- source.has_binding? receiver, routing_key: key
264
- end
264
+ source.routes_to? receiver, routing_key: key
265
+ end
265
266
 
266
- # @private
267
- def xchg_has_binding?(key, xchg)
267
+ # @private
268
+ def xchg_routes_to?(key, xchg)
268
269
 
269
- exchange = @connection.find_exchange xchg
270
+ exchange = @connection.find_exchange xchg
270
271
 
271
- raise NotFound.new "Exchange '#{xchg}' was not found" unless exchange
272
+ raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange
272
273
 
273
- exchange.has_binding? key
274
- end
274
+ exchange.routes_to? key
275
+ end
275
276
 
276
- # @private
277
- def xchg_bind(receiver, routing_key, name)
277
+ # @private
278
+ def xchg_bind(receiver, routing_key, name)
278
279
 
279
- source = @connection.find_exchange name
280
+ source = @connection.find_exchange name
280
281
 
281
- raise NotFound.new "Exchange '#{name}' was not found" unless source
282
+ raise Bunny::NotFound.new("Exchange '#{name}' was not found", self, false) unless source
282
283
 
283
- source.add_route routing_key, receiver
284
- end
284
+ source.add_route routing_key, receiver
285
+ end
285
286
 
286
- # @private
287
- def xchg_unbind(routing_key, name)
287
+ # @private
288
+ def xchg_unbind(routing_key, name)
288
289
 
289
- source = @connection.find_exchange name
290
+ source = @connection.find_exchange name
290
291
 
291
- raise NotFound.new "Exchange '#{name}' was not found" unless source
292
+ raise Bunny::NotFound.new("Exchange '#{name}' was not found", self, false) unless source
292
293
 
293
- source.remove_route routing_key
294
- end
295
- end
294
+ source.remove_route routing_key
295
+ end
296
+ end
296
297
  end