Skip to content

Commit 387686f

Browse files
committed
Initialize MongoShardedClient with MONGODB_URI.
This fixes the errors that were occurring when instantiating a MongoShardedClient when a MONGODB_URI environment variable is present. This also fixes MongoShardedClient.from_uri to properly get a MongoShardedClient connection from the URIParser. [ RUBY-541 ]
1 parent 56c2724 commit 387686f

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed

lib/mongo/mongo_sharded_client.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ def initialize(*args)
1616

1717
if nodes.empty? and ENV.has_key?('MONGODB_URI')
1818
parser = URIParser.new ENV['MONGODB_URI']
19-
if parser.direct?
20-
raise MongoArgumentError, "Mongo::MongoShardedClient.new called with no arguments, but ENV['MONGODB_URI'] implies a direct connection."
21-
end
2219
opts = parser.connection_options.merge! opts
23-
nodes = [parser.nodes]
20+
nodes = parser.node_strings
2421
end
2522

2623
unless nodes.length > 0
@@ -61,7 +58,7 @@ def initialize(*args)
6158
end
6259

6360
def valid_opts
64-
GENERIC_OPTS + SHARDED_CLUSTER_OPTS
61+
GENERIC_OPTS + SHARDED_CLUSTER_OPTS + READ_PREFERENCE_OPTS + WRITE_CONCERN_OPTS
6562
end
6663

6764
def inspect
@@ -135,5 +132,17 @@ def checkout(&block)
135132
tries < 2 ? retry : raise
136133
end
137134
end
135+
136+
# Initialize a connection to MongoDB using the MongoDB URI spec.
137+
#
138+
# @param uri [ String ] string of the format:
139+
# mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]
140+
#
141+
# @param opts [ Hash ] Any of the options available for MongoShardedClient.new
142+
#
143+
# @return [ Mongo::MongoShardedClient ] The sharded client.
144+
def self.from_uri(uri = ENV['MONGODB_URI'], options = {})
145+
URIParser.new(uri).connection(options, false, true)
146+
end
138147
end
139148
end

lib/mongo/util/uri_parser.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,18 @@ def initialize(uri)
140140
# @note Don't confuse this with attribute getter method #connect.
141141
#
142142
# @return [MongoClient,MongoReplicaSetClient]
143-
def connection(extra_opts, legacy=false)
144-
opts = connection_options.merge! extra_opts
143+
def connection(extra_opts, legacy = false, sharded = false)
144+
opts = connection_options.merge!(extra_opts)
145145
if(legacy)
146146
if replicaset?
147147
ReplSetConnection.new(node_strings, opts)
148148
else
149149
Connection.new(host, port, opts)
150150
end
151151
else
152-
if replicaset?
152+
if sharded
153+
MongoShardedClient.new(node_strings, opts)
154+
elsif replicaset?
153155
MongoReplicaSetClient.new(node_strings, opts)
154156
else
155157
MongoClient.new(host, port, opts)

test/functional/uri_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,27 @@ def test_read_preference_when_no_replica_set
174174
parser = Mongo::URIParser.new("mongodb://localhost:27018?readPreference=nearest")
175175
assert_nil parser.connection_options[:read]
176176
end
177+
178+
def test_connection_when_sharded_with_no_options
179+
parser = Mongo::URIParser.new("mongodb://localhost:27017,localhost:27018")
180+
client = parser.connection({}, false, true)
181+
assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
182+
assert_true client.mongos?
183+
end
184+
185+
def test_connection_when_sharded_with_options
186+
parser = Mongo::URIParser.new("mongodb://localhost:27017,localhost:27018")
187+
client = parser.connection({ :refresh_interval => 10 }, false, true)
188+
assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
189+
assert_equal 10, client.refresh_interval
190+
assert_true client.mongos?
191+
end
192+
193+
def test_connection_when_sharded_with_uri_options
194+
parser = Mongo::URIParser.new("mongodb://localhost:27017,localhost:27018?readPreference=nearest")
195+
client = parser.connection({}, false, true)
196+
assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
197+
assert_equal :nearest, client.read
198+
assert_true client.mongos?
199+
end
177200
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require "test_helper"
2+
3+
class MongoShardedClientTest < Test::Unit::TestCase
4+
include Mongo
5+
6+
def setup
7+
ENV["MONGODB_URI"] = nil
8+
end
9+
10+
def test_initialize_with_single_mongos_uri
11+
ENV["MONGODB_URI"] = "mongodb://localhost:27017"
12+
client = MongoShardedClient.new
13+
assert_equal [[ "localhost", 27017 ]], client.seeds
14+
end
15+
16+
def test_initialize_with_multiple_mongos_uris
17+
ENV["MONGODB_URI"] = "mongodb://localhost:27017,localhost:27018"
18+
client = MongoShardedClient.new
19+
assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
20+
end
21+
22+
def test_from_uri_with_string
23+
client = MongoShardedClient.from_uri("mongodb://localhost:27017,localhost:27018")
24+
assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
25+
end
26+
27+
def test_from_uri_with_env_variable
28+
ENV["MONGODB_URI"] = "mongodb://localhost:27017,localhost:27018"
29+
client = MongoShardedClient.from_uri
30+
assert_equal [[ "localhost", 27017 ], [ "localhost", 27018 ]], client.seeds
31+
end
32+
end

0 commit comments

Comments
 (0)