bunny-mock 1.0.0 → 1.1.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: 845c697ca137bf613dc4b11eccaeafdaad4d6cf8
4
- data.tar.gz: 4b4322cf0a31e2557bf3088fc621e0d0d153087d
3
+ metadata.gz: 1a5612c89a3f3570ab35b3bf6b56edb8e898ba11
4
+ data.tar.gz: e9d0d142ca7ef7bdd3ef2eb193097118550d2b62
5
5
  SHA512:
6
- metadata.gz: 7a7e65dea49c9844023700baf5fb46bd2b69255d2736c0b1dece7189698482bf7d538a411bfca438a82cfcb694e84697f8dae209f0f1295393f602da0a72fb33
7
- data.tar.gz: 82c071096f87d173b139870b42e555f8ffc0219c1f662a77ef14eb94d6b3b6b1f245a44e237c4934d1316b308b2fe31b27b96dbc563c33ba8caa2bd2b07897f5
6
+ metadata.gz: ee490d3e0ec271a295f8c802d34d568e9f8b17bdc702c083b3ab84943cfcbbd5b98fe278f0f79e432609abac19e0f8264986a043cce62f7cd182dd7ebb7c4fbe
7
+ data.tar.gz: ee1bdd4f57f0f8b8b7c7a4cf8a8e48857c63251e4d35ddd9207fc473ccafcc651d3d2089aed61c1d83053a570c12527497b3e2117e0648fdc8f5652952eb6cbe
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore CHANGED
@@ -21,3 +21,4 @@ debug/*
21
21
  *.dump
22
22
  deploy.docs.sh
23
23
  .ruby-version
24
+ coverage/
@@ -0,0 +1,8 @@
1
+ ## v1.1.0
2
+
3
+ * Moves queue and exchange storage to BunnyMock::Session
4
+ * Adds queue_exists? and exchange_exists? methods to BunnyMock::Session
5
+
6
+ ## v1.0.0
7
+
8
+ First stable release!
data/Gemfile CHANGED
@@ -8,7 +8,7 @@ group :development do
8
8
  end
9
9
 
10
10
  group :test do
11
-
11
+ gem 'coveralls'
12
12
  gem 'rspec', '~> 3.4.0'
13
13
  end
14
14
 
data/README.md CHANGED
@@ -2,16 +2,15 @@ Bunny Mock
2
2
  ==========
3
3
 
4
4
  [![Build Status](https://travis-ci.org/arempe93/bunny-mock.svg?branch=master)](https://travis-ci.org/arempe93/bunny-mock)
5
+ [![Gem Version](https://badge.fury.io/rb/bunny-mock.svg)](https://rubygems.org/gems/bunny-mock)
6
+ [![Coverage Status](https://coveralls.io/repos/arempe93/bunny-mock/badge.svg?branch=master&service=github)](https://coveralls.io/github/arempe93/bunny-mock?branch=master)
7
+ [![Documentation](http://inch-ci.org/github/arempe93/bunny-mock.svg?branch=master)](http://www.rubydoc.info/github/arempe93/bunny-mock)
5
8
 
6
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.
7
10
 
8
11
  ## Usage
9
12
 
10
- BunnyMock can be injected into your RabbitMQ application in place of Bunny for testing.
11
-
12
- ### Example
13
-
14
- Consider the following example of an RabibtMQ helper module, in Rails
13
+ BunnyMock can be injected into your RabbitMQ application in place of Bunny for testing. Consider the following example of an RabibtMQ helper module, in Rails
15
14
 
16
15
  ```ruby
17
16
  require 'bunny'
@@ -54,7 +53,31 @@ end
54
53
 
55
54
  In this case, to set up your tests, you can simply set `AMQP::Factory.connection = BunnyMock.new.start` to inject the mock library. Then you can use the mock helpers in your tests.
56
55
 
57
- ### Publishing
56
+ ## Examples
57
+
58
+ #### Declaration
59
+
60
+ ```ruby
61
+ it 'should create queues and exchanges' do
62
+
63
+ session = BunnyMock.new.start
64
+ channel = session.channel
65
+
66
+ queue = channel.queue 'queue.test'
67
+ expect(session.queue_exists?('queue.test')).to be_truthy
68
+
69
+ queue.delete
70
+ expect(session.queue_exists?('queue.test')).to be_falsey
71
+
72
+ xchg = channel.exchange 'xchg.test'
73
+ expect(session.exchange_exists?('exchange.test')).to be_truthy
74
+
75
+ xchg.delete
76
+ expect(session.exchange_exists?('exchange.test')).to be_falsey
77
+ end
78
+ ```
79
+
80
+ #### Publishing
58
81
 
59
82
  ```ruby
60
83
  it 'should publish messages to queues' do
@@ -72,9 +95,22 @@ it 'should publish messages to queues' do
72
95
  expect(payload[:message]).to eq('Testing message')
73
96
  expect(payload[:options][:priority]).to eq(5)
74
97
  end
98
+
99
+ it 'should route messages from exchanges' do
100
+
101
+ channel = BunnyMock.new.start.channel
102
+
103
+ xchg = channel.topic 'xchg.topic'
104
+ queue = channel.queue 'queue.test'
105
+
106
+ xchg.publish 'Routed message', routing_key: '*.test'
107
+
108
+ expect(queue.message_count).to eq(1)
109
+ expect(queue.pop[:message]).to eq('Routed message')
110
+ end
75
111
  ```
76
112
 
77
- ### Binding
113
+ #### Binding
78
114
 
79
115
  ```ruby
80
116
  it 'should bind queues to exchanges' do
@@ -1,6 +1,8 @@
1
1
  require 'bunny_mock/version'
2
2
  require 'amq/protocol/client'
3
3
 
4
+ require 'bunny_mock/exceptions'
5
+
4
6
  require 'bunny_mock/session'
5
7
  require 'bunny_mock/channel'
6
8
  require 'bunny_mock/exchange'
@@ -14,12 +14,6 @@ module BunnyMock
14
14
  # @return [Symbol] Current channel state
15
15
  attr_reader :status
16
16
 
17
- # @return [Hash<String, BunnyMock::Exchange>] Exchanges created by this channel
18
- attr_reader :exchanges
19
-
20
- # @return [Hash<String, BunnyMock::Queue>] Queues created by this channel
21
- attr_reader :queues
22
-
23
17
  ##
24
18
  # Create a new {BunnyMock::Channel} instance
25
19
  #
@@ -106,9 +100,9 @@ module BunnyMock
106
100
  #
107
101
  def exchange(name, opts = {})
108
102
 
109
- xchg = find_exchange(name) || Exchange.declare(self, name, opts)
103
+ xchg = @connection.find_exchange(name) || Exchange.declare(self, name, opts)
110
104
 
111
- register_exchange xchg
105
+ @connection.register_exchange xchg
112
106
  end
113
107
 
114
108
  ##
@@ -204,9 +198,9 @@ module BunnyMock
204
198
  #
205
199
  def queue(name = '', opts = {})
206
200
 
207
- queue = find_queue(name) || Queue.new(self, name, opts)
201
+ queue = @connection.find_queue(name) || Queue.new(self, name, opts)
208
202
 
209
- register_queue queue
203
+ @connection.register_queue queue
210
204
  end
211
205
 
212
206
  ##
@@ -230,24 +224,21 @@ module BunnyMock
230
224
  #
231
225
 
232
226
  # @private
233
- def find_queue(name)
234
- @queues[name]
235
- end
236
-
237
- # @private
238
- def register_queue(queue)
239
- @queues[queue.name] = queue
227
+ def deregister_queue(queue)
228
+ @connection.deregister_queue queue.name
240
229
  end
241
230
 
242
231
  # @private
243
- def deregister_queue(queue)
244
- @queues.delete queue.name
232
+ def deregister_exchange(xchg)
233
+ @connection.deregister_exchange xchg.name
245
234
  end
246
235
 
247
236
  # @private
248
237
  def queue_bind(queue, key, xchg)
249
238
 
250
- exchange = @exchanges[xchg] || exchange(xchg)
239
+ exchange = @connection.find_exchange xchg
240
+
241
+ raise NotFound.new "Exchange '#{xchg}' was not found" unless exchange
251
242
 
252
243
  exchange.add_route key, queue
253
244
  end
@@ -255,7 +246,9 @@ module BunnyMock
255
246
  # @private
256
247
  def queue_unbind(key, xchg)
257
248
 
258
- exchange = @exchanges[xchg] || exchange(xchg)
249
+ exchange = @connection.find_exchange xchg
250
+
251
+ raise NotFound.new "Exchange '#{xchg}' was not found" unless exchange
259
252
 
260
253
  exchange.remove_route key
261
254
  end
@@ -263,7 +256,9 @@ module BunnyMock
263
256
  # @private
264
257
  def xchg_bound_to?(receiver, key, name)
265
258
 
266
- source = @exchanges[name] || exchange(name)
259
+ source = @connection.find_exchange name
260
+
261
+ raise NotFound.new "Exchange '#{name}' was not found" unless source
267
262
 
268
263
  source.has_binding? receiver, routing_key: key
269
264
  end
@@ -271,30 +266,19 @@ module BunnyMock
271
266
  # @private
272
267
  def xchg_has_binding?(key, xchg)
273
268
 
274
- exchange = @exchanges[xchg] || exchange(xchg)
275
-
276
- exchange.has_binding? key
277
- end
278
-
279
- # @private
280
- def find_exchange(name)
281
- @exchanges[name]
282
- end
269
+ exchange = @connection.find_exchange xchg
283
270
 
284
- # @private
285
- def register_exchange(xchg)
286
- @exchanges[xchg.name] = xchg
287
- end
271
+ raise NotFound.new "Exchange '#{xchg}' was not found" unless exchange
288
272
 
289
- # @private
290
- def deregister_exchange(xchg)
291
- @exchanges.delete xchg.name
273
+ exchange.has_binding? key
292
274
  end
293
275
 
294
276
  # @private
295
277
  def xchg_bind(receiver, routing_key, name)
296
278
 
297
- source = @exchanges[name] || exchange(name)
279
+ source = @connection.find_exchange name
280
+
281
+ raise NotFound.new "Exchange '#{name}' was not found" unless source
298
282
 
299
283
  source.add_route routing_key, receiver
300
284
  end
@@ -302,7 +286,9 @@ module BunnyMock
302
286
  # @private
303
287
  def xchg_unbind(routing_key, name)
304
288
 
305
- source = @exchanges[name] || exchange(name)
289
+ source = @connection.find_exchange name
290
+
291
+ raise NotFound.new "Exchange '#{name}' was not found" unless source
306
292
 
307
293
  source.remove_route routing_key
308
294
  end
@@ -0,0 +1,16 @@
1
+ module BunnyMock
2
+
3
+ ##
4
+ # Base class for all exceptions
5
+ #
6
+ # @api public
7
+ #
8
+ class Exception < ::StandardError; end
9
+
10
+ ##
11
+ # Raised when a queue or exchange is not found
12
+ #
13
+ # @api public
14
+ #
15
+ class NotFound < Exception; end
16
+ end
@@ -137,6 +137,9 @@ module BunnyMock
137
137
  # @api public
138
138
  #
139
139
  def delete(opts = {})
140
+
141
+ @channel.deregister_exchange self
142
+
140
143
  @deleted = true
141
144
  end
142
145
 
@@ -197,6 +197,9 @@ module BunnyMock
197
197
  # @api public
198
198
  #
199
199
  def delete
200
+
201
+ @channel.deregister_queue self
202
+
200
203
  @deleted = true
201
204
  end
202
205
 
@@ -9,6 +9,12 @@ module BunnyMock
9
9
  # @return [Symbol] Current session status
10
10
  attr_reader :status
11
11
 
12
+ # @return [Hash<String, BunnyMock::Exchange>] Exchanges created by this channel
13
+ attr_reader :exchanges
14
+
15
+ # @return [Hash<String, BunnyMock::Queue>] Queues created by this channel
16
+ attr_reader :queues
17
+
12
18
  ##
13
19
  # Creates a new {BunnyMock::Session} instance
14
20
  #
@@ -20,6 +26,10 @@ module BunnyMock
20
26
 
21
27
  # create channel hash
22
28
  @channels = Hash.new
29
+
30
+ # create storage for queues and exchanges
31
+ @queues = Hash.new
32
+ @exchanges = Hash.new
23
33
  end
24
34
 
25
35
  ##
@@ -81,5 +91,63 @@ module BunnyMock
81
91
  @channels[n] = channel
82
92
  end
83
93
  alias channel create_channel
94
+
95
+ ##
96
+ # Test if queue exists in channel cache
97
+ #
98
+ # @param [String] name Name of queue
99
+ #
100
+ # @return [Boolean] true if queue exists, false otherwise
101
+ # @api public
102
+ #
103
+ def queue_exists?(name)
104
+ !!find_queue(name)
105
+ end
106
+
107
+ ##
108
+ # Test if exchange exists in channel cache
109
+ #
110
+ # @param [String] name Name of exchange
111
+ #
112
+ # @return [Boolean] true if exchange exists, false otherwise
113
+ # @api public
114
+ #
115
+ def exchange_exists?(name)
116
+ !!find_exchange(name)
117
+ end
118
+
119
+ #
120
+ # Implementation
121
+ #
122
+
123
+ # @private
124
+ def find_queue(name)
125
+ @queues[name]
126
+ end
127
+
128
+ # @private
129
+ def register_queue(queue)
130
+ @queues[queue.name] = queue
131
+ end
132
+
133
+ # @private
134
+ def deregister_queue(queue)
135
+ @queues.delete queue
136
+ end
137
+
138
+ # @private
139
+ def find_exchange(name)
140
+ @exchanges[name]
141
+ end
142
+
143
+ # @private
144
+ def register_exchange(xchg)
145
+ @exchanges[xchg.name] = xchg
146
+ end
147
+
148
+ # @private
149
+ def deregister_exchange(xchg)
150
+ @exchanges.delete xchg
151
+ end
84
152
  end
85
153
  end
@@ -3,5 +3,5 @@
3
3
  module BunnyMock
4
4
 
5
5
  # @return [String] Version of the library
6
- VERSION = "1.0.0"
6
+ VERSION = "1.1.0"
7
7
  end
@@ -6,6 +6,9 @@ require 'bundler'
6
6
  Bundler.setup :default, :test
7
7
  Bundler.require
8
8
 
9
+ require 'coveralls'
10
+ Coveralls.wear!
11
+
9
12
  require 'bunny-mock'
10
13
 
11
14
  RSpec.configure do |config|
@@ -94,7 +94,7 @@ describe BunnyMock::Channel do
94
94
 
95
95
  xchg = @channel.exchange 'testing.xchg', type: :fanout
96
96
 
97
- expect(@channel.exchanges['testing.xchg']).to eq(xchg)
97
+ expect(@session.exchange_exists?('testing.xchg')).to be_truthy
98
98
  end
99
99
  end
100
100
 
@@ -161,7 +161,7 @@ describe BunnyMock::Channel do
161
161
 
162
162
  q = @channel.queue 'testing.q'
163
163
 
164
- expect(@channel.queues['testing.q']).to eq(q)
164
+ expect(@session.queue_exists?('testing.q')).to be_truthy
165
165
  end
166
166
  end
167
167
 
@@ -50,6 +50,11 @@ describe BunnyMock::Exchange do
50
50
  expect(@receiver.bound_to?(@source)).to be_truthy
51
51
  expect(@source.has_binding?(@receiver)).to be_truthy
52
52
  end
53
+
54
+ it 'should raise error when exchange does not exists' do
55
+
56
+ expect { @receiver.bind('this.xchg.does.not.exist') }.to raise_exception(BunnyMock::NotFound)
57
+ end
53
58
  end
54
59
 
55
60
  context '#unbind' do
@@ -76,6 +81,11 @@ describe BunnyMock::Exchange do
76
81
  expect(@receiver.bound_to?(@source)).to be_falsey
77
82
  expect(@source.has_binding?(@receiver)).to be_falsey
78
83
  end
84
+
85
+ it 'should raise error when exchange does not exists' do
86
+
87
+ expect { @receiver.unbind('this.xchg.does.not.exist') }.to raise_exception(BunnyMock::NotFound)
88
+ end
79
89
  end
80
90
 
81
91
  context '#bound_to?' do
@@ -114,6 +124,11 @@ describe BunnyMock::Exchange do
114
124
 
115
125
  expect(@receiver.bound_to?(@source)).to be_falsey
116
126
  end
127
+
128
+ it 'should raise error when exchange does not exists' do
129
+
130
+ expect { @receiver.bound_to?('this.xchg.does.not.exist') }.to raise_exception(BunnyMock::NotFound)
131
+ end
117
132
  end
118
133
 
119
134
  context '#has_binding?' do
@@ -142,4 +157,16 @@ describe BunnyMock::Exchange do
142
157
  end
143
158
  end
144
159
 
160
+ context '#delete' do
161
+
162
+ before do
163
+ @exchange = @channel.direct 'xchg.direct'
164
+ @exchange.delete
165
+ end
166
+
167
+ it 'should remove exchange from session' do
168
+
169
+ expect(@session.exchange_exists?(@exchange.name)).to be_falsey
170
+ end
171
+ end
145
172
  end
@@ -38,6 +38,11 @@ describe BunnyMock::Queue do
38
38
  expect(@receiver.bound_to?(@source)).to be_truthy
39
39
  expect(@source.has_binding?(@receiver)).to be_truthy
40
40
  end
41
+
42
+ it 'should raise error when exchange does not exists' do
43
+
44
+ expect { @receiver.bind('this.xchg.does.not.exist') }.to raise_exception(BunnyMock::NotFound)
45
+ end
41
46
  end
42
47
 
43
48
  context '#unbind' do
@@ -64,6 +69,11 @@ describe BunnyMock::Queue do
64
69
  expect(@receiver.bound_to?(@source)).to be_falsey
65
70
  expect(@source.has_binding?(@receiver)).to be_falsey
66
71
  end
72
+
73
+ it 'should raise error when exchange does not exists' do
74
+
75
+ expect { @receiver.unbind('this.xchg.does.not.exist') }.to raise_exception(BunnyMock::NotFound)
76
+ end
67
77
  end
68
78
 
69
79
  context '#bound_to?' do
@@ -102,6 +112,11 @@ describe BunnyMock::Queue do
102
112
 
103
113
  expect(@receiver.bound_to?(@source)).to be_falsey
104
114
  end
115
+
116
+ it 'should raise error when exchange does not exists' do
117
+
118
+ expect { @receiver.bound_to?('this.xchg.does.not.exist') }.to raise_exception(BunnyMock::NotFound)
119
+ end
105
120
  end
106
121
 
107
122
  context '#message_count' do
@@ -142,4 +157,16 @@ describe BunnyMock::Queue do
142
157
  expect(@queue.message_count).to eq(0)
143
158
  end
144
159
  end
160
+
161
+ context '#delete' do
162
+
163
+ before do
164
+ @queue.delete
165
+ end
166
+
167
+ it 'should remove queue from session' do
168
+
169
+ expect(@session.queue_exists?(@queue.name)).to be_falsey
170
+ end
171
+ end
145
172
  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.0.0
4
+ version: 1.1.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: 2015-12-31 00:00:00.000000000 Z
11
+ date: 2016-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: amq-protocol
@@ -33,16 +33,19 @@ extensions: []
33
33
  extra_rdoc_files:
34
34
  - README.md
35
35
  files:
36
+ - ".coveralls.yml"
36
37
  - ".gitignore"
37
38
  - ".rspec"
38
39
  - ".travis.yml"
39
40
  - ".yardopts"
41
+ - CHANGELOG
40
42
  - Gemfile
41
43
  - LICENSE
42
44
  - README.md
43
45
  - bunny-mock.gemspec
44
46
  - lib/bunny-mock.rb
45
47
  - lib/bunny_mock/channel.rb
48
+ - lib/bunny_mock/exceptions.rb
46
49
  - lib/bunny_mock/exchange.rb
47
50
  - lib/bunny_mock/exchanges/direct.rb
48
51
  - lib/bunny_mock/exchanges/fanout.rb