<s> from __future__ import unicode_literals <EOL> """<STR_LIT>""" <EOL> import gc <EOL> import sys <EOL> import unittest <EOL> import warnings <EOL> import greenlet <EOL> import pymongo <EOL> from mockupdb import OpQuery <EOL> from mockupdb import OpKillCursors <EOL> from tornado import gen <EOL> from tornado . concurrent import Future <EOL> from tornado . testing import gen_test <EOL> from tornado import version_info as tornado_version <EOL> from pymongo . errors import InvalidOperation , ExecutionTimeout <EOL> from pymongo . errors import OperationFailure <EOL> import motor <EOL> import motor . motor_tornado <EOL> from test import SkipTest <EOL> from test . tornado_tests import ( at_least , get_command_line , <EOL> MotorTest , MotorMockServerTest , <EOL> server_is_mongos ) <EOL> from test . utils import one , safe_get <EOL> class MotorCursorTest ( MotorMockServerTest ) : <EOL> def test_cursor ( self ) : <EOL> cursor = self . collection . find ( ) <EOL> self . assertTrue ( isinstance ( cursor , motor . motor_tornado . MotorCursor ) ) <EOL> self . assertFalse ( cursor . started , "<STR_LIT>" ) <EOL> @ gen_test <EOL> def test_count_callback ( self ) : <EOL> yield self . check_optional_callback ( self . collection . find ( ) . count ) <EOL> @ gen_test <EOL> def test_count ( self ) : <EOL> yield self . make_test_data ( ) <EOL> coll = self . collection <EOL> self . assertEqual ( <NUM_LIT:200> , ( yield coll . find ( ) . count ( ) ) ) <EOL> self . assertEqual ( <NUM_LIT:100> , ( yield coll . find ( { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } } ) . count ( ) ) ) <EOL> where = '<STR_LIT>' <EOL> self . assertEqual ( <NUM_LIT> , ( yield coll . find ( { '<STR_LIT>' : where } ) . count ( ) ) ) <EOL> self . assertEqual ( <NUM_LIT> , ( yield coll . find ( ) . where ( where ) . count ( ) ) ) <EOL> self . assertEqual ( <EOL> <NUM_LIT> , <EOL> ( yield coll . find ( { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:100> } } ) . where ( where ) . count ( ) ) ) <EOL> self . assertEqual ( <EOL> <NUM_LIT> , <EOL> ( yield coll . find ( { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:100> } , '<STR_LIT>' : where } ) . count ( ) ) ) <EOL> @ gen_test <EOL> def test_fetch_next ( self ) : <EOL> yield self . make_test_data ( ) <EOL> coll = self . collection <EOL> cursor = coll . find ( { } , { '<STR_LIT>' : <NUM_LIT:1> } ) . sort ( <EOL> [ ( '<STR_LIT>' , pymongo . ASCENDING ) ] ) . batch_size ( <NUM_LIT> ) <EOL> self . assertEqual ( None , cursor . cursor_id ) <EOL> self . assertEqual ( None , cursor . next_object ( ) ) <EOL> i = <NUM_LIT:0> <EOL> while ( yield cursor . fetch_next ) : <EOL> self . assertEqual ( { '<STR_LIT>' : i } , cursor . next_object ( ) ) <EOL> i += <NUM_LIT:1> <EOL> if i <= <NUM_LIT> : <EOL> self . assertNotEqual ( <NUM_LIT:0> , cursor . cursor_id ) <EOL> else : <EOL> self . assertEqual ( <NUM_LIT:0> , cursor . cursor_id ) <EOL> self . assertEqual ( False , ( yield cursor . fetch_next ) ) <EOL> self . assertEqual ( None , cursor . next_object ( ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , cursor . cursor_id ) <EOL> self . assertEqual ( <NUM_LIT:200> , i ) <EOL> @ gen_test <EOL> def test_fetch_next_delete ( self ) : <EOL> if '<STR_LIT>' in sys . version : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> client , server = self . client_server ( auto_ismaster = { '<STR_LIT>' : True } ) <EOL> cursor = client . test . collection . find ( ) <EOL> cursor . fetch_next <EOL> request = yield self . run_thread ( server . receives , OpQuery ) <EOL> request . replies ( { '<STR_LIT>' : <NUM_LIT:1> } , cursor_id = <NUM_LIT> ) <EOL> del cursor <EOL> yield self . run_thread ( server . receives , <EOL> OpKillCursors ( cursor_ids = [ <NUM_LIT> ] ) ) <EOL> @ gen_test <EOL> def test_fetch_next_without_results ( self ) : <EOL> coll = self . collection <EOL> cursor = coll . find ( { '<STR_LIT:foo>' : '<STR_LIT:bar>' } ) <EOL> self . assertEqual ( None , cursor . next_object ( ) ) <EOL> self . assertEqual ( False , ( yield cursor . fetch_next ) ) <EOL> self . assertEqual ( None , cursor . next_object ( ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , cursor . cursor_id ) <EOL> @ gen_test <EOL> def test_fetch_next_is_idempotent ( self ) : <EOL> yield self . make_test_data ( ) <EOL> coll = self . collection <EOL> cursor = coll . find ( ) <EOL> self . assertEqual ( None , cursor . cursor_id ) <EOL> yield cursor . fetch_next <EOL> self . assertTrue ( cursor . cursor_id ) <EOL> self . assertEqual ( <NUM_LIT> , cursor . _buffer_size ( ) ) <EOL> yield cursor . fetch_next <EOL> self . assertEqual ( <NUM_LIT> , cursor . _buffer_size ( ) ) <EOL> yield cursor . close ( ) <EOL> @ gen_test <EOL> def test_fetch_next_exception ( self ) : <EOL> coll = self . collection <EOL> cursor = coll . find ( ) <EOL> cursor . delegate . _Cursor__id = <NUM_LIT> <EOL> with self . assertRaises ( OperationFailure ) : <EOL> yield cursor . fetch_next <EOL> cursor . delegate . _Cursor__id = None <EOL> @ gen_test <EOL> def test_each_callback ( self ) : <EOL> cursor = self . collection . find ( ) <EOL> self . assertRaises ( TypeError , cursor . each , callback = '<STR_LIT:foo>' ) <EOL> self . assertRaises ( TypeError , cursor . each , callback = None ) <EOL> self . assertRaises ( TypeError , cursor . each ) <EOL> ( result , error ) , _ = yield gen . Task ( cursor . each ) <EOL> if error : <EOL> raise error <EOL> @ gen_test ( timeout = <NUM_LIT:30> ) <EOL> def test_each ( self ) : <EOL> yield self . make_test_data ( ) <EOL> cursor = self . collection . find ( { } , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> cursor . sort ( [ ( '<STR_LIT>' , pymongo . ASCENDING ) ] ) <EOL> future = Future ( ) <EOL> results = [ ] <EOL> def callback ( result , error ) : <EOL> if error : <EOL> raise error <EOL> if result is not None : <EOL> results . append ( result ) <EOL> else : <EOL> future . set_result ( True ) <EOL> cursor . each ( callback ) <EOL> yield future <EOL> expected = [ { '<STR_LIT>' : i } for i in range ( <NUM_LIT:200> ) ] <EOL> self . assertEqual ( expected , results ) <EOL> @ gen_test <EOL> def test_to_list_argument_checking ( self ) : <EOL> yield self . make_test_data ( ) <EOL> coll = self . collection <EOL> cursor = coll . find ( ) <EOL> yield self . check_optional_callback ( cursor . to_list , <NUM_LIT:10> ) <EOL> cursor = coll . find ( ) <EOL> with self . assertRaises ( ValueError ) : <EOL> yield cursor . to_list ( - <NUM_LIT:1> ) <EOL> with self . assertRaises ( TypeError ) : <EOL> yield cursor . to_list ( '<STR_LIT:foo>' ) <EOL> @ gen_test <EOL> def test_to_list_callback ( self ) : <EOL> yield self . make_test_data ( ) <EOL> cursor = self . collection . find ( { } , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> cursor . sort ( [ ( '<STR_LIT>' , pymongo . ASCENDING ) ] ) <EOL> expected = [ { '<STR_LIT>' : i } for i in range ( <NUM_LIT:200> ) ] <EOL> ( result , error ) , _ = yield gen . Task ( cursor . to_list , length = <NUM_LIT:1000> ) <EOL> self . assertEqual ( expected , result ) <EOL> cursor = self . collection . find ( ) . where ( '<STR_LIT>' ) <EOL> ( result , error ) , _ = yield gen . Task ( cursor . to_list , length = <NUM_LIT:1000> ) <EOL> self . assertEqual ( None , result ) <EOL> self . assertTrue ( isinstance ( error , OperationFailure ) ) <EOL> @ gen_test <EOL> def test_to_list_with_length ( self ) : <EOL> yield self . make_test_data ( ) <EOL> coll = self . collection <EOL> cursor = coll . find ( ) . sort ( '<STR_LIT>' ) <EOL> self . assertEqual ( [ ] , ( yield cursor . to_list ( <NUM_LIT:0> ) ) ) <EOL> def expected ( start , stop ) : <EOL> return [ { '<STR_LIT>' : i } for i in range ( start , stop ) ] <EOL> self . assertEqual ( expected ( <NUM_LIT:0> , <NUM_LIT:10> ) , ( yield cursor . to_list ( <NUM_LIT:10> ) ) ) <EOL> self . assertEqual ( expected ( <NUM_LIT:10> , <NUM_LIT:100> ) , ( yield cursor . to_list ( <NUM_LIT> ) ) ) <EOL> self . assertEqual ( expected ( <NUM_LIT:100> , <NUM_LIT> ) , ( yield cursor . to_list ( <NUM_LIT:1> ) ) ) <EOL> self . assertEqual ( expected ( <NUM_LIT> , <NUM_LIT> ) , ( yield cursor . to_list ( <NUM_LIT:1> ) ) ) <EOL> self . assertEqual ( expected ( <NUM_LIT> , <NUM_LIT> ) , ( yield cursor . to_list ( <NUM_LIT:1> ) ) ) <EOL> self . assertEqual ( [ ] , ( yield cursor . to_list ( <NUM_LIT:0> ) ) ) <EOL> self . assertEqual ( expected ( <NUM_LIT> , <NUM_LIT> ) , ( yield cursor . to_list ( <NUM_LIT:2> ) ) ) <EOL> self . assertEqual ( expected ( <NUM_LIT> , <NUM_LIT:200> ) , ( yield cursor . to_list ( <NUM_LIT:100> ) ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , cursor . cursor_id ) <EOL> self . assertEqual ( [ ] , ( yield cursor . to_list ( <NUM_LIT:100> ) ) ) <EOL> @ gen_test <EOL> def test_to_list_with_length_of_none ( self ) : <EOL> yield self . make_test_data ( ) <EOL> collection = self . collection <EOL> cursor = collection . find ( ) <EOL> docs = yield cursor . to_list ( None ) <EOL> count = yield collection . count ( ) <EOL> self . assertEqual ( count , len ( docs ) ) <EOL> @ gen_test <EOL> def test_to_list_tailable ( self ) : <EOL> coll = self . collection <EOL> cursor = coll . find ( tailable = True ) <EOL> with self . assertRaises ( InvalidOperation ) : <EOL> yield cursor . to_list ( <NUM_LIT:10> ) <EOL> @ gen_test <EOL> def test_cursor_explicit_close ( self ) : <EOL> client , server = self . client_server ( auto_ismaster = { '<STR_LIT>' : True } ) <EOL> collection = client . test . collection <EOL> cursor = collection . find ( ) <EOL> fetch_next = cursor . fetch_next <EOL> request = yield self . run_thread ( server . receives , OpQuery ) <EOL> request . replies ( { '<STR_LIT>' : <NUM_LIT:1> } , cursor_id = <NUM_LIT> ) <EOL> self . assertTrue ( ( yield fetch_next ) ) <EOL> close_future = cursor . close ( ) <EOL> yield self . run_thread ( server . receives , OpKillCursors ( cursor_ids = [ <NUM_LIT> ] ) ) <EOL> yield close_future <EOL> self . assertTrue ( cursor . alive ) <EOL> self . assertEqual ( { '<STR_LIT>' : <NUM_LIT:1> } , cursor . next_object ( ) ) <EOL> self . assertFalse ( ( yield cursor . fetch_next ) ) <EOL> self . assertFalse ( cursor . alive ) <EOL> @ gen_test <EOL> def test_each_cancel ( self ) : <EOL> yield self . make_test_data ( ) <EOL> loop = self . io_loop <EOL> collection = self . collection <EOL> results = [ ] <EOL> future = Future ( ) <EOL> def cancel ( result , error ) : <EOL> if error : <EOL> future . set_exception ( error ) <EOL> else : <EOL> results . append ( result ) <EOL> loop . add_callback ( canceled ) <EOL> return False <EOL> def canceled ( ) : <EOL> try : <EOL> self . assertFalse ( cursor . delegate . _Cursor__killed ) <EOL> self . assertTrue ( cursor . alive ) <EOL> cursor . each ( each ) <EOL> except Exception as e : <EOL> future . set_exception ( e ) <EOL> def each ( result , error ) : <EOL> if error : <EOL> future . set_exception ( error ) <EOL> elif result : <EOL> pass <EOL> results . append ( result ) <EOL> else : <EOL> future . set_result ( None ) <EOL> cursor = collection . find ( ) <EOL> cursor . each ( cancel ) <EOL> yield future <EOL> self . assertEqual ( ( yield collection . count ( ) ) , len ( results ) ) <EOL> @ gen_test <EOL> def test_rewind ( self ) : <EOL> yield self . collection . insert ( [ { } , { } , { } ] ) <EOL> cursor = self . collection . find ( ) . limit ( <NUM_LIT:2> ) <EOL> count = <NUM_LIT:0> <EOL> while ( yield cursor . fetch_next ) : <EOL> cursor . next_object ( ) <EOL> count += <NUM_LIT:1> <EOL> self . assertEqual ( <NUM_LIT:2> , count ) <EOL> cursor . rewind ( ) <EOL> count = <NUM_LIT:0> <EOL> while ( yield cursor . fetch_next ) : <EOL> cursor . next_object ( ) <EOL> count += <NUM_LIT:1> <EOL> self . assertEqual ( <NUM_LIT:2> , count ) <EOL> cursor . rewind ( ) <EOL> count = <NUM_LIT:0> <EOL> while ( yield cursor . fetch_next ) : <EOL> cursor . next_object ( ) <EOL> break <EOL> cursor . rewind ( ) <EOL> while ( yield cursor . fetch_next ) : <EOL> cursor . next_object ( ) <EOL> count += <NUM_LIT:1> <EOL> self . assertEqual ( <NUM_LIT:2> , count ) <EOL> self . assertEqual ( cursor , cursor . rewind ( ) ) <EOL> @ gen_test <EOL> def test_del_on_main_greenlet ( self ) : <EOL> if tornado_version < ( <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> client , server = self . client_server ( auto_ismaster = True ) <EOL> cursor = client . test . collection . find ( ) <EOL> future = cursor . fetch_next <EOL> request = yield self . run_thread ( server . receives , OpQuery ) <EOL> request . replies ( { '<STR_LIT>' : <NUM_LIT:1> } , cursor_id = <NUM_LIT> ) <EOL> yield future <EOL> del cursor <EOL> yield self . pause ( <NUM_LIT:0> ) <EOL> if '<STR_LIT>' in sys . version : <EOL> gc . collect ( ) <EOL> yield self . run_thread ( server . receives , OpKillCursors ) <EOL> @ gen_test <EOL> def test_del_on_child_greenlet ( self ) : <EOL> if tornado_version < ( <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> client , server = self . client_server ( auto_ismaster = True ) <EOL> self . cursor = client . test . collection . find ( ) <EOL> future = self . cursor . fetch_next <EOL> request = yield self . run_thread ( server . receives , OpQuery ) <EOL> request . replies ( { '<STR_LIT>' : <NUM_LIT:1> } , cursor_id = <NUM_LIT> ) <EOL> yield future <EOL> def f ( ) : <EOL> del self . cursor <EOL> greenlet . greenlet ( f ) . switch ( ) <EOL> yield self . pause ( <NUM_LIT:0> ) <EOL> if '<STR_LIT>' in sys . version : <EOL> gc . collect ( ) <EOL> yield self . run_thread ( server . receives , OpKillCursors ) <EOL> @ gen_test <EOL> def test_exhaust ( self ) : <EOL> if ( yield server_is_mongos ( self . cx ) ) : <EOL> self . assertRaises ( InvalidOperation , <EOL> self . db . test . find , exhaust = True ) <EOL> return <EOL> self . assertRaises ( TypeError , self . db . test . find , exhaust = <NUM_LIT:5> ) <EOL> cur = self . db . test . find ( exhaust = True ) <EOL> self . assertRaises ( InvalidOperation , cur . limit , <NUM_LIT:5> ) <EOL> cur = self . db . test . find ( limit = <NUM_LIT:5> ) <EOL> self . assertRaises ( InvalidOperation , cur . add_option , <NUM_LIT:64> ) <EOL> cur = self . db . test . find ( ) <EOL> cur . add_option ( <NUM_LIT:64> ) <EOL> self . assertRaises ( InvalidOperation , cur . limit , <NUM_LIT:5> ) <EOL> yield self . db . drop_collection ( "<STR_LIT:test>" ) <EOL> yield self . db . test . insert ( [ { } for _ in range ( <NUM_LIT> ) ] ) <EOL> client = self . motor_client ( max_pool_size = <NUM_LIT:1> ) <EOL> yield client . db . collection . find_one ( ) <EOL> socks = client . _get_primary_pool ( ) . sockets <EOL> cur = client [ self . db . name ] . test . find ( exhaust = True ) <EOL> has_next = yield cur . fetch_next <EOL> self . assertTrue ( has_next ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( socks ) ) <EOL> while ( yield cur . fetch_next ) : <EOL> cur . next_object ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( socks ) ) <EOL> docs = yield client [ self . db . name ] . test . find ( exhaust = True ) . to_list ( None ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( socks ) ) <EOL> self . assertEqual ( <EOL> ( yield self . db . test . count ( ) ) , <EOL> len ( docs ) ) <EOL> sock = one ( socks ) <EOL> cur = client [ self . db . name ] . test . find ( exhaust = True ) . batch_size ( <NUM_LIT:1> ) <EOL> has_next = yield cur . fetch_next <EOL> self . assertTrue ( has_next ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( socks ) ) <EOL> if '<STR_LIT>' in sys . version : <EOL> cur . close ( ) <EOL> del cur <EOL> yield self . pause ( <NUM_LIT:0.1> ) <EOL> self . assertNotIn ( sock , socks ) <EOL> self . assertTrue ( sock . closed ) <EOL> def test_iter ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> for _ in self . db . test . find ( ) : <EOL> pass <EOL> @ gen_test <EOL> def test_close_with_docs_in_batch ( self ) : <EOL> yield self . make_test_data ( ) <EOL> cursor = self . collection . find ( ) <EOL> yield cursor . fetch_next <EOL> yield cursor . close ( ) <EOL> self . cx . disconnect ( ) <EOL> with warnings . catch_warnings ( record = True ) as w : <EOL> del cursor <EOL> self . assertEqual ( <NUM_LIT:0> , len ( w ) ) <EOL> class MotorCursorMaxTimeMSTest ( MotorTest ) : <EOL> def setUp ( self ) : <EOL> super ( MotorCursorMaxTimeMSTest , self ) . setUp ( ) <EOL> self . io_loop . run_sync ( self . maybe_skip ) <EOL> def tearDown ( self ) : <EOL> self . io_loop . run_sync ( self . disable_timeout ) <EOL> super ( MotorCursorMaxTimeMSTest , self ) . tearDown ( ) <EOL> @ gen . coroutine <EOL> def maybe_skip ( self ) : <EOL> if ( yield server_is_mongos ( self . cx ) ) : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> if not ( yield at_least ( self . cx , ( <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:3> , - <NUM_LIT:1> ) ) ) : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> cmdline = yield get_command_line ( self . cx ) <EOL> if '<STR_LIT:1>' != safe_get ( cmdline , '<STR_LIT>' ) : <EOL> if '<STR_LIT>' not in cmdline [ '<STR_LIT>' ] : <EOL> raise SkipTest ( "<STR_LIT>" ) <EOL> @ gen . coroutine <EOL> def enable_timeout ( self ) : <EOL> yield self . cx . admin . command ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> mode = "<STR_LIT>" ) <EOL> @ gen . coroutine <EOL> def disable_timeout ( self ) : <EOL> yield self . cx . admin . command ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> mode = "<STR_LIT>" ) <EOL> @ gen_test <EOL> def test_max_time_ms_query ( self ) : <EOL> yield self . enable_timeout ( ) <EOL> cursor = self . collection . find ( ) . max_time_ms ( <NUM_LIT> ) <EOL> with self . assertRaises ( ExecutionTimeout ) : <EOL> yield cursor . fetch_next <EOL> cursor = self . collection . find ( ) . max_time_ms ( <NUM_LIT> ) <EOL> with self . assertRaises ( ExecutionTimeout ) : <EOL> yield cursor . to_list ( <NUM_LIT:10> ) <EOL> with self . assertRaises ( ExecutionTimeout ) : <EOL> yield self . collection . find_one ( max_time_ms = <NUM_LIT> ) <EOL> @ gen_test ( timeout = <NUM_LIT> ) <EOL> def test_max_time_ms_getmore ( self ) : <EOL> yield self . collection . insert ( { } for _ in range ( <NUM_LIT:200> ) ) <EOL> try : <EOL> cursor = self . collection . find ( ) . max_time_ms ( <NUM_LIT> ) <EOL> yield cursor . fetch_next <EOL> cursor . next_object ( ) <EOL> yield self . enable_timeout ( ) <EOL> with self . assertRaises ( ExecutionTimeout ) : <EOL> while ( yield cursor . fetch_next ) : <EOL> cursor . next_object ( ) <EOL> yield cursor . close ( ) <EOL> yield self . disable_timeout ( ) <EOL> cursor = self . collection . find ( ) . max_time_ms ( <NUM_LIT> ) <EOL> yield cursor . fetch_next <EOL> cursor . next_object ( ) <EOL> yield self . enable_timeout ( ) <EOL> with self . assertRaises ( ExecutionTimeout ) : <EOL> yield cursor . to_list ( None ) <EOL> yield cursor . close ( ) <EOL> finally : <EOL> yield self . disable_timeout ( ) <EOL> yield self . collection . remove ( ) <EOL> @ gen_test <EOL> def test_max_time_ms_each_query ( self ) : <EOL> yield self . enable_timeout ( ) <EOL> cursor = self . collection . find ( ) . max_time_ms ( <NUM_LIT> ) <EOL> future = Future ( ) <EOL> def callback ( result , error ) : <EOL> if error : <EOL> future . set_exception ( error ) <EOL> elif not result : <EOL> future . set_result ( None ) <EOL> with self . assertRaises ( ExecutionTimeout ) : <EOL> cursor . each ( callback ) <EOL> yield future <EOL> @ gen_test ( timeout = <NUM_LIT:30> ) <EOL> def test_max_time_ms_each_getmore ( self ) : <EOL> yield self . collection . insert ( { } for _ in range ( <NUM_LIT:200> ) ) <EOL> try : <EOL> cursor = self . collection . find ( ) . max_time_ms ( <NUM_LIT> ) <EOL> yield cursor . fetch_next <EOL> cursor . next_object ( ) <EOL> future = Future ( ) <EOL> def callback ( result , error ) : <EOL> if error : <EOL> future . set_exception ( error ) <EOL> elif not result : <EOL> future . set_result ( None ) <EOL> yield self . enable_timeout ( ) <EOL> with self . assertRaises ( ExecutionTimeout ) : <EOL> cursor . each ( callback ) <EOL> yield future <EOL> yield cursor . close ( ) <EOL> finally : <EOL> yield self . disable_timeout ( ) <EOL> yield self . collection . remove ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
