2012-04-11 15:08:04 +00:00
|
|
|
require File.expand_path("../../test_helper", __FILE__)
|
2010-02-20 00:17:38 +00:00
|
|
|
include Mongo
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
class ConnectionTest < Test::Unit::TestCase
|
|
|
|
context "Initialization: " do
|
|
|
|
context "given a single node" do
|
|
|
|
setup do
|
|
|
|
@conn = Connection.new('localhost', 27017, :connect => false)
|
2010-07-19 16:07:46 +00:00
|
|
|
TCPSocket.stubs(:new).returns(new_mock_socket)
|
2009-11-23 20:20:05 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
admin_db = new_mock_db
|
2011-08-25 18:57:24 +00:00
|
|
|
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
|
|
|
|
@conn.expects(:[]).with('admin').returns(admin_db)
|
2010-07-19 16:07:46 +00:00
|
|
|
@conn.connect
|
2010-02-22 20:49:04 +00:00
|
|
|
end
|
2009-11-23 20:20:05 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "set localhost and port to master" do
|
2010-11-16 20:43:59 +00:00
|
|
|
assert_equal 'localhost', @conn.primary_pool.host
|
|
|
|
assert_equal 27017, @conn.primary_pool.port
|
2010-02-22 20:49:04 +00:00
|
|
|
end
|
2009-11-23 20:20:05 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "set connection pool to 1" do
|
2010-11-16 20:43:59 +00:00
|
|
|
assert_equal 1, @conn.primary_pool.size
|
2009-11-23 20:20:05 +00:00
|
|
|
end
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "default slave_ok to false" do
|
|
|
|
assert !@conn.slave_ok?
|
2009-11-23 20:20:05 +00:00
|
|
|
end
|
2012-02-27 21:42:16 +00:00
|
|
|
|
2012-02-27 19:06:07 +00:00
|
|
|
should "warn if invalid options are specified" do
|
|
|
|
conn = Connection.allocate
|
|
|
|
opts = {:connect => false}
|
2012-02-27 21:42:16 +00:00
|
|
|
|
2012-02-27 19:06:07 +00:00
|
|
|
ReplSetConnection::REPL_SET_OPTS.each do |opt|
|
|
|
|
conn.expects(:warn).with("#{opt} is not a valid option for #{conn.class}")
|
|
|
|
opts[opt] = true
|
|
|
|
end
|
2012-02-27 21:42:16 +00:00
|
|
|
|
2012-02-27 19:06:07 +00:00
|
|
|
args = ['localhost', 27017, opts]
|
|
|
|
conn.send(:initialize, *args)
|
|
|
|
end
|
2012-02-27 21:42:16 +00:00
|
|
|
|
2012-02-27 19:06:07 +00:00
|
|
|
context "given a replica set" do
|
2012-04-04 17:45:19 +00:00
|
|
|
#should "enforce a minimum refresh_interval" do
|
|
|
|
# @conn = ReplSetConnection.new(['localhost:27017'],
|
|
|
|
# :connect => false, :refresh_mode => :sync, :refresh_interval => 10)
|
|
|
|
# assert_equal 60, @conn.refresh_interval
|
|
|
|
#end
|
2012-03-21 23:01:37 +00:00
|
|
|
|
2012-02-27 19:06:07 +00:00
|
|
|
should "warn if invalid options are specified" do
|
|
|
|
conn = ReplSetConnection.allocate
|
|
|
|
opts = {:connect => false}
|
2012-02-27 21:42:16 +00:00
|
|
|
|
2012-02-27 19:06:07 +00:00
|
|
|
Connection::CONNECTION_OPTS.each do |opt|
|
|
|
|
conn.expects(:warn).with("#{opt} is not a valid option for #{conn.class}")
|
|
|
|
opts[opt] = true
|
|
|
|
end
|
2012-02-27 21:42:16 +00:00
|
|
|
|
2012-02-27 19:06:07 +00:00
|
|
|
args = [['localhost:27017'], opts]
|
|
|
|
conn.send(:initialize, *args)
|
|
|
|
end
|
|
|
|
end
|
2009-11-23 20:20:05 +00:00
|
|
|
end
|
2010-02-17 20:15:07 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
context "initializing with a mongodb uri" do
|
|
|
|
should "parse a simple uri" do
|
|
|
|
@conn = Connection.from_uri("mongodb://localhost", :connect => false)
|
2010-12-10 21:00:35 +00:00
|
|
|
assert_equal ['localhost', 27017], @conn.host_to_try
|
2010-02-22 20:49:04 +00:00
|
|
|
end
|
2010-02-17 20:15:07 +00:00
|
|
|
|
2010-06-15 02:06:55 +00:00
|
|
|
should "allow a complex host names" do
|
|
|
|
host_name = "foo.bar-12345.org"
|
|
|
|
@conn = Connection.from_uri("mongodb://#{host_name}", :connect => false)
|
2010-12-10 21:00:35 +00:00
|
|
|
assert_equal [host_name, 27017], @conn.host_to_try
|
2010-02-17 20:15:07 +00:00
|
|
|
end
|
|
|
|
|
2011-09-26 20:27:10 +00:00
|
|
|
should "allow db without username and password" do
|
|
|
|
host_name = "foo.bar-12345.org"
|
|
|
|
@conn = Connection.from_uri("mongodb://#{host_name}/foo", :connect => false)
|
|
|
|
assert_equal [host_name, 27017], @conn.host_to_try
|
|
|
|
end
|
2012-02-27 21:42:16 +00:00
|
|
|
|
2012-01-17 23:45:09 +00:00
|
|
|
should "set safe options on connection" do
|
|
|
|
host_name = "localhost"
|
2012-01-26 21:52:25 +00:00
|
|
|
opts = "safe=true&w=2&wtimeoutMS=1000&fsync=true&journal=true"
|
2012-01-17 23:45:09 +00:00
|
|
|
@conn = Connection.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
|
2012-01-26 21:52:25 +00:00
|
|
|
assert_equal({:w => 2, :wtimeout => 1000, :fsync => true, :j => true}, @conn.safe)
|
2012-01-17 23:45:09 +00:00
|
|
|
end
|
2012-02-27 21:42:16 +00:00
|
|
|
|
2012-01-17 23:45:09 +00:00
|
|
|
should "set timeout options on connection" do
|
|
|
|
host_name = "localhost"
|
|
|
|
opts = "connectTimeoutMS=1000&socketTimeoutMS=5000"
|
|
|
|
@conn = Connection.from_uri("mongodb://#{host_name}/foo?#{opts}", :connect => false)
|
|
|
|
assert_equal 1, @conn.connect_timeout
|
|
|
|
assert_equal 5, @conn.op_timeout
|
|
|
|
end
|
2011-09-26 20:27:10 +00:00
|
|
|
|
2010-06-19 19:15:19 +00:00
|
|
|
should "parse a uri with a hyphen & underscore in the username or password" do
|
|
|
|
@conn = Connection.from_uri("mongodb://hyphen-user_name:p-s_s@localhost:27017/db", :connect => false)
|
2010-12-10 21:00:35 +00:00
|
|
|
assert_equal ['localhost', 27017], @conn.host_to_try
|
2010-06-19 19:15:19 +00:00
|
|
|
auth_hash = { 'db_name' => 'db', 'username' => 'hyphen-user_name', "password" => 'p-s_s' }
|
|
|
|
assert_equal auth_hash, @conn.auths[0]
|
|
|
|
end
|
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "attempt to connect" do
|
|
|
|
TCPSocket.stubs(:new).returns(new_mock_socket)
|
|
|
|
@conn = Connection.from_uri("mongodb://localhost", :connect => false)
|
|
|
|
|
|
|
|
admin_db = new_mock_db
|
2011-08-25 18:57:24 +00:00
|
|
|
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
|
|
|
|
@conn.expects(:[]).with('admin').returns(admin_db)
|
2010-07-19 16:07:46 +00:00
|
|
|
@conn.connect
|
2010-02-17 20:15:07 +00:00
|
|
|
end
|
2009-11-23 20:20:05 +00:00
|
|
|
|
2010-02-22 20:49:04 +00:00
|
|
|
should "raise an error on invalid uris" do
|
|
|
|
assert_raise MongoArgumentError do
|
|
|
|
Connection.from_uri("mongo://localhost", :connect => false)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise MongoArgumentError do
|
|
|
|
Connection.from_uri("mongodb://localhost:abc", :connect => false)
|
|
|
|
end
|
2010-02-20 00:17:38 +00:00
|
|
|
end
|
2009-12-02 21:43:30 +00:00
|
|
|
|
2011-09-26 20:27:10 +00:00
|
|
|
should "require all of username, if password and db are specified" do
|
|
|
|
assert Connection.from_uri("mongodb://kyle:jones@localhost/db", :connect => false)
|
2010-02-22 20:49:04 +00:00
|
|
|
|
|
|
|
assert_raise MongoArgumentError do
|
|
|
|
Connection.from_uri("mongodb://kyle:password@localhost", :connect => false)
|
|
|
|
end
|
2010-02-20 00:17:38 +00:00
|
|
|
end
|
2009-12-02 21:43:30 +00:00
|
|
|
end
|
|
|
|
end
|
2009-11-25 15:25:28 +00:00
|
|
|
end
|