bunny-mock 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.editorconfig +12 -0
- data/.rubocop.yml +9 -0
- data/.rubocop_todo.yml +65 -0
- data/.travis.yml +6 -5
- data/.yardopts +1 -0
- data/CHANGELOG.md +14 -0
- data/Gemfile +1 -11
- data/README.md +61 -88
- data/Rakefile +29 -0
- data/UPGRADING.md +12 -0
- data/bunny-mock.gemspec +19 -16
- data/lib/bunny-mock.rb +30 -29
- data/lib/bunny_mock/channel.rb +275 -274
- data/lib/bunny_mock/exchange.rb +258 -267
- data/lib/bunny_mock/exchanges/direct.rb +20 -19
- data/lib/bunny_mock/exchanges/fanout.rb +20 -23
- data/lib/bunny_mock/exchanges/headers.rb +27 -26
- data/lib/bunny_mock/exchanges/topic.rb +45 -49
- data/lib/bunny_mock/queue.rb +210 -209
- data/lib/bunny_mock/session.rb +171 -151
- data/lib/bunny_mock/version.rb +3 -2
- data/spec/spec_helper.rb +1 -1
- data/spec/unit/bunny_mock/exchange_spec.rb +10 -10
- data/spec/unit/bunny_mock/queue_spec.rb +7 -7
- data/spec/unit/bunny_mock/session_spec.rb +74 -48
- metadata +86 -14
- data/CHANGELOG +0 -8
- data/lib/bunny_mock/exceptions.rb +0 -16
data/lib/bunny-mock.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'bunny_mock/version'
|
2
|
-
require 'amq/protocol/client'
|
3
3
|
|
4
|
-
require '
|
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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
data/lib/bunny_mock/channel.rb
CHANGED
@@ -1,296 +1,297 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
module BunnyMock
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
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
|
-
|
247
|
-
|
247
|
+
# @private
|
248
|
+
def queue_unbind(key, xchg)
|
248
249
|
|
249
|
-
|
250
|
+
exchange = @connection.find_exchange xchg
|
250
251
|
|
251
|
-
|
252
|
+
raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange
|
252
253
|
|
253
|
-
|
254
|
-
|
254
|
+
exchange.remove_route key
|
255
|
+
end
|
255
256
|
|
256
|
-
|
257
|
-
|
257
|
+
# @private
|
258
|
+
def xchg_bound_to?(receiver, key, name)
|
258
259
|
|
259
|
-
|
260
|
+
source = @connection.find_exchange name
|
260
261
|
|
261
|
-
|
262
|
+
raise Bunny::NotFound.new("Exchange '#{name}' was not found", self, false) unless source
|
262
263
|
|
263
|
-
|
264
|
-
|
264
|
+
source.routes_to? receiver, routing_key: key
|
265
|
+
end
|
265
266
|
|
266
|
-
|
267
|
-
|
267
|
+
# @private
|
268
|
+
def xchg_routes_to?(key, xchg)
|
268
269
|
|
269
|
-
|
270
|
+
exchange = @connection.find_exchange xchg
|
270
271
|
|
271
|
-
|
272
|
+
raise Bunny::NotFound.new("Exchange '#{xchg}' was not found", self, false) unless exchange
|
272
273
|
|
273
|
-
|
274
|
-
|
274
|
+
exchange.routes_to? key
|
275
|
+
end
|
275
276
|
|
276
|
-
|
277
|
-
|
277
|
+
# @private
|
278
|
+
def xchg_bind(receiver, routing_key, name)
|
278
279
|
|
279
|
-
|
280
|
+
source = @connection.find_exchange name
|
280
281
|
|
281
|
-
|
282
|
+
raise Bunny::NotFound.new("Exchange '#{name}' was not found", self, false) unless source
|
282
283
|
|
283
|
-
|
284
|
-
|
284
|
+
source.add_route routing_key, receiver
|
285
|
+
end
|
285
286
|
|
286
|
-
|
287
|
-
|
287
|
+
# @private
|
288
|
+
def xchg_unbind(routing_key, name)
|
288
289
|
|
289
|
-
|
290
|
+
source = @connection.find_exchange name
|
290
291
|
|
291
|
-
|
292
|
+
raise Bunny::NotFound.new("Exchange '#{name}' was not found", self, false) unless source
|
292
293
|
|
293
|
-
|
294
|
-
|
295
|
-
|
294
|
+
source.remove_route routing_key
|
295
|
+
end
|
296
|
+
end
|
296
297
|
end
|