def test_cursor_index_each(self): # test_collection was filled out in setUp() with 200 docs coll = self.cx.pymongo_test.test_collection results = set() yield_points = [] for i in range(3): yield_points.append((yield gen.Callback(i))) def each(result, error): if error: raise error if result: results.add(result['_id']) else: yield_points.pop()() coll.find({}, {'_id': 1}).sort([('_id', 1)])[0].each(each) coll.find({}, {'_id': 1}).sort([('_id', 1)])[5].each(each) # Only 200 documents, so 1000th doc doesn't exist. PyMongo raises # IndexError here, but Motor simply returns None, which won't show up # in results. coll.find()[1000].each(each) yield gen.WaitAll(list(range(3))) self.assertEqual(set([0, 5]), results)
def test_find_is_async(self): # Confirm find() is async by launching two operations which will finish # out of order. Also test that MotorClient doesn't reuse sockets # incorrectly. # Launch find operations for _id's 1 and 2 which will finish in order # 2, then 1. results = [] yield_points = [(yield gen.Callback(0)), (yield gen.Callback(1))] def callback(result, error): if result: results.append(result) yield_points.pop()() # This find() takes 0.5 seconds self.cx.pymongo_test.test_collection.find( { '_id': 1, '$where': delay(0.5) }, fields={ 's': True, '_id': False }).limit(1).each(callback) # Very fast lookup self.cx.pymongo_test.test_collection.find({ '_id': 2 }, fields={ 's': True, '_id': False }).limit(1).each(callback) yield gen.WaitAll([0, 1]) # Results were appended in order 2, 1 self.assertEqual([{'s': hex(s)} for s in (2, 1)], results)
def test_find_one_is_async(self): # Confirm find_one() is async by launching two operations which will # finish out of order. # Launch 2 find_one operations for _id's 1 and 2, which will finish in # order 2 then 1. results = [] yield_points = [(yield gen.Callback(0)), (yield gen.Callback(1))] def callback(result, error): if result: results.append(result) yield_points.pop()() # This find_one() takes 3 seconds self.cx.pymongo_test.test_collection.find_one( { '_id': 1, '$where': delay(3) }, fields={ 's': True, '_id': False }, callback=callback) # Very fast lookup self.cx.pymongo_test.test_collection.find_one({'_id': 2}, fields={ 's': True, '_id': False }, callback=callback) yield gen.WaitAll([0, 1]) # Results were appended in order 2, 1 self.assertEqual([{'s': hex(s)} for s in (2, 1)], results)
def f(): (yield gen.Callback("k1"))("v1") (yield gen.Callback("k2"))("v2") results = yield gen.WaitAll(["k1", "k2"]) self.assertEqual(results, ["v1", "v2"]) self.stop()