Skip to content

Commit 05a9d4b

Browse files
authored
Merge pull request #15 from ninech/providing_existing_connection_through_config
Sane way of providing an existing connection
2 parents 6302967 + 3454c86 commit 05a9d4b

File tree

5 files changed

+49
-26
lines changed

5 files changed

+49
-26
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
cony (2.0.1)
4+
cony (2.1.0)
55
activesupport (>= 3)
66
bunny (~> 2.3)
77

README.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,20 @@ To configure the AMQP-Settings, use an initializer (e.g.
1818

1919
```ruby
2020
Cony.configure do |config|
21+
config.test_mode = Rails.env.test?
22+
# config.durable = false
2123
config.amqp = {
2224
host: 'localhost',
2325
exchange: 'organization.application',
2426
ssl: true,
2527
user: 'username',
2628
pass: 'secret',
2729
}
28-
config.test_mode = Rails.env.test?
29-
# config.durable = false
30+
# or:
31+
# config.amqp_connection = my_existing_bunny_session
3032
end
3133
```
3234

33-
### Using an existing Bunny connection
34-
35-
You can share your already established `Bunny::Session` with Cony.
36-
37-
```ruby
38-
Cony::AMQPConnection.instance.connection = your_connection
39-
```
40-
41-
Cony will only accept the given connection if it's current connection is closed or if there is no current
42-
connection. There will be an error if Cony already has a connection!
43-
This restriction is imposed because else Cony might leak connections.
44-
4535
## Getting Started
4636

4737
To enable the notifications for a model, you just need to include the

lib/cony.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313
# To configure Cony:
1414
# <code>
1515
# Cony.configure do |config|
16+
# config.test_mode = Rails.env.test?
17+
# # config.durable = false
1618
# config.amqp = {
1719
# host: 'localhost',
1820
# exchange: 'organization.application',
1921
# ssl: true,
2022
# user: 'username',
2123
# pass: 'secret',
2224
# }
23-
# config.test_mode = Rails.env.test?
24-
# # config.durable = false
25+
# # or:
26+
# # config.amqp_connection = my_existing_bunny_session
2527
# end
2628
# </code>
2729
module Cony

lib/cony/amqp_connection.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def publish(message, routing_key)
3232

3333
##
3434
# Sets a custom connection if no valid_connection? is already provided
35+
# :deprecated: Use the Cony Initializer
3536
def connection=(connection)
3637
fail Cony::ValidConnectionAlreadyDefined, 'A connection has already been set.' if valid_connection_present?
3738
@connection = connection
@@ -42,7 +43,9 @@ def connection=(connection)
4243
def connection
4344
return @connection if valid_connection_present?
4445

45-
@connection = Bunny.new Cony.config.amqp
46+
return @connection = Cony.config.amqp_connection unless Cony.config.amqp_connection.nil?
47+
48+
@connection = Bunny.new(Cony.config.amqp)
4649
ObjectSpace.define_finalizer(self, proc { cleanup })
4750
@connection.start
4851
end

spec/cony/amqp_connection_spec.rb

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
describe Cony::AMQPConnection do
88
let(:amqp_config) { { exchange: 'bunny-tests' } }
9-
let(:config) { double('Cony Config', amqp: amqp_config, durable: false) }
9+
let(:config) { double('Cony Config', amqp: amqp_config, durable: false, amqp_connection: nil) }
1010
let(:handler) { Cony::AMQPConnection }
1111
let(:message) { 'Bunnies are connies' }
1212
let(:routing_key) { 'bunny.info' }
@@ -93,16 +93,44 @@
9393
describe 'setting a connection' do
9494
let(:existing_connection) { connection_double.clone }
9595

96-
it 'sets the connection to the given one' do
97-
subject.instance.connection = existing_connection
96+
describe 'on the AmqpConnection (deprecated)' do
97+
it 'sets the connection to the given one' do
98+
subject.instance.connection = existing_connection
9899

99-
expect(subject.instance.connection).to be(existing_connection)
100+
expect(subject.instance.connection).to be(existing_connection)
101+
end
102+
103+
it 'raises exception when redefining the connection' do
104+
expect(subject.instance.connection).to be(connection_double)
105+
expect { subject.instance.connection = existing_connection }.
106+
to raise_error(Cony::ValidConnectionAlreadyDefined)
107+
end
100108
end
101109

102-
it 'raises exception when redefining the connection' do
103-
expect(subject.instance.connection).to be(connection_double)
104-
expect { subject.instance.connection = existing_connection }.
105-
to raise_error(Cony::ValidConnectionAlreadyDefined)
110+
describe 'through the config' do
111+
let(:config) do
112+
double('Cony Config', amqp: amqp_config, durable: false, amqp_connection: existing_connection)
113+
end
114+
115+
it 'uses the connection from the config' do
116+
expect(subject.instance.connection).to be(existing_connection)
117+
end
118+
119+
it 'raises exception when redefining the connection' do
120+
expect(subject.instance.connection).to be(existing_connection)
121+
expect { subject.instance.connection = existing_connection }.
122+
to raise_error(Cony::ValidConnectionAlreadyDefined)
123+
end
124+
125+
describe 'no amqp config hash given' do
126+
let(:config) do
127+
double('Cony Config', amqp: nil, durable: false, amqp_connection: existing_connection)
128+
end
129+
130+
it 'still uses the connection from the config' do
131+
expect(subject.instance.connection).to be(existing_connection)
132+
end
133+
end
106134
end
107135
end
108136
end

0 commit comments

Comments
 (0)