From fdb4ed7dc5ee5afdf25a090cfecac540db4d760c Mon Sep 17 00:00:00 2001 From: Jim Menard Date: Wed, 14 Jan 2009 15:49:49 -0500 Subject: [PATCH] New switch_to_master db method. Reorganized db tests a bit. --- lib/mongo/db.rb | 14 ++++++++++++++ tests/test_db.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++ tests/test_db_api.rb | 15 --------------- 3 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 tests/test_db.rb diff --git a/lib/mongo/db.rb b/lib/mongo/db.rb index 2e8719b..e5610f5 100644 --- a/lib/mongo/db.rb +++ b/lib/mongo/db.rb @@ -167,6 +167,20 @@ module XGen end end + # Switches our socket to the master database. If we are already the + # master, no change is made. + def switch_to_master + master_str = master() + unless master_str == "#@host:#@port" + @semaphore.synchronize { + master_str =~ /(.+):(\d+)/ + @host, @port = $1, $2 + close() + @socket = TCPSocket.new(@host, @port) + } + end + end + # Close the connection to the database. def close @socket.close diff --git a/tests/test_db.rb b/tests/test_db.rb new file mode 100644 index 0000000..9474b14 --- /dev/null +++ b/tests/test_db.rb @@ -0,0 +1,46 @@ +$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib') +require 'mongo' +require 'test/unit' + +# NOTE: assumes Mongo is running +class DBAPITest < Test::Unit::TestCase + + include XGen::Mongo::Driver + + def setup + host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' + port = ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::DEFAULT_PORT + @db = Mongo.new(host, port).db('ruby-mongo-test') + @coll = @db.collection('test') + @coll.clear + @r1 = @coll.insert('a' => 1) # collection not created until it's used + @coll_full_name = 'ruby-mongo-test.test' + end + + def teardown + @coll.clear unless @coll == nil || @db.socket.closed? + end + + def test_close + @db.close + assert @db.socket.closed? + begin + @coll.insert('a' => 1) + fail "expected IOError exception" + rescue IOError => ex + assert_match /closed stream/, ex.to_s + end + end + + def test_full_coll_name + assert_equal @coll_full_name, @db.full_coll_name(@coll.name) + end + + def test_master + # Doesn't really test anything since we probably only have one database + # during this test. + @db.switch_to_master + assert_not_nil @db.socket + end + +end diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index eeefaa9..33f3c5a 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -196,17 +196,6 @@ class DBAPITest < Test::Unit::TestCase assert_equal 4, docs.size end - def test_close - @db.close - assert @db.socket.closed? - begin - @coll.insert('a' => 1) - fail "expected IOError exception" - rescue IOError => ex - assert_match /closed stream/, ex.to_s - end - end - def test_drop_collection assert @db.drop_collection(@coll.name), "drop of collection #{@coll.name} failed" assert !@db.collection_names.include?(@coll_full_name) @@ -253,10 +242,6 @@ class DBAPITest < Test::Unit::TestCase end end - def test_full_coll_name - assert_equal @coll_full_name, @db.full_coll_name(@coll.name) - end - def test_index_information @db.create_index(@coll.name, 'index_name', ['a']) list = @db.index_information(@coll.name)