depricated wtimeout in favor of wtimeoutMS

This commit is contained in:
Tyler Brock 2012-01-17 19:35:46 -05:00
parent 1124918502
commit 9a39987737
3 changed files with 28 additions and 8 deletions

View File

@ -23,7 +23,7 @@ module Mongo
MONGODB_URI_MATCHER = /(([-.\w:]+):([^@,]+)@)?((?:(?:[-.\w]+)(?::(?:[\w]+))?,?)+)(\/([-\w]+))?/ MONGODB_URI_MATCHER = /(([-.\w:]+):([^@,]+)@)?((?:(?:[-.\w]+)(?::(?:[\w]+))?,?)+)(\/([-\w]+))?/
MONGODB_URI_SPEC = "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]" MONGODB_URI_SPEC = "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]"
SPEC_ATTRS = [:nodes, :auths] SPEC_ATTRS = [:nodes, :auths]
OPT_ATTRS = [:connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync, :journal, :connectTimeoutMS, :socketTimeoutMS] OPT_ATTRS = [:connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync, :journal, :connectTimeoutMS, :socketTimeoutMS, :wtimeoutMS]
OPT_VALID = {:connect => lambda {|arg| ['direct', 'replicaset'].include?(arg)}, OPT_VALID = {:connect => lambda {|arg| ['direct', 'replicaset'].include?(arg)},
:replicaset => lambda {|arg| arg.length > 0}, :replicaset => lambda {|arg| arg.length > 0},
@ -35,6 +35,7 @@ module Mongo
:journal => lambda {|arg| ['true', 'false'].include?(arg)}, :journal => lambda {|arg| ['true', 'false'].include?(arg)},
:connectTimeoutMS => lambda {|arg| arg =~ /^\d+$/ }, :connectTimeoutMS => lambda {|arg| arg =~ /^\d+$/ },
:socketTimeoutMS => lambda {|arg| arg =~ /^\d+$/ }, :socketTimeoutMS => lambda {|arg| arg =~ /^\d+$/ },
:wtimeoutMS => lambda {|arg| arg =~ /^\d+$/ }
} }
OPT_ERR = {:connect => "must be 'direct' or 'replicaset'", OPT_ERR = {:connect => "must be 'direct' or 'replicaset'",
@ -47,6 +48,7 @@ module Mongo
:journal => "must be 'true' or 'false'", :journal => "must be 'true' or 'false'",
:connectTimeoutMS => "must be an integer specifying milliseconds a connection can take to be opened before timing out", :connectTimeoutMS => "must be an integer specifying milliseconds a connection can take to be opened before timing out",
:socketTimeoutMS => "must be an integer specifying milliseconds a send or receive on a socket can take before timing out", :socketTimeoutMS => "must be an integer specifying milliseconds a send or receive on a socket can take before timing out",
:wtimeoutMS => "must be an integer specifying milliseconds a send or receive on a socket can take before timing out"
} }
OPT_CONV = {:connect => lambda {|arg| arg}, OPT_CONV = {:connect => lambda {|arg| arg},
@ -59,9 +61,10 @@ module Mongo
:journal => lambda {|arg| arg == 'true' ? true : false}, :journal => lambda {|arg| arg == 'true' ? true : false},
:connectTimeoutMS => lambda {|arg| arg.to_i }, :connectTimeoutMS => lambda {|arg| arg.to_i },
:socketTimeoutMS => lambda {|arg| arg.to_i }, :socketTimeoutMS => lambda {|arg| arg.to_i },
:wtimeoutMS => lambda {|arg| arg.to_i }
} }
attr_reader :nodes, :auths, :connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync, :journal, :connectTimeoutMS, :socketTimeoutMS attr_reader :nodes, :auths, :connect, :replicaset, :slaveok, :safe, :w, :wtimeout, :fsync, :journal, :connectTimeoutMS, :socketTimeoutMS, :wtimeoutMS
# Parse a MongoDB URI. This method is used by Connection.from_uri. # Parse a MongoDB URI. This method is used by Connection.from_uri.
# Returns an array of nodes and an array of db authorizations, if applicable. # Returns an array of nodes and an array of db authorizations, if applicable.
@ -85,16 +88,25 @@ module Mongo
def connection_options def connection_options
opts = {} opts = {}
if (@w || @journal || @wtimeout || @fsync) && !@safe if (@w || @journal || @wtimeout || @fsync || @wtimeoutMS) && !@safe
raise MongoArgumentError, "Safe must be true if w, journal, wtimeout, or fsync is specified" raise MongoArgumentError, "Safe must be true if w, journal, wtimeoutMS, or fsync is specified"
end end
if @safe if @safe
if @w || @journal || @wtimeout || @fsync if @w || @journal || @wtimeout || @fsync || @wtimeoutMS
safe_opts = {} safe_opts = {}
safe_opts[:w] = @w if @w safe_opts[:w] = @w if @w
safe_opts[:j] = @journal if @journal safe_opts[:j] = @journal if @journal
safe_opts[:wtimeout] = @wtimeout if @wtimeout
if @wtimeout
warn "Using wtimeout in a URI is deprecated, please use wtimeoutMS. It will be removed in v2.0."
safe_opts[:wtimeout] = @wtimeout
end
if @wtimeoutMS
safe_opts[:wtimeout] = @wtimeoutMS / 1000
end
safe_opts[:fsync] = @fsync if @fsync safe_opts[:fsync] = @fsync if @fsync
else else
safe_opts = true safe_opts = true

View File

@ -48,11 +48,18 @@ class ConnectionTest < Test::Unit::TestCase
should "set safe options on connection" do should "set safe options on connection" do
host_name = "localhost" host_name = "localhost"
opts = "safe=true&w=2&wtimeout=10&fsync=true&journal=true" opts = "safe=true&w=2&wtimeoutMS=10000&fsync=true&journal=true"
@conn = Connection.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false) @conn = Connection.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
assert_equal({:w => 2, :wtimeout => 10, :fsync => true, :j => true}, @conn.safe) assert_equal({:w => 2, :wtimeout => 10, :fsync => true, :j => true}, @conn.safe)
end end
should "have wtimeoutMS take precidence over the depricated wtimeout" do
host_name = "localhost"
opts = "safe=true&wtimeout=10&wtimeoutMS=2000"
@conn = Connection.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
assert_equal({:wtimeout => 2}, @conn.safe)
end
should "set timeout options on connection" do should "set timeout options on connection" do
host_name = "localhost" host_name = "localhost"
opts = "connectTimeoutMS=1000&socketTimeoutMS=5000" opts = "connectTimeoutMS=1000&socketTimeoutMS=5000"

View File

@ -78,12 +78,13 @@ class TestThreading < Test::Unit::TestCase
end end
def test_opts_safe def test_opts_safe
parser = Mongo::URIParser.new('mongodb://localhost:27018?safe=true;w=2;journal=true;wtimeout=200;fsync=true') parser = Mongo::URIParser.new('mongodb://localhost:27018?safe=true;w=2;journal=true;wtimeout=200;fsync=true;wtimeoutMS=200')
assert parser.safe assert parser.safe
assert_equal 2, parser.w assert_equal 2, parser.w
assert_equal 200, parser.wtimeout assert_equal 200, parser.wtimeout
assert parser.fsync assert parser.fsync
assert parser.journal assert parser.journal
assert_equal 200, parser.wtimeoutMS
end end
def test_opts_unsafe_timeout def test_opts_unsafe_timeout