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/session.rb
CHANGED
@@ -1,153 +1,173 @@
|
|
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
|
-
|
3
|
+
# Mocks Bunny::Session
|
4
|
+
class Session
|
5
|
+
|
6
|
+
#
|
7
|
+
# API
|
8
|
+
#
|
9
|
+
|
10
|
+
# @return [Symbol] Current session status
|
11
|
+
attr_reader :status
|
12
|
+
|
13
|
+
# @return [Hash<String, BunnyMock::Exchange>] Exchanges created by this channel
|
14
|
+
attr_reader :exchanges
|
15
|
+
|
16
|
+
# @return [Hash<String, BunnyMock::Queue>] Queues created by this channel
|
17
|
+
attr_reader :queues
|
18
|
+
|
19
|
+
##
|
20
|
+
# Creates a new {BunnyMock::Session} instance
|
21
|
+
#
|
22
|
+
# @api public
|
23
|
+
def initialize(*)
|
24
|
+
|
25
|
+
# not connected until {BunnyMock::Session#start} is called
|
26
|
+
@status = :not_connected
|
27
|
+
|
28
|
+
# create channel hash
|
29
|
+
@channels = {}
|
30
|
+
|
31
|
+
# create storage for queues and exchanges
|
32
|
+
@queues = {}
|
33
|
+
@exchanges = {}
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# Sets status to connected
|
38
|
+
#
|
39
|
+
# @return [BunnyMock::Session] self
|
40
|
+
# @api public
|
41
|
+
def start
|
42
|
+
|
43
|
+
@status = :connected
|
44
|
+
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
# Sets status to closed
|
50
|
+
#
|
51
|
+
# @return [BunnyMock::Session] self
|
52
|
+
# @api public
|
53
|
+
def stop
|
54
|
+
|
55
|
+
@status = :closed
|
56
|
+
|
57
|
+
self
|
58
|
+
end
|
59
|
+
alias close stop
|
60
|
+
|
61
|
+
##
|
62
|
+
# Tests if connection is available
|
63
|
+
#
|
64
|
+
# @return [Boolean] true if status is connected, false otherwise
|
65
|
+
# @api public
|
66
|
+
def open?
|
67
|
+
|
68
|
+
@status == :connected
|
69
|
+
end
|
70
|
+
|
71
|
+
##
|
72
|
+
# Creates a new {BunnyMock::Channel} instance
|
73
|
+
#
|
74
|
+
# @param [Integer] n Channel identifier
|
75
|
+
# @param [Integer] pool_size Work pool size (insignificant)
|
76
|
+
#
|
77
|
+
# @return [BunnyMock::Channel] Channel instance
|
78
|
+
# @api public
|
79
|
+
def create_channel(n = nil, _pool_size = 1)
|
80
|
+
|
81
|
+
# raise same error as {Bunny::Session#create_channel}
|
82
|
+
raise ArgumentError, 'channel number 0 is reserved in the protocol and cannot be used' if n == 0
|
83
|
+
|
84
|
+
# return cached channel if exists
|
85
|
+
return @channels[n] if n && @channels.key?(n)
|
86
|
+
|
87
|
+
# create and open channel
|
88
|
+
channel = Channel.new self, n
|
89
|
+
channel.open
|
90
|
+
|
91
|
+
# return channel
|
92
|
+
@channels[n] = channel
|
93
|
+
end
|
94
|
+
alias channel create_channel
|
95
|
+
|
96
|
+
##
|
97
|
+
# Creates a temporary {BunnyMock::Channel} instance, yields it to
|
98
|
+
# the block given, then closes it
|
99
|
+
#
|
100
|
+
# @param [Integer] n Channel identifier
|
101
|
+
#
|
102
|
+
# @return [BunnyMock::Session] self
|
103
|
+
# @api public
|
104
|
+
def with_channel(n = nil)
|
105
|
+
ch = create_channel(n)
|
106
|
+
begin
|
107
|
+
yield ch
|
108
|
+
ensure
|
109
|
+
ch.close if ch.open?
|
110
|
+
end
|
111
|
+
|
112
|
+
self
|
113
|
+
end
|
114
|
+
|
115
|
+
##
|
116
|
+
# Test if queue exists in channel cache
|
117
|
+
#
|
118
|
+
# @param [String] name Name of queue
|
119
|
+
#
|
120
|
+
# @return [Boolean] true if queue exists, false otherwise
|
121
|
+
# @api public
|
122
|
+
#
|
123
|
+
def queue_exists?(name)
|
124
|
+
!find_queue(name).nil?
|
125
|
+
end
|
126
|
+
|
127
|
+
##
|
128
|
+
# Test if exchange exists in channel cache
|
129
|
+
#
|
130
|
+
# @param [String] name Name of exchange
|
131
|
+
#
|
132
|
+
# @return [Boolean] true if exchange exists, false otherwise
|
133
|
+
# @api public
|
134
|
+
#
|
135
|
+
def exchange_exists?(name)
|
136
|
+
!find_exchange(name).nil?
|
137
|
+
end
|
138
|
+
|
139
|
+
#
|
140
|
+
# Implementation
|
141
|
+
#
|
142
|
+
|
143
|
+
# @private
|
144
|
+
def find_queue(name)
|
145
|
+
@queues[name]
|
146
|
+
end
|
147
|
+
|
148
|
+
# @private
|
149
|
+
def register_queue(queue)
|
150
|
+
@queues[queue.name] = queue
|
151
|
+
end
|
152
|
+
|
153
|
+
# @private
|
154
|
+
def deregister_queue(queue)
|
155
|
+
@queues.delete queue
|
156
|
+
end
|
157
|
+
|
158
|
+
# @private
|
159
|
+
def find_exchange(name)
|
160
|
+
@exchanges[name]
|
161
|
+
end
|
162
|
+
|
163
|
+
# @private
|
164
|
+
def register_exchange(xchg)
|
165
|
+
@exchanges[xchg.name] = xchg
|
166
|
+
end
|
167
|
+
|
168
|
+
# @private
|
169
|
+
def deregister_exchange(xchg)
|
170
|
+
@exchanges.delete xchg
|
171
|
+
end
|
172
|
+
end
|
153
173
|
end
|
data/lib/bunny_mock/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -40,7 +40,7 @@ describe BunnyMock::Exchange do
|
|
40
40
|
@receiver.bind @source
|
41
41
|
|
42
42
|
expect(@receiver.bound_to?(@source)).to be_truthy
|
43
|
-
expect(@source.
|
43
|
+
expect(@source.routes_to?(@receiver)).to be_truthy
|
44
44
|
end
|
45
45
|
|
46
46
|
it 'should bind by exchange name' do
|
@@ -48,12 +48,12 @@ describe BunnyMock::Exchange do
|
|
48
48
|
@receiver.bind @source.name
|
49
49
|
|
50
50
|
expect(@receiver.bound_to?(@source)).to be_truthy
|
51
|
-
expect(@source.
|
51
|
+
expect(@source.routes_to?(@receiver)).to be_truthy
|
52
52
|
end
|
53
53
|
|
54
54
|
it 'should raise error when exchange does not exists' do
|
55
55
|
|
56
|
-
expect { @receiver.bind('this.xchg.does.not.exist') }.to raise_exception(
|
56
|
+
expect { @receiver.bind('this.xchg.does.not.exist') }.to raise_exception(Bunny::NotFound)
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
@@ -71,7 +71,7 @@ describe BunnyMock::Exchange do
|
|
71
71
|
@receiver.unbind @source
|
72
72
|
|
73
73
|
expect(@receiver.bound_to?(@source)).to be_falsey
|
74
|
-
expect(@source.
|
74
|
+
expect(@source.routes_to?(@receiver)).to be_falsey
|
75
75
|
end
|
76
76
|
|
77
77
|
it 'should unbind by exchange name' do
|
@@ -79,12 +79,12 @@ describe BunnyMock::Exchange do
|
|
79
79
|
@receiver.unbind @source.name
|
80
80
|
|
81
81
|
expect(@receiver.bound_to?(@source)).to be_falsey
|
82
|
-
expect(@source.
|
82
|
+
expect(@source.routes_to?(@receiver)).to be_falsey
|
83
83
|
end
|
84
84
|
|
85
85
|
it 'should raise error when exchange does not exists' do
|
86
86
|
|
87
|
-
expect { @receiver.unbind('this.xchg.does.not.exist') }.to raise_exception(
|
87
|
+
expect { @receiver.unbind('this.xchg.does.not.exist') }.to raise_exception(Bunny::NotFound)
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
@@ -127,11 +127,11 @@ describe BunnyMock::Exchange do
|
|
127
127
|
|
128
128
|
it 'should raise error when exchange does not exists' do
|
129
129
|
|
130
|
-
expect { @receiver.bound_to?('this.xchg.does.not.exist') }.to raise_exception(
|
130
|
+
expect { @receiver.bound_to?('this.xchg.does.not.exist') }.to raise_exception(Bunny::NotFound)
|
131
131
|
end
|
132
132
|
end
|
133
133
|
|
134
|
-
context '#
|
134
|
+
context '#routes_to?' do
|
135
135
|
|
136
136
|
before do
|
137
137
|
@source = @channel.exchange 'xchg.source'
|
@@ -145,7 +145,7 @@ describe BunnyMock::Exchange do
|
|
145
145
|
@receiver.unbind @source
|
146
146
|
|
147
147
|
expect(@receiver.bound_to?(@source)).to be_falsey
|
148
|
-
expect(@source.
|
148
|
+
expect(@source.routes_to?(@receiver)).to be_falsey
|
149
149
|
end
|
150
150
|
|
151
151
|
it 'should unbind by exchange name' do
|
@@ -153,7 +153,7 @@ describe BunnyMock::Exchange do
|
|
153
153
|
@receiver.unbind @source.name
|
154
154
|
|
155
155
|
expect(@receiver.bound_to?(@source)).to be_falsey
|
156
|
-
expect(@source.
|
156
|
+
expect(@source.routes_to?(@receiver)).to be_falsey
|
157
157
|
end
|
158
158
|
end
|
159
159
|
|
@@ -28,7 +28,7 @@ describe BunnyMock::Queue do
|
|
28
28
|
@receiver.bind @source
|
29
29
|
|
30
30
|
expect(@receiver.bound_to?(@source)).to be_truthy
|
31
|
-
expect(@source.
|
31
|
+
expect(@source.routes_to?(@receiver)).to be_truthy
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'should bind by exchange name' do
|
@@ -36,12 +36,12 @@ describe BunnyMock::Queue do
|
|
36
36
|
@receiver.bind @source.name
|
37
37
|
|
38
38
|
expect(@receiver.bound_to?(@source)).to be_truthy
|
39
|
-
expect(@source.
|
39
|
+
expect(@source.routes_to?(@receiver)).to be_truthy
|
40
40
|
end
|
41
41
|
|
42
42
|
it 'should raise error when exchange does not exists' do
|
43
43
|
|
44
|
-
expect { @receiver.bind('this.xchg.does.not.exist') }.to raise_exception(
|
44
|
+
expect { @receiver.bind('this.xchg.does.not.exist') }.to raise_exception(Bunny::NotFound)
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
@@ -59,7 +59,7 @@ describe BunnyMock::Queue do
|
|
59
59
|
@receiver.unbind @source
|
60
60
|
|
61
61
|
expect(@receiver.bound_to?(@source)).to be_falsey
|
62
|
-
expect(@source.
|
62
|
+
expect(@source.routes_to?(@receiver)).to be_falsey
|
63
63
|
end
|
64
64
|
|
65
65
|
it 'should unbind by exchange name' do
|
@@ -67,12 +67,12 @@ describe BunnyMock::Queue do
|
|
67
67
|
@receiver.unbind @source.name
|
68
68
|
|
69
69
|
expect(@receiver.bound_to?(@source)).to be_falsey
|
70
|
-
expect(@source.
|
70
|
+
expect(@source.routes_to?(@receiver)).to be_falsey
|
71
71
|
end
|
72
72
|
|
73
73
|
it 'should raise error when exchange does not exists' do
|
74
74
|
|
75
|
-
expect { @receiver.unbind('this.xchg.does.not.exist') }.to raise_exception(
|
75
|
+
expect { @receiver.unbind('this.xchg.does.not.exist') }.to raise_exception(Bunny::NotFound)
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
@@ -115,7 +115,7 @@ describe BunnyMock::Queue do
|
|
115
115
|
|
116
116
|
it 'should raise error when exchange does not exists' do
|
117
117
|
|
118
|
-
expect { @receiver.bound_to?('this.xchg.does.not.exist') }.to raise_exception(
|
118
|
+
expect { @receiver.bound_to?('this.xchg.does.not.exist') }.to raise_exception(Bunny::NotFound)
|
119
119
|
end
|
120
120
|
end
|
121
121
|
|