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.
@@ -1,153 +1,173 @@
1
+ # frozen_string_literal: true
1
2
  module BunnyMock
2
- # Mocks Bunny::Session
3
- class Session
4
-
5
- #
6
- # API
7
- #
8
-
9
- # @return [Symbol] Current session status
10
- attr_reader :status
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
-
18
- ##
19
- # Creates a new {BunnyMock::Session} instance
20
- #
21
- # @api public
22
- def initialize(*args)
23
-
24
- # not connected until {BunnyMock::Session#start} is called
25
- @status = :not_connected
26
-
27
- # create channel hash
28
- @channels = Hash.new
29
-
30
- # create storage for queues and exchanges
31
- @queues = Hash.new
32
- @exchanges = Hash.new
33
- end
34
-
35
- ##
36
- # Sets status to connected
37
- #
38
- # @return [BunnyMock::Session] self
39
- # @api public
40
- def start
41
-
42
- @status = :connected
43
-
44
- self
45
- end
46
-
47
- ##
48
- # Sets status to closed
49
- #
50
- # @return [BunnyMock::Session] self
51
- # @api public
52
- def stop
53
-
54
- @status = :closed
55
-
56
- self
57
- end
58
- alias close stop
59
-
60
- ##
61
- # Tests if connection is available
62
- #
63
- # @return [Boolean] true if status is connected, false otherwise
64
- # @api public
65
- def open?
66
-
67
- @status == :connected
68
- end
69
-
70
- ##
71
- # Creates a new {BunnyMock::Channel} instance
72
- #
73
- # @param [Integer] n Channel identifier
74
- # @param [Integer] pool_size Work pool size (insignificant)
75
- #
76
- # @return [BunnyMock::Channel] Channel instance
77
- # @api public
78
- def create_channel(n = nil, pool_size = 1)
79
-
80
- # raise same error as {Bunny::Session#create_channel}
81
- raise ArgumentError, "channel number 0 is reserved in the protocol and cannot be used" if n == 0
82
-
83
- # return cached channel if exists
84
- return @channels[n] if n and @channels.key?(n)
85
-
86
- # create and open channel
87
- channel = Channel.new self, n
88
- channel.open
89
-
90
- # return channel
91
- @channels[n] = channel
92
- end
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
152
- end
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
@@ -1,7 +1,8 @@
1
1
  # encoding: UTF-8
2
+ # frozen_string_literal: true
2
3
 
3
4
  module BunnyMock
4
5
 
5
- # @return [String] Version of the library
6
- VERSION = "1.1.0"
6
+ # @return [String] Version of the library
7
+ VERSION = '1.2.0'
7
8
  end
@@ -14,7 +14,7 @@ require 'bunny-mock'
14
14
  RSpec.configure do |config|
15
15
 
16
16
  config.before :each do
17
- @session = BunnyMock::Session.new
17
+ @session = BunnyMock.new.start
18
18
  @channel = @session.channel
19
19
  end
20
20
  end
@@ -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.has_binding?(@receiver)).to be_truthy
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.has_binding?(@receiver)).to be_truthy
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(BunnyMock::NotFound)
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.has_binding?(@receiver)).to be_falsey
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.has_binding?(@receiver)).to be_falsey
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(BunnyMock::NotFound)
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(BunnyMock::NotFound)
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 '#has_binding?' do
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.has_binding?(@receiver)).to be_falsey
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.has_binding?(@receiver)).to be_falsey
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.has_binding?(@receiver)).to be_truthy
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.has_binding?(@receiver)).to be_truthy
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(BunnyMock::NotFound)
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.has_binding?(@receiver)).to be_falsey
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.has_binding?(@receiver)).to be_falsey
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(BunnyMock::NotFound)
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(BunnyMock::NotFound)
118
+ expect { @receiver.bound_to?('this.xchg.does.not.exist') }.to raise_exception(Bunny::NotFound)
119
119
  end
120
120
  end
121
121