2009-11-23 20:20:05 +00:00
|
|
|
require 'test/test_helper'
|
|
|
|
|
|
|
|
class ConnectionTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def new_mock_socket
|
|
|
|
socket = Object.new
|
|
|
|
socket.stubs(:setsockopt).with(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
|
|
|
socket
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_mock_db
|
|
|
|
db = Object.new
|
|
|
|
end
|
|
|
|
|
2009-11-25 15:25:28 +00:00
|
|
|
context "Initialization: " do
|
2009-11-23 20:20:05 +00:00
|
|
|
|
2009-11-25 15:25:28 +00:00
|
|
|
context "given a single node" do
|
|
|
|
setup do
|
2009-11-23 21:03:33 +00:00
|
|
|
TCPSocket.stubs(:new).returns(new_mock_socket)
|
2009-12-02 21:43:30 +00:00
|
|
|
@conn = Connection.new('localhost', 27017, :connect => false)
|
2009-11-23 20:20:05 +00:00
|
|
|
|
|
|
|
admin_db = new_mock_db
|
|
|
|
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
|
|
|
|
@conn.expects(:[]).with('admin').returns(admin_db)
|
|
|
|
@conn.connect_to_master
|
|
|
|
end
|
|
|
|
|
2009-11-25 15:25:28 +00:00
|
|
|
should "set localhost and port to master" do
|
2009-11-23 20:20:05 +00:00
|
|
|
assert_equal 'localhost', @conn.host
|
|
|
|
assert_equal 27017, @conn.port
|
|
|
|
end
|
|
|
|
|
2009-11-25 15:25:28 +00:00
|
|
|
should "set connection pool to 1" do
|
2009-11-23 20:20:05 +00:00
|
|
|
assert_equal 1, @conn.size
|
|
|
|
end
|
|
|
|
|
2009-11-25 15:25:28 +00:00
|
|
|
should "default slave_ok to false" do
|
2009-11-23 20:20:05 +00:00
|
|
|
assert !@conn.slave_ok?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-02 21:43:30 +00:00
|
|
|
context "with a nonstandard port" do
|
|
|
|
setup do
|
|
|
|
TCPSocket.stubs(:new).returns(new_mock_socket)
|
|
|
|
@conn = Connection.new('255.255.255.255', 2500, :connect => false)
|
|
|
|
admin_db = new_mock_db
|
|
|
|
admin_db.expects(:command).returns({'ok' => 1, 'ismaster' => 1})
|
|
|
|
@conn.expects(:[]).with('admin').returns(admin_db)
|
|
|
|
@conn.connect_to_master
|
|
|
|
end
|
|
|
|
|
|
|
|
should "set localhost and port correctly" do
|
|
|
|
assert_equal '255.255.255.255', @conn.host
|
|
|
|
assert_equal 2500, @conn.port
|
|
|
|
end
|
|
|
|
end
|
2009-11-25 15:25:28 +00:00
|
|
|
end
|
2009-11-23 20:20:05 +00:00
|
|
|
|