00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 """Test for master slave connections."""
00016
00017 import unittest
00018 import os
00019 import time
00020 import sys
00021 sys.path[0:0] = [""]
00022
00023 from nose.plugins.skip import SkipTest
00024
00025 from pymongo.errors import ConnectionFailure, InvalidName
00026 from pymongo.errors import CollectionInvalid, OperationFailure
00027 from pymongo.database import Database
00028 from pymongo.connection import Connection
00029 from pymongo.collection import Collection
00030 from pymongo.master_slave_connection import MasterSlaveConnection
00031
00032
00033 class TestMasterSlaveConnection(unittest.TestCase):
00034
00035 def setUp(self):
00036 host = os.environ.get("DB_IP", "localhost")
00037 self.master = Connection(host, int(os.environ.get("DB_PORT", 27017)))
00038
00039 self.slaves = []
00040 try:
00041 self.slaves.append(Connection(os.environ.get("DB_IP2", host),
00042 int(os.environ.get("DB_PORT2", 27018)),
00043 slave_okay=True))
00044 except ConnectionFailure:
00045 pass
00046
00047 try:
00048 self.slaves.append(Connection(os.environ.get("DB_IP3", host),
00049 int(os.environ.get("DB_PORT3", 27019)),
00050 slave_okay=True))
00051 except ConnectionFailure:
00052 pass
00053
00054 if not self.slaves:
00055 raise SkipTest()
00056
00057 self.connection = MasterSlaveConnection(self.master, self.slaves)
00058 self.db = self.connection.pymongo_test
00059
00060 def test_types(self):
00061 self.assertRaises(TypeError, MasterSlaveConnection, 1)
00062 self.assertRaises(TypeError, MasterSlaveConnection, self.master, 1)
00063 self.assertRaises(TypeError, MasterSlaveConnection, self.master, [1])
00064
00065 def test_repr(self):
00066 self.assertEqual(repr(self.connection),
00067 "MasterSlaveConnection(%r, %r)" %
00068 (self.master, self.slaves))
00069
00070 def test_get_db(self):
00071
00072 def make_db(base, name):
00073 return base[name]
00074
00075 self.assertRaises(InvalidName, make_db, self.connection, "")
00076 self.assertRaises(InvalidName, make_db, self.connection, "te$t")
00077 self.assertRaises(InvalidName, make_db, self.connection, "te.t")
00078 self.assertRaises(InvalidName, make_db, self.connection, "te\\t")
00079 self.assertRaises(InvalidName, make_db, self.connection, "te/t")
00080 self.assertRaises(InvalidName, make_db, self.connection, "te st")
00081
00082 self.assert_(isinstance(self.connection.test, Database))
00083 self.assertEqual(self.connection.test, self.connection["test"])
00084 self.assertEqual(self.connection.test, Database(self.connection,
00085 "test"))
00086
00087 def test_database_names(self):
00088 self.connection.pymongo_test.test.save({"dummy": u"object"})
00089 self.connection.pymongo_test_mike.test.save({"dummy": u"object"})
00090
00091 dbs = self.connection.database_names()
00092 self.assert_("pymongo_test" in dbs)
00093 self.assert_("pymongo_test_mike" in dbs)
00094
00095 def test_drop_database(self):
00096 self.assertRaises(TypeError, self.connection.drop_database, 5)
00097 self.assertRaises(TypeError, self.connection.drop_database, None)
00098
00099 self.connection.pymongo_test.test.save({"dummy": u"object"}, safe=True)
00100 dbs = self.connection.database_names()
00101 self.assert_("pymongo_test" in dbs)
00102 self.connection.drop_database("pymongo_test")
00103 dbs = self.connection.database_names()
00104 self.assert_("pymongo_test" not in dbs)
00105
00106 self.connection.pymongo_test.test.save({"dummy": u"object"})
00107 dbs = self.connection.database_names()
00108 self.assert_("pymongo_test" in dbs)
00109 self.connection.drop_database(self.connection.pymongo_test)
00110 dbs = self.connection.database_names()
00111 self.assert_("pymongo_test" not in dbs)
00112
00113 def test_iteration(self):
00114
00115 def iterate():
00116 [a for a in self.connection]
00117
00118 self.assertRaises(TypeError, iterate)
00119
00120 def test_insert_find_one_in_request(self):
00121 count = 0
00122 for i in range(100):
00123 self.connection.start_request()
00124 self.db.test.remove({})
00125 self.db.test.insert({"x": i})
00126 try:
00127 if i != self.db.test.find_one()["x"]:
00128 count += 1
00129 except:
00130 count += 1
00131 self.connection.end_request()
00132 self.assertFalse(count)
00133
00134
00135 def test_create_collection(self):
00136 self.connection.drop_database('pymongo_test')
00137
00138 collection = self.db.create_collection('test')
00139 self.assert_(isinstance(collection, Collection))
00140
00141 self.assertRaises(CollectionInvalid, self.db.create_collection, 'test')
00142
00143
00144 def test_unique_index(self):
00145 self.connection.drop_database('pymongo_test')
00146 self.db.test.create_index('username', unique=True)
00147
00148 self.db.test.save({'username': 'mike'}, safe=True)
00149 self.assertRaises(OperationFailure, self.db.test.save, {'username': 'mike'}, safe=True)
00150
00151
00152
00153 def test_insert_find_one_with_slaves(self):
00154 count = 0
00155 for i in range(100):
00156 self.db.test.remove({})
00157 self.db.test.insert({"x": i})
00158 try:
00159 if i != self.db.test.find_one()["x"]:
00160 count += 1
00161 except:
00162 count += 1
00163 self.assert_(count)
00164
00165
00166
00167 def test_insert_find_one_with_pause(self):
00168 count = 0
00169
00170 self.db.test.remove({})
00171 self.db.test.insert({"x": 5586})
00172 time.sleep(11)
00173 for _ in range(10):
00174 try:
00175 if 5586 != self.db.test.find_one()["x"]:
00176 count += 1
00177 except:
00178 count += 1
00179 self.assertFalse(count)
00180
00181 def test_kill_cursors(self):
00182
00183 def cursor_count():
00184 count = 0
00185 res = self.connection.master.test_pymongo.command("cursorInfo")
00186 count += res["clientCursors_size"]
00187 for slave in self.connection.slaves:
00188 res = slave.test_pymongo.command("cursorInfo")
00189 count += res["clientCursors_size"]
00190 return count
00191
00192 self.connection.test_pymongo.drop_collection("test")
00193 db = self.db
00194
00195 before = cursor_count()
00196
00197 for i in range(10000):
00198 db.test.insert({"i": i})
00199 time.sleep(11)
00200
00201 self.assertEqual(before, cursor_count())
00202
00203 for _ in range(10):
00204 db.test.find_one()
00205
00206 self.assertEqual(before, cursor_count())
00207
00208 for _ in range(10):
00209 for x in db.test.find():
00210 break
00211
00212 self.assertEqual(before, cursor_count())
00213
00214 a = db.test.find()
00215 for x in a:
00216 break
00217
00218 self.assertNotEqual(before, cursor_count())
00219
00220 del a
00221
00222 self.assertEqual(before, cursor_count())
00223
00224 a = db.test.find().limit(10)
00225 for x in a:
00226 break
00227
00228 self.assertEqual(before, cursor_count())
00229
00230 if __name__ == "__main__":
00231 unittest.main()