bunny-mock 1.2.2 → 1.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 236588fb9cea816f91284a397efdf607f564077e
4
- data.tar.gz: 58a81920f3d8909260f4468f6f0fdab8a303c150
3
+ metadata.gz: 7ce7333898b5b71042b88715549e3aff6b578548
4
+ data.tar.gz: 576392f37adb5c7baf5c85eb3074b096bf75894d
5
5
  SHA512:
6
- metadata.gz: d924c1e85439de0784bd9821343d6422b18a9692f829a785e4ac3a6298390c075c88371ab1a0df22db4e81648884457f8cda85df504146c2a90d652df71b8f95
7
- data.tar.gz: 0c66093b0771d596a9e25724685e4c1846c9886c654897d8df55ef112d837fbc4df5ba4d831ef08bc4b068eb3a8b76afc28497338de45ce4e209249c2d1a6666
6
+ metadata.gz: 9aa0f6f2ad6557edbf70a984c0277396c69a820b2f8ed43f18e8e4be6ccdd654b1cc5993cff421a510713461a5755c599b6773ab28adfb1efb3ebcf76033953d
7
+ data.tar.gz: 94ea8ce5094702d17692bb653a9761313a6f6b9c0d02e9eabca040c9ca33e47bf447b8fd5cb97d02f04e1760f46cb5efd5ff8cb22395e322b90fc86b9d02bb0a
@@ -6,4 +6,7 @@ AllCops:
6
6
  Exclude:
7
7
  - 'spec/**/*'
8
8
 
9
+ Metrics/ClassLength:
10
+ Enabled: false
11
+
9
12
  inherit_from: .rubocop_todo.yml
@@ -24,22 +24,3 @@ Style/Documentation:
24
24
  - 'lib/bunny_mock/exchanges/headers.rb'
25
25
  - 'lib/bunny_mock/exchanges/topic.rb'
26
26
  - 'lib/bunny_mock/queue.rb'
27
-
28
- # Offense count: 9
29
- # Cop supports --auto-correct.
30
- # Configuration parameters: EnforcedStyle, SupportedStyles.
31
- # SupportedStyles: empty_lines, no_empty_lines
32
- Style/EmptyLinesAroundClassBody:
33
- Enabled: false
34
-
35
- # Offense count: 36
36
- # Cop supports --auto-correct.
37
- Style/EmptyLinesAroundMethodBody:
38
- Enabled: false
39
-
40
- # Offense count: 2
41
- # Cop supports --auto-correct.
42
- # Configuration parameters: EnforcedStyle, SupportedStyles.
43
- # SupportedStyles: empty_lines, no_empty_lines
44
- Style/EmptyLinesAroundModuleBody:
45
- Enabled: false
@@ -1,3 +1,8 @@
1
+ ## v1.3.0
2
+
3
+ * [#12](https://github.com/arempe93/bunny-mock/pull/12): Adds `basic_publish` functionality to `Channel` - [@podung](https://github.com/podung)
4
+ * [#13](https://github.com/arempe93/bunny-mock/pull/13): Add `confirm_select` method stub - [@baelter](https://github.com/baelter)
5
+
1
6
  ## v1.2.2
2
7
 
3
8
  * [#6](https://github.com/arempe93/bunny-mock/pull/6): Adds more status methods to `Session` - [@syndbg](https://github.com/syndbg)
data/README.md CHANGED
@@ -8,6 +8,10 @@ Bunny Mock
8
8
 
9
9
  A mock client for RabbitMQ, modeled after the popular [Bunny client](https://github.com/ruby-amqp/bunny). It currently supports basic usage of Bunny for managing exchanges and queues, with the goal of being able to handle and test all Bunny use cases.
10
10
 
11
+ ##### Upgrading
12
+
13
+ This project does its best to follow [semantic versioning practices](http://semver.org/). Check the [CHANGELOG](CHANGELOG.md) to see detailed versioning notes, and [UPGRADING](UPGRADING.md) for notes about major changes or deprecations.
14
+
11
15
  ## Usage
12
16
 
13
17
  BunnyMock can be injected into your RabbitMQ application in place of Bunny for testing. For example, if you have a helper module named `AMQFactory`, some code similar to the following placed in `spec_helper` or `test_helper` or what have you is all you need to start using BunnyMock to test your RabbitMQ application
@@ -43,10 +47,10 @@ it 'should create queues and exchanges' do
43
47
  expect(session.queue_exists?('queue.test')).to be_falsey
44
48
 
45
49
  xchg = channel.exchange 'xchg.test'
46
- expect(session.exchange_exists?('exchange.test')).to be_truthy
50
+ expect(session.exchange_exists?('xchg.test')).to be_truthy
47
51
 
48
52
  xchg.delete
49
- expect(session.exchange_exists?('exchange.test')).to be_falsey
53
+ expect(session.exchange_exists?('xchg.test')).to be_falsey
50
54
  end
51
55
  ```
52
56
 
@@ -76,6 +80,7 @@ it 'should route messages from exchanges' do
76
80
  xchg = channel.topic 'xchg.topic'
77
81
  queue = channel.queue 'queue.test'
78
82
 
83
+ queue.bind xchg
79
84
  xchg.publish 'Routed message', routing_key: '*.test'
80
85
 
81
86
  expect(queue.message_count).to eq(1)
@@ -119,7 +124,7 @@ it 'should bind exchanges to exchanges' do
119
124
 
120
125
  receiver.unbind source
121
126
  expect(receiver.bound_to?(source)).to be_falsey
122
- expect(xchg.routes_to?(receiver)).to be_falsey
127
+ expect(source.routes_to?(receiver)).to be_falsey
123
128
 
124
129
  receiver.bind 'xchg.source'
125
130
  expect(receiver.bound_to?(source)).to be_truthy
@@ -20,12 +20,10 @@ require 'bunny_mock/exchanges/headers'
20
20
  # @see https://github.com/ruby-amq/bunny
21
21
  #
22
22
  module BunnyMock
23
-
24
23
  # AMQP protocol version
25
24
  PROTOCOL_VERSION = AMQ::Protocol::PROTOCOL_VERSION
26
25
 
27
26
  class << self
28
-
29
27
  #
30
28
  # API
31
29
  #
@@ -36,7 +34,6 @@ module BunnyMock
36
34
  # @return [BunnyMock::Session] Session instance
37
35
  # @api public
38
36
  def new(*)
39
-
40
37
  # return new mock session
41
38
  BunnyMock::Session.new
42
39
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module BunnyMock
3
3
  class Channel
4
-
5
4
  #
6
5
  # API
7
6
  #
@@ -24,7 +23,6 @@ module BunnyMock
24
23
  # @api public
25
24
  #
26
25
  def initialize(connection = nil, id = nil)
27
-
28
26
  # store channel id
29
27
  @id = id
30
28
 
@@ -58,7 +56,6 @@ module BunnyMock
58
56
  # @api public
59
57
  #
60
58
  def open
61
-
62
59
  @status = :open
63
60
 
64
61
  self
@@ -71,7 +68,6 @@ module BunnyMock
71
68
  # @api public
72
69
  #
73
70
  def close
74
-
75
71
  @status = :closed
76
72
 
77
73
  self
@@ -100,10 +96,7 @@ module BunnyMock
100
96
  # @api public
101
97
  #
102
98
  def exchange(name, opts = {})
103
-
104
- xchg = @connection.find_exchange(name) || Exchange.declare(self, name, opts)
105
-
106
- @connection.register_exchange xchg
99
+ @connection.register_exchange xchg_find_or_create(name, opts)
107
100
  end
108
101
 
109
102
  ##
@@ -184,6 +177,23 @@ module BunnyMock
184
177
  direct '', no_declare: true
185
178
  end
186
179
 
180
+ ##
181
+ # Mocks Bunny::Channel#basic_publish
182
+ #
183
+ # @param [String] payload Message payload. It will never be modified by Bunny or RabbitMQ in any way.
184
+ # @param [String] exchange Exchange to publish to
185
+ # @param [String] routing_key Routing key
186
+ # @param [Hash] opts Publishing options
187
+
188
+ # @return [BunnyMock::Channel] Self
189
+ def basic_publish(payload, xchg, routing_key, opts = {})
190
+ xchg = xchg_find_or_create(xchg) unless xchg.respond_to? :name
191
+
192
+ xchg.publish payload, opts.merge(routing_key: routing_key)
193
+
194
+ self
195
+ end
196
+
187
197
  # @endgroup
188
198
 
189
199
  # @group Queue API
@@ -198,9 +208,7 @@ module BunnyMock
198
208
  # @api public
199
209
  #
200
210
  def queue(name = '', opts = {})
201
-
202
211
  queue = @connection.find_queue(name) || Queue.new(self, name, opts)
203
-
204
212
  @connection.register_queue queue
205
213
  end
206
214
 
@@ -214,10 +222,19 @@ module BunnyMock
214
222
  # @api public
215
223
  #
216
224
  def temporary_queue(opts = {})
217
-
218
225
  queue '', opts.merge(exclusive: true)
219
226
  end
220
227
 
228
+ ##
229
+ # Does nothing atm.
230
+ #
231
+ # @return nil
232
+ # @api public
233
+ #
234
+ def confirm_select(callback = nil)
235
+ # noop
236
+ end
237
+
221
238
  # @endgroup
222
239
 
223
240
  #
@@ -236,9 +253,7 @@ module BunnyMock
236
253
 
237
254
  # @private
238
255
  def queue_bind(queue, key, xchg)
239
-
240
256
  exchange = @connection.find_exchange xchg
241
-
242
257
  raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange
243
258
 
244
259
  exchange.add_route key, queue
@@ -246,9 +261,7 @@ module BunnyMock
246
261
 
247
262
  # @private
248
263
  def queue_unbind(key, xchg)
249
-
250
264
  exchange = @connection.find_exchange xchg
251
-
252
265
  raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange
253
266
 
254
267
  exchange.remove_route key
@@ -256,9 +269,7 @@ module BunnyMock
256
269
 
257
270
  # @private
258
271
  def xchg_bound_to?(receiver, key, name)
259
-
260
272
  source = @connection.find_exchange name
261
-
262
273
  raise Bunny::NotFound.new("Exchange '#{name}' was not found", self, false) unless source
263
274
 
264
275
  source.routes_to? receiver, routing_key: key
@@ -266,9 +277,7 @@ module BunnyMock
266
277
 
267
278
  # @private
268
279
  def xchg_routes_to?(key, xchg)
269
-
270
280
  exchange = @connection.find_exchange xchg
271
-
272
281
  raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange
273
282
 
274
283
  exchange.routes_to? key
@@ -276,9 +285,7 @@ module BunnyMock
276
285
 
277
286
  # @private
278
287
  def xchg_bind(receiver, routing_key, name)
279
-
280
288
  source = @connection.find_exchange name
281
-
282
289
  raise Bunny::NotFound.new("Exchange '#{name}' was not found", self, false) unless source
283
290
 
284
291
  source.add_route routing_key, receiver
@@ -286,12 +293,17 @@ module BunnyMock
286
293
 
287
294
  # @private
288
295
  def xchg_unbind(routing_key, name)
289
-
290
296
  source = @connection.find_exchange name
291
-
292
297
  raise Bunny::NotFound.new("Exchange '#{name}' was not found", self, false) unless source
293
298
 
294
299
  source.remove_route routing_key
295
300
  end
301
+
302
+ private
303
+
304
+ # @private
305
+ def xchg_find_or_create(name, opts = {})
306
+ @connection.find_exchange(name) || Exchange.declare(self, name, opts)
307
+ end
296
308
  end
297
309
  end
@@ -1,9 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  module BunnyMock
3
3
  class Exchange
4
-
5
4
  class << self
6
-
7
5
  ##
8
6
  # Create a new {BunnyMock::Exchange} instance
9
7
  #
@@ -20,7 +18,6 @@ module BunnyMock
20
18
  # @api public
21
19
  #
22
20
  def declare(channel, name = '', opts = {})
23
-
24
21
  # get requested type
25
22
  type = opts.fetch :type, :direct
26
23
 
@@ -69,7 +66,6 @@ module BunnyMock
69
66
 
70
67
  # @private
71
68
  def initialize(channel, name, type, opts)
72
-
73
69
  # store creation information
74
70
  @channel = channel
75
71
  @name = name
@@ -117,7 +113,6 @@ module BunnyMock
117
113
  # @api public
118
114
  #
119
115
  def publish(payload, opts = {})
120
-
121
116
  # handle message sending, varies by type
122
117
  deliver payload, opts, opts.fetch(:routing_key, '')
123
118
 
@@ -130,7 +125,6 @@ module BunnyMock
130
125
  # @api public
131
126
  #
132
127
  def delete(*)
133
-
134
128
  @channel.deregister_exchange self
135
129
 
136
130
  @deleted = true
@@ -148,12 +142,10 @@ module BunnyMock
148
142
  # @api public
149
143
  #
150
144
  def bind(exchange, opts = {})
151
-
152
145
  if exchange.respond_to?(:add_route)
153
146
 
154
147
  # we can do the binding ourselves
155
148
  exchange.add_route opts.fetch(:routing_key, @name), self
156
-
157
149
  else
158
150
 
159
151
  # we need the channel to look up the exchange
@@ -175,12 +167,10 @@ module BunnyMock
175
167
  # @api public
176
168
  #
177
169
  def unbind(exchange, opts = {})
178
-
179
170
  if exchange.respond_to?(:remove_route)
180
171
 
181
172
  # we can do the unbinding ourselves
182
173
  exchange.remove_route opts.fetch(:routing_key, @name)
183
-
184
174
  else
185
175
 
186
176
  # we need the channel to look up the exchange
@@ -204,12 +194,10 @@ module BunnyMock
204
194
  # @api public
205
195
  #
206
196
  def bound_to?(exchange, opts = {})
207
-
208
197
  if exchange.respond_to?(:routes_to?)
209
198
 
210
199
  # we can find out on the exchange object
211
200
  exchange.routes_to? self, opts
212
-
213
201
  else
214
202
 
215
203
  # we need the channel to look up the exchange
@@ -229,9 +217,7 @@ module BunnyMock
229
217
  # @api public
230
218
  #
231
219
  def routes_to?(exchange_or_queue, opts = {})
232
-
233
220
  route = exchange_or_queue.respond_to?(:name) ? exchange_or_queue.name : exchange_or_queue
234
-
235
221
  @routes.key? opts.fetch(:routing_key, route)
236
222
  end
237
223
 
@@ -2,7 +2,6 @@
2
2
  module BunnyMock
3
3
  module Exchanges
4
4
  class Direct < BunnyMock::Exchange
5
-
6
5
  #
7
6
  # API
8
7
  #
@@ -17,7 +16,6 @@ module BunnyMock
17
16
  # @api public
18
17
  #
19
18
  def deliver(payload, opts, key)
20
-
21
19
  @routes[key].publish payload, opts if @routes[key]
22
20
  end
23
21
  end
@@ -2,7 +2,6 @@
2
2
  module BunnyMock
3
3
  module Exchanges
4
4
  class Fanout < BunnyMock::Exchange
5
-
6
5
  #
7
6
  # API
8
7
  #
@@ -17,7 +16,6 @@ module BunnyMock
17
16
  # @api public
18
17
  #
19
18
  def deliver(payload, opts, _key)
20
-
21
19
  @routes.each_value { |destination| destination.publish(payload, opts) }
22
20
  end
23
21
  end
@@ -2,7 +2,6 @@
2
2
  module BunnyMock
3
3
  module Exchanges
4
4
  class Header < BunnyMock::Exchange
5
-
6
5
  # @private
7
6
  # @return [Regexp] Any match
8
7
  ANY = /^any$/i
@@ -25,7 +24,6 @@ module BunnyMock
25
24
  # @api public
26
25
  #
27
26
  def deliver(payload, opts, key)
28
-
29
27
  # ~: proper headers exchange implementation
30
28
  @routes[key].publish payload, opts if @routes[key]
31
29
  end
@@ -2,7 +2,6 @@
2
2
  module BunnyMock
3
3
  module Exchanges
4
4
  class Topic < BunnyMock::Exchange
5
-
6
5
  # @private
7
6
  # @return [String] Multiple subdomain wildcard
8
7
  MULTI_WILDCARD = '#'
@@ -25,7 +24,6 @@ module BunnyMock
25
24
  # @api public
26
25
  #
27
26
  def deliver(payload, opts, key)
28
-
29
27
  # escape periods with backslash for regex
30
28
  key = key.gsub('.', '\.')
31
29
 
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  module BunnyMock
3
3
  class Queue
4
-
5
4
  #
6
5
  # API
7
6
  #
@@ -25,7 +24,6 @@ module BunnyMock
25
24
  # @see BunnyMock::Channel#queue
26
25
  #
27
26
  def initialize(channel, name = '', opts = {})
28
-
29
27
  # Store creation information
30
28
  @channel = channel
31
29
  @name = name
@@ -63,7 +61,6 @@ module BunnyMock
63
61
  # @api public
64
62
  #
65
63
  def publish(payload, opts = {})
66
-
67
64
  check_queue_deleted!
68
65
 
69
66
  # add to messages
@@ -83,14 +80,12 @@ module BunnyMock
83
80
  # @api public
84
81
  #
85
82
  def bind(exchange, opts = {})
86
-
87
83
  check_queue_deleted!
88
84
 
89
85
  if exchange.respond_to?(:add_route)
90
86
 
91
87
  # we can do the binding ourselves
92
88
  exchange.add_route opts.fetch(:routing_key, @name), self
93
-
94
89
  else
95
90
 
96
91
  # we need the channel to lookup the exchange
@@ -109,14 +104,12 @@ module BunnyMock
109
104
  # @api public
110
105
  #
111
106
  def unbind(exchange, opts = {})
112
-
113
107
  check_queue_deleted!
114
108
 
115
109
  if exchange.respond_to?(:remove_route)
116
110
 
117
111
  # we can do the unbinding ourselves
118
112
  exchange.remove_route opts.fetch(:routing_key, @name)
119
-
120
113
  else
121
114
 
122
115
  # we need the channel to lookup the exchange
@@ -138,14 +131,12 @@ module BunnyMock
138
131
  # @api public
139
132
  #
140
133
  def bound_to?(exchange, opts = {})
141
-
142
134
  check_queue_deleted!
143
135
 
144
136
  if exchange.respond_to?(:routes_to?)
145
137
 
146
138
  # we can do the check ourselves
147
139
  exchange.routes_to? opts.fetch(:routing_key, @name)
148
-
149
140
  else
150
141
 
151
142
  # we need the channel to lookup the exchange
@@ -198,9 +189,7 @@ module BunnyMock
198
189
  # @api public
199
190
  #
200
191
  def delete
201
-
202
192
  @channel.deregister_queue self
203
-
204
193
  @deleted = true
205
194
  end
206
195
 
@@ -2,7 +2,6 @@
2
2
  module BunnyMock
3
3
  # Mocks Bunny::Session
4
4
  class Session
5
-
6
5
  #
7
6
  # API
8
7
  #
@@ -21,7 +20,6 @@ module BunnyMock
21
20
  #
22
21
  # @api public
23
22
  def initialize(*)
24
-
25
23
  # not connected until {BunnyMock::Session#start} is called
26
24
  @status = :not_connected
27
25
 
@@ -39,9 +37,7 @@ module BunnyMock
39
37
  # @return [BunnyMock::Session] self
40
38
  # @api public
41
39
  def start
42
-
43
40
  @status = :connected
44
-
45
41
  self
46
42
  end
47
43
 
@@ -52,7 +48,6 @@ module BunnyMock
52
48
  # @api public
53
49
  def stop
54
50
  @status = :closed
55
-
56
51
  self
57
52
  end
58
53
  alias close stop
@@ -94,7 +89,6 @@ module BunnyMock
94
89
  # @return [BunnyMock::Channel] Channel instance
95
90
  # @api public
96
91
  def create_channel(n = nil, _pool_size = 1)
97
-
98
92
  # raise same error as {Bunny::Session#create_channel}
99
93
  raise ArgumentError, 'channel number 0 is reserved in the protocol and cannot be used' if n == 0
100
94
 
@@ -2,7 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module BunnyMock
5
-
6
5
  # @return [String] Version of the library
7
- VERSION = '1.2.2'
6
+ VERSION = '1.3.0'
8
7
  end
@@ -114,6 +114,49 @@ describe BunnyMock::Channel do
114
114
  end
115
115
  end
116
116
 
117
+ context '#basic_publish' do
118
+ let(:xchg_name) { 'testing.xchg' }
119
+ let(:key) { 'routing.key' }
120
+ let(:data) { { some: 'data' } }
121
+
122
+ let(:xchg) { @channel.direct xchg_name }
123
+ let(:queue) { @channel.queue 'testing.queue' }
124
+
125
+ before do
126
+ queue.bind(xchg, routing_key: key)
127
+ end
128
+
129
+ it 'returns BunnyMock::Channel#self' do
130
+ result = @channel.basic_publish(data, xchg_name, key)
131
+
132
+ expect(result).to eq @channel
133
+ end
134
+
135
+ it 'should publish to the exchange' do
136
+ @channel.basic_publish(data, xchg_name, key)
137
+
138
+ expect(queue.pop[:message]).to eq data
139
+ end
140
+
141
+ it 'accepts exchange object for exchange param' do
142
+ @channel.basic_publish(data, xchg, key)
143
+
144
+ expect(queue.pop[:message]).to eq data
145
+ end
146
+
147
+ it 'passes opts down to exchange' do
148
+ @channel.basic_publish(data, xchg, key, extra: 'opts')
149
+
150
+ expect(queue.pop[:options]).to include(extra: 'opts')
151
+ end
152
+
153
+ it 'creates exchange if it does not exist' do
154
+ @channel.basic_publish(data, 'some.other.xchg', key)
155
+
156
+ expect(@channel.exchange('some.other.xchg')).to be_kind_of BunnyMock::Exchange
157
+ end
158
+ end
159
+
117
160
  context '#queue' do
118
161
 
119
162
  it 'should declare a new queue' do
@@ -22,4 +22,14 @@ describe BunnyMock do
22
22
  expect(BunnyMock.protocol_version).to eq('0.9.1')
23
23
  end
24
24
  end
25
+
26
+ it 'should route messages from exchanges' do
27
+ channel = BunnyMock.new.start.channel
28
+
29
+ xchg = channel.topic 'xchg.topic'
30
+ queue = channel.queue 'queue.test'
31
+
32
+ queue.bind xchg
33
+ xchg.publish('Routed message', routing_key: '*.test')
34
+ end
25
35
  end
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.2.2
4
+ version: 1.3.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-03-28 00:00:00.000000000 Z
11
+ date: 2016-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny