bunny-mock 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d54d4507d1c57efad12f4d1525154cde983b98b7
4
- data.tar.gz: d5986217aed023311d8ac3903627d36cbead2a10
3
+ metadata.gz: c50a06b6ad300048718d1da4576cf4aa26bb3f7a
4
+ data.tar.gz: d3a46df9f089b40a09bf763a55dec107f1adde22
5
5
  SHA512:
6
- metadata.gz: 58a3e975090251bcbe4dcf40d4534e1c2382e151b50280a59d2524a20485a30c9bfff7ed34af448f3a5fbce9c481aa0fcc0f4184568c98caa73eac06bf013905
7
- data.tar.gz: 81960ae54b49872107f122ed8c681934392940dd3698fe0c20a3736fbf3aafd8244925edae01858b3b0133af99a1cb880c3759ae730076bd7873f105f52a2208
6
+ metadata.gz: a66ab64795f09af460aecb30c702a177caeed40f20603a2680bdccbc8c2307f6c312fb73ec1de039c4a787c21bf671569ae7ecacae9e59143e238d27b174e852
7
+ data.tar.gz: fa47402ad74277744d11cbf0c560476ea507379b385d0ab2453f0d5ba1589d4938777244cd9b86a7241d608f47ade3b8535f5aa200217d3e9ef513ca480a0a44
@@ -1,3 +1,11 @@
1
+ ## Next release
2
+
3
+ _Add contribution here_
4
+
5
+ ## v1.5.0
6
+
7
+ *[#20](https://github.com/arempe93/bunny-mock/pull/20): Adds implementation of `Queue#subscribe` - [@baelter](https://github.com/baelter)
8
+
1
9
  ## v1.4.0
2
10
 
3
11
  * [#19](https://github.com/arempe93/bunny-mock/pull/12): Adds support for JRuby with Bunny 1.7.0 - [@TimothyMDean](https://github.com/TimothyMDean)
data/README.md CHANGED
@@ -163,7 +163,7 @@ View the documentation on [RubyDoc](http://www.rubydoc.info/github/arempe93/bunn
163
163
 
164
164
  * [Bunny](https://github.com/ruby-amqp/bunny) - To use original exception classes
165
165
 
166
- * Ruby version >= 2.0 (A requirement of Bunny)
166
+ * ~~Ruby version >= 2.0 (A requirement of Bunny)~~ Now works with other Ruby versions (even JRuby!) thanks to **[@TimothyMDean](https://github.com/TimothyMDean)**
167
167
 
168
168
  ## License
169
169
 
@@ -235,6 +235,46 @@ module BunnyMock
235
235
  # noop
236
236
  end
237
237
 
238
+ ##
239
+ # Does nothing atm.
240
+ #
241
+ # @return nil
242
+ # @api public
243
+ #
244
+ def prefetch(*)
245
+ # noop
246
+ end
247
+
248
+ ##
249
+ # Does not actually wait, but always return true.
250
+ #
251
+ # @return true
252
+ # @api public
253
+ #
254
+ def wait_for_confirms(*)
255
+ true
256
+ end
257
+
258
+ ##
259
+ # Does nothing atm.
260
+ #
261
+ # @return nil
262
+ # @api public
263
+ #
264
+ def acknowledge(*)
265
+ # noop
266
+ end
267
+
268
+ ##
269
+ # Does nothing atm.
270
+ #
271
+ # @return nil
272
+ # @api public
273
+ #
274
+ def reject(*)
275
+ # noop
276
+ end
277
+
238
278
  # @endgroup
239
279
 
240
280
  #
@@ -260,11 +300,11 @@ module BunnyMock
260
300
  end
261
301
 
262
302
  # @private
263
- def queue_unbind(key, xchg)
303
+ def queue_unbind(queue, key, xchg)
264
304
  exchange = @connection.find_exchange xchg
265
305
  raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange
266
306
 
267
- exchange.remove_route key
307
+ exchange.remove_route key, queue
268
308
  end
269
309
 
270
310
  # @private
@@ -276,11 +316,11 @@ module BunnyMock
276
316
  end
277
317
 
278
318
  # @private
279
- def xchg_routes_to?(key, xchg)
319
+ def xchg_routes_to?(queue, key, xchg)
280
320
  exchange = @connection.find_exchange xchg
281
321
  raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange
282
322
 
283
- exchange.routes_to? key
323
+ exchange.routes_to? queue, routing_key: key
284
324
  end
285
325
 
286
326
  # @private
@@ -292,11 +332,11 @@ module BunnyMock
292
332
  end
293
333
 
294
334
  # @private
295
- def xchg_unbind(routing_key, name)
335
+ def xchg_unbind(routing_key, name, exchange)
296
336
  source = @connection.find_exchange name
297
337
  raise Bunny::NotFound.new("Exchange '#{name}' was not found", self, false) unless source
298
338
 
299
- source.remove_route routing_key
339
+ source.remove_route routing_key, exchange
300
340
  end
301
341
 
302
342
  private
@@ -168,13 +168,11 @@ module BunnyMock
168
168
  #
169
169
  def unbind(exchange, opts = {})
170
170
  if exchange.respond_to?(:remove_route)
171
-
172
171
  # we can do the unbinding ourselves
173
- exchange.remove_route opts.fetch(:routing_key, @name)
172
+ exchange.remove_route opts.fetch(:routing_key, @name), self
174
173
  else
175
-
176
174
  # we need the channel to look up the exchange
177
- @channel.xchg_unbind opts.fetch(:routing_key, @name), exchange
175
+ @channel.xchg_unbind opts.fetch(:routing_key, @name), exchange, self
178
176
  end
179
177
 
180
178
  self
@@ -195,7 +193,6 @@ module BunnyMock
195
193
  #
196
194
  def bound_to?(exchange, opts = {})
197
195
  if exchange.respond_to?(:routes_to?)
198
-
199
196
  # we can find out on the exchange object
200
197
  exchange.routes_to? self, opts
201
198
  else
@@ -218,7 +215,8 @@ module BunnyMock
218
215
  #
219
216
  def routes_to?(exchange_or_queue, opts = {})
220
217
  route = exchange_or_queue.respond_to?(:name) ? exchange_or_queue.name : exchange_or_queue
221
- @routes.key? opts.fetch(:routing_key, route)
218
+ rk = opts.fetch(:routing_key, route)
219
+ @routes.key?(rk) && @routes[rk].any? { |r| r == exchange_or_queue }
222
220
  end
223
221
 
224
222
  def has_binding?(exchange_or_queue, opts = {}) # rubocop:disable Style/PredicateName
@@ -245,12 +243,17 @@ module BunnyMock
245
243
 
246
244
  # @private
247
245
  def add_route(key, xchg_or_queue)
248
- @routes[key] = xchg_or_queue
246
+ @routes[key] ||= []
247
+ @routes[key] << xchg_or_queue
249
248
  end
250
249
 
251
250
  # @private
252
- def remove_route(key)
253
- @routes.delete key
251
+ def remove_route(key, xchg_or_queue)
252
+ instance = xchg_or_queue.respond_to?(:name) ? xchg_or_queue.name : xchg_or_queue
253
+ @routes[key].delete_if do |r|
254
+ route = r.respond_to?(:name) ? r.name : r
255
+ route == instance
256
+ end if @routes.key? key
254
257
  end
255
258
  end
256
259
  end
@@ -16,7 +16,7 @@ module BunnyMock
16
16
  # @api public
17
17
  #
18
18
  def deliver(payload, opts, key)
19
- @routes[key].publish payload, opts if @routes[key]
19
+ @routes[key].each { |route| route.publish payload, opts } if @routes[key]
20
20
  end
21
21
  end
22
22
  end
@@ -16,7 +16,7 @@ module BunnyMock
16
16
  # @api public
17
17
  #
18
18
  def deliver(payload, opts, _key)
19
- @routes.each_value { |destination| destination.publish(payload, opts) }
19
+ @routes.values.flatten.each { |destination| destination.publish(payload, opts) }
20
20
  end
21
21
  end
22
22
  end
@@ -25,7 +25,7 @@ module BunnyMock
25
25
  #
26
26
  def deliver(payload, opts, key)
27
27
  # ~: proper headers exchange implementation
28
- @routes[key].publish payload, opts if @routes[key]
28
+ @routes[key].each { |route| route.publish payload, opts } if @routes[key]
29
29
  end
30
30
  end
31
31
  end
@@ -25,7 +25,7 @@ module BunnyMock
25
25
  #
26
26
  def deliver(payload, opts, key)
27
27
  delivery_routes = @routes.dup.keep_if { |route, _| key =~ route_to_regex(route) }
28
- delivery_routes.values.each { |dest| dest.publish(payload, opts) }
28
+ delivery_routes.values.flatten.each { |dest| dest.publish(payload, opts) }
29
29
  end
30
30
 
31
31
  private
@@ -65,6 +65,22 @@ module BunnyMock
65
65
 
66
66
  # add to messages
67
67
  @messages << { message: payload, options: opts }
68
+ yield_consumers
69
+ self
70
+ end
71
+
72
+ ##
73
+ # Adds a consumer to the queue (subscribes for message deliveries).
74
+ #
75
+ # All params are ignored atm. Takes a block which is called when a message is delivered
76
+ # to the queue
77
+ #
78
+ # @api public
79
+ #
80
+ def subscribe(*_args, &block)
81
+ @consumers ||= []
82
+ @consumers << block
83
+ yield_consumers
68
84
 
69
85
  self
70
86
  end
@@ -109,11 +125,11 @@ module BunnyMock
109
125
  if exchange.respond_to?(:remove_route)
110
126
 
111
127
  # we can do the unbinding ourselves
112
- exchange.remove_route opts.fetch(:routing_key, @name)
128
+ exchange.remove_route opts.fetch(:routing_key, @name), self
113
129
  else
114
130
 
115
131
  # we need the channel to lookup the exchange
116
- @channel.queue_unbind opts.fetch(:routing_key, @name), exchange
132
+ @channel.queue_unbind self, opts.fetch(:routing_key, @name), exchange
117
133
  end
118
134
  end
119
135
 
@@ -134,13 +150,11 @@ module BunnyMock
134
150
  check_queue_deleted!
135
151
 
136
152
  if exchange.respond_to?(:routes_to?)
137
-
138
153
  # we can do the check ourselves
139
- exchange.routes_to? opts.fetch(:routing_key, @name)
154
+ exchange.routes_to? self, opts
140
155
  else
141
-
142
156
  # we need the channel to lookup the exchange
143
- @channel.xchg_routes_to? opts.fetch(:routing_key, @name), exchange
157
+ @channel.xchg_routes_to? self, opts.fetch(:routing_key, @name), exchange
144
158
  end
145
159
  end
146
160
 
@@ -223,5 +237,17 @@ module BunnyMock
223
237
 
224
238
  [di, mp, message[:message]]
225
239
  end
240
+
241
+ # @private
242
+ def yield_consumers
243
+ return if @consumers.nil?
244
+ @consumers.each do |c|
245
+ # rubocop:disable AssignmentInCondition
246
+ while message = all.pop
247
+ response = pop_response(message)
248
+ c.call(response)
249
+ end
250
+ end
251
+ end
226
252
  end
227
253
  end
@@ -3,5 +3,5 @@
3
3
 
4
4
  module BunnyMock
5
5
  # @return [String] Version of the library
6
- VERSION = '1.4.0'
6
+ VERSION = '1.5.0'
7
7
  end
@@ -0,0 +1,44 @@
1
+ describe BunnyMock::Queue, '#subscribe' do
2
+ before do
3
+ @ch1 = @session.channel
4
+ @ch2 = @session.channel
5
+ end
6
+
7
+ context 'when delevered to an exchange' do
8
+ it 'should be delevered in all queues bound to the routing key' do
9
+ t = @ch1.topic 'amq.topic'
10
+ q1 = @ch1.queue 'q1'
11
+ q2 = @ch1.queue 'q2'
12
+ q3 = @ch2.queue 'q3'
13
+ q4 = @ch2.queue 'q4'
14
+ q1.bind(t, routing_key: 'rk1')
15
+ q2.bind(t, routing_key: 'rk1')
16
+ q3.bind(t, routing_key: 'rk1')
17
+ q4.bind(t, routing_key: 'rk1')
18
+
19
+ delivered = 0
20
+ q1.subscribe do |_, _, body|
21
+ expect(body).to eq 'test'
22
+ delivered += 1
23
+ end
24
+
25
+ q2.subscribe do |_, _, body|
26
+ expect(body).to eq 'test'
27
+ delivered += 1
28
+ end
29
+
30
+ q3.subscribe do |_, _, body|
31
+ expect(body).to eq 'test'
32
+ delivered += 1
33
+ end
34
+
35
+ q4.subscribe do |_, _, body|
36
+ expect(body).to eq 'test'
37
+ delivered += 1
38
+ end
39
+
40
+ t.publish('test', { routing_key: 'rk1' })
41
+ expect(delivered).to eq 4
42
+ end
43
+ end
44
+ end
@@ -164,6 +164,16 @@ describe BunnyMock::Queue do
164
164
  end
165
165
  end
166
166
 
167
+ context '#subscribe' do
168
+
169
+ it 'should consume messages delivered' do
170
+ @queue.subscribe do |_delivery, _headers, body|
171
+ expect(body).to eq('test')
172
+ end
173
+ @queue.publish 'test'
174
+ end
175
+ end
176
+
167
177
  context '#purge' do
168
178
 
169
179
  it 'should clear all messages' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bunny-mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Rempe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-27 00:00:00.000000000 Z
11
+ date: 2016-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny
@@ -129,6 +129,7 @@ files:
129
129
  - lib/bunny_mock/session.rb
130
130
  - lib/bunny_mock/version.rb
131
131
  - spec/integration/queue_pop_spec.rb
132
+ - spec/integration/queue_subscribe_spec.rb
132
133
  - spec/spec_helper.rb
133
134
  - spec/unit/bunny_mock/channel_spec.rb
134
135
  - spec/unit/bunny_mock/exchange_spec.rb
@@ -164,6 +165,7 @@ specification_version: 4
164
165
  summary: Mocking for the popular Bunny client for RabbitMQ
165
166
  test_files:
166
167
  - spec/integration/queue_pop_spec.rb
168
+ - spec/integration/queue_subscribe_spec.rb
167
169
  - spec/spec_helper.rb
168
170
  - spec/unit/bunny_mock/channel_spec.rb
169
171
  - spec/unit/bunny_mock/exchange_spec.rb