test killing cursors on master-slave

This commit is contained in:
Mike Dirolf 2009-03-09 14:02:51 -04:00
parent 33ef246812
commit 98dd6765bb
3 changed files with 58 additions and 1 deletions

View File

@ -66,7 +66,10 @@ class Cursor(object):
"""Closes this cursor.
"""
if self.__id and not self.__killed:
self.__collection.database().connection().close_cursor(self.__id)
if self.__connection_id is not None:
self.__collection.database().connection().close_cursor(self.__id, self.__connection_id)
else:
self.__collection.database().connection().close_cursor(self.__id)
self.__killed = True
def __query_spec(self):

View File

@ -60,6 +60,14 @@ class MasterSlaveConnection(object):
self.__master = master
self.__slaves = slaves
@property
def master(self):
return self.__master
@property
def slaves(self):
return self.__slaves
def set_cursor_manager(self, manager_class):
"""Set the cursor manager for this connection.

View File

@ -165,6 +165,52 @@ class TestMasterSlaveConnection(unittest.TestCase):
count += 1
self.failIf(count)
def test_kill_cursors(self):
def cursor_count():
count = 0
res = self.connection.master.test_pymongo._command({"cursorInfo":1})
count += res["clientCursors_size"]
for slave in self.connection.slaves:
res = slave.test_pymongo._command({"cursorInfo":1})
count += res["clientCursors_size"]
return count
self.connection.test_pymongo.drop_collection("test")
db = self.db
before = cursor_count()
for i in range(10000):
db.test.insert({"i": i})
self.assertEqual(before, cursor_count())
for _ in range(10):
db.test.find_one()
self.assertEqual(before, cursor_count())
for _ in range(10):
for x in db.test.find():
break
self.assertEqual(before, cursor_count())
a = db.test.find()
for x in a:
break
self.assertNotEqual(before, cursor_count())
del a
self.assertEqual(before, cursor_count())
a = db.test.find().limit(10)
for x in a:
break
self.assertEqual(before, cursor_count())
if __name__ == "__main__":
unittest.main()