Added some basic, sanity-checking tests.

This commit is contained in:
Kyle Banker 2011-10-07 13:55:09 -04:00
parent 118cb18c33
commit f5f714a1a1
4 changed files with 50 additions and 46 deletions

View File

@ -22,7 +22,7 @@ module Mongo
# Instantiates and manages connections to a MongoDB replica set.
class ReplSetConnection < Connection
attr_reader :nodes, :secondaries, :arbiters, :secondary_pools,
attr_reader :secondaries, :arbiters, :secondary_pools,
:replica_set_name, :read_pool, :seeds, :tags_to_pools,
:refresh_interval, :refresh_mode
@ -101,9 +101,6 @@ module Mongo
# TODO: get rid of this
@nodes = @seeds.dup
# The members of the replica set, stored as instances of Mongo::Node.
@members = []
# Connection pool for primary node
@primary = nil
@primary_pool = nil

View File

@ -8,7 +8,6 @@ module Mongo
def initialize(connection, seeds)
@connection = connection
@seeds = seeds
@refresh_node = nil
@previously_connected = false
end
@ -31,11 +30,7 @@ module Mongo
end
def healthy?
if !@refresh_node || !refresh_node.set_config
return false
end
#if refresh_node.node_list
end
def close

View File

@ -0,0 +1,47 @@
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require './test/replica_sets/rs_test_helper'
class ConnectTest < Test::Unit::TestCase
include Mongo
def teardown
RS.restart_killed_nodes
@conn.close if defined?(@conn) && @conn
end
def test_connect
@conn = ReplSetConnection.new([RS.host, RS.ports[1]], [RS.host, RS.ports[0]],
[RS.host, RS.ports[2]], :name => RS.name)
assert @conn.connected?
assert_equal RS.primary, @conn.primary
assert_equal RS.secondaries.sort, @conn.secondaries.sort
assert_equal RS.arbiters.sort, @conn.arbiters.sort
@conn = ReplSetConnection.new([RS.host, RS.ports[1]], [RS.host, RS.ports[0]],
:name => RS.name)
assert @conn.connected?
end
def test_accessors
seeds = [RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
[RS.host, RS.ports[2]]
@conn = ReplSetConnection.new(*seeds, :name => RS.name)
assert_equal @conn.host, RS.primary[0]
assert_equal @conn.port, RS.primary[1]
assert_equal @conn.host, @conn.primary_pool.host
assert_equal @conn.port, @conn.primary_pool.port
assert_equal @conn.nodes, @conn.seeds
assert_equal 2, @conn.secondaries.length
assert_equal 2, @conn.arbiters.length
assert_equal 2, @conn.secondary_pools.length
assert_equal RS.name, @conn.replica_set_name
assert @conn.secondary_pools.include?(@conn.read_pool)
assert_equal seeds.sort {|a,b| a[1] <=> b[1]},
@conn.seeds.sort {|a,b| a[1] <=> b[1]}
assert_equal 5, @conn.tags_to_pools.keys.length
assert_equal 90, @conn.refresh_interval
assert_equal @conn.refresh_mode, :sync
end
end

View File

@ -11,6 +11,8 @@ class ConnectTest < Test::Unit::TestCase
@conn.close if defined?(@conn) && @conn
end
# TODO: test connect timeout.
def test_connect_with_deprecated_multi
@conn = Connection.multi([[RS.host, RS.ports[0]], [RS.host, RS.ports[1]]], :name => RS.name)
assert @conn.is_a?(ReplSetConnection)
@ -24,43 +26,6 @@ class ConnectTest < Test::Unit::TestCase
end
end
# def test_connect_timeout
# passed = false
# timeout = 3
# begin
# t0 = Time.now
# @conn = ReplSetConnection.new(['192.169.169.1', 27017], :connect_timeout => timeout)
# rescue OperationTimeout
# passed = true
# t1 = Time.now
# end
# assert passed
# assert t1 - t0 < timeout + 1
# end
def test_connect
@conn = ReplSetConnection.new([RS.host, RS.ports[1]], [RS.host, RS.ports[0]],
[RS.host, RS.ports[2]], :name => RS.name)
assert @conn.connected?
assert_equal RS.primary, @conn.primary
assert_equal RS.secondaries.sort, @conn.secondaries.sort
assert_equal RS.arbiters.sort, @conn.arbiters.sort
@conn = ReplSetConnection.new([RS.host, RS.ports[1]], [RS.host, RS.ports[0]],
:name => RS.name)
assert @conn.connected?
end
def test_host_port_accessors
@conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
[RS.host, RS.ports[2]], :name => RS.name)
assert_equal @conn.host, RS.primary[0]
assert_equal @conn.port, RS.primary[1]
end
def test_connect_with_primary_node_killed
node = RS.kill_primary