Permalink
Browse files

allow mongo data store to connect to replica sets

  • Loading branch information...
1 parent 916bac7 commit 6ce82b6a77a3089f697063bbd208ae242a5feb29 @fcheung committed Mar 15, 2012
@@ -8,6 +8,8 @@ class MongoDataStore
include Serializer
configurable_attr :host
+ configurable_attr :hosts
+ configurable_attr :mongo_connection_options
configurable_attr :port
configurable_attr :database, 'dragonfly'
configurable_attr :username
@@ -21,6 +23,8 @@ class MongoDataStore
def initialize(opts={})
self.host = opts[:host]
+ self.hosts = opts[:hosts]
+ self.mongo_connection_options = opts[:mongo_connection_options] || {}
self.port = opts[:port]
self.database = opts[:database] if opts[:database]
self.username = opts[:username]
@@ -60,7 +64,11 @@ def destroy(uid)
end
def connection
- @connection ||= Mongo::Connection.new(host, port)
+ if hosts
+ @connection ||= Mongo::ReplSetConnection.new(hosts, mongo_connection_options)
+ else
+ @connection ||= Mongo::Connection.new(host, port, mongo_connection_options)
+ end
end
def db
@@ -16,6 +16,15 @@
it_should_behave_like 'data_store'
+ describe "contecting to a replica set" do
+ it "should initiate a replica set connection if hosts is set" do
+ @data_store.hosts = ['1.2.3.4:27017', '1.2.3.4:27017']
+ @data_store.mongo_connection_options = {:name => 'testingset'}
+ Mongo::ReplSetConnection.should_receive(:new).with(['1.2.3.4:27017', '1.2.3.4:27017'], :name => 'testingset')
+ @data_store.connection
+ end
+ end
+
describe "authenticating" do
before(:each) do
@temp_object = Dragonfly::TempObject.new('Feijão verde')

0 comments on commit 6ce82b6

Please sign in to comment.