<s> import unittest <EOL> from uuid import uuid4 <EOL> from time import time , sleep <EOL> from toto . tasks import TaskQueue , AwaitableInstance , InstancePool <EOL> from tornado . ioloop import IOLoop <EOL> from tornado . gen import coroutine <EOL> class _Instance ( object ) : <EOL> def __init__ ( self ) : <EOL> self . counter = <NUM_LIT:0> <EOL> def increment ( self ) : <EOL> self . counter += <NUM_LIT:1> <EOL> return self . counter <EOL> def value ( self ) : <EOL> return self . counter <EOL> class TestTasks ( unittest . TestCase ) : <EOL> def test_add_task ( self ) : <EOL> queue = TaskQueue ( ) <EOL> self . assertEquals ( len ( queue ) , <NUM_LIT:0> ) <EOL> task_results = [ ] <EOL> task = lambda x : task_results . append ( x ) <EOL> queue . add_task ( task , <NUM_LIT:1> ) <EOL> queue . add_task ( task , <NUM_LIT:2> ) <EOL> queue . add_task ( task , <NUM_LIT:3> ) <EOL> start = time ( ) <EOL> while <NUM_LIT:1> : <EOL> if len ( task_results ) == <NUM_LIT:3> : <EOL> break <EOL> if time ( ) - start > <NUM_LIT:5> : <EOL> break <EOL> sleep ( <NUM_LIT> ) <EOL> self . assertEquals ( len ( task_results ) , <NUM_LIT:3> ) <EOL> self . assertEquals ( task_results , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_yield_task ( self ) : <EOL> queue = TaskQueue ( ) <EOL> task_results = [ ] <EOL> @ coroutine <EOL> def yield_tasks ( ) : <EOL> task = lambda x : x <EOL> futures = [ ] <EOL> futures . append ( queue . yield_task ( task , <NUM_LIT:1> ) ) <EOL> futures . append ( queue . yield_task ( task , <NUM_LIT:2> ) ) <EOL> futures . append ( queue . yield_task ( task , <NUM_LIT:3> ) ) <EOL> res = yield futures <EOL> task_results [ : ] = res <EOL> loop = IOLoop ( ) <EOL> loop . make_current ( ) <EOL> loop . run_sync ( yield_tasks ) <EOL> self . assertEquals ( len ( task_results ) , <NUM_LIT:3> ) <EOL> self . assertEquals ( task_results , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_add_task_exception ( self ) : <EOL> queue = TaskQueue ( ) <EOL> self . assertEquals ( len ( queue ) , <NUM_LIT:0> ) <EOL> task_results = [ ] <EOL> def task ( x ) : <EOL> task_results . append ( x ) <EOL> raise Exception ( '<STR_LIT>' ) <EOL> queue . add_task ( task , <NUM_LIT:1> ) <EOL> queue . add_task ( task , <NUM_LIT:2> ) <EOL> queue . add_task ( task , <NUM_LIT:3> ) <EOL> start = time ( ) <EOL> while <NUM_LIT:1> : <EOL> if len ( task_results ) == <NUM_LIT:3> : <EOL> break <EOL> if time ( ) - start > <NUM_LIT:5> : <EOL> break <EOL> sleep ( <NUM_LIT> ) <EOL> self . assertEquals ( len ( task_results ) , <NUM_LIT:3> ) <EOL> self . assertEquals ( task_results , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_yield_task_exception ( self ) : <EOL> queue = TaskQueue ( ) <EOL> task_results = [ ] <EOL> @ coroutine <EOL> def yield_tasks ( ) : <EOL> def task ( x ) : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> futures = [ ] <EOL> futures . append ( queue . yield_task ( task , <NUM_LIT:1> ) ) <EOL> futures . append ( queue . yield_task ( task , <NUM_LIT:2> ) ) <EOL> futures . append ( queue . yield_task ( task , <NUM_LIT:3> ) ) <EOL> for f in futures : <EOL> try : <EOL> yield f <EOL> except Exception as e : <EOL> task_results . append ( e ) <EOL> loop = IOLoop ( ) <EOL> loop . make_current ( ) <EOL> loop . run_sync ( yield_tasks ) <EOL> self . assertEquals ( len ( task_results ) , <NUM_LIT:3> ) <EOL> for e in task_results : <EOL> self . assertEquals ( e . message , '<STR_LIT>' ) <EOL> def test_awaitable ( self ) : <EOL> instance = _Instance ( ) <EOL> instance . increment ( ) <EOL> self . assertEquals ( instance . value ( ) , <NUM_LIT:1> ) <EOL> awaitable = AwaitableInstance ( instance ) <EOL> @ coroutine <EOL> def yield_tasks ( ) : <EOL> self . assertEquals ( ( yield awaitable . increment ( ) ) , <NUM_LIT:2> ) <EOL> self . assertEquals ( ( yield awaitable . increment ( ) ) , <NUM_LIT:3> ) <EOL> self . assertEquals ( ( yield awaitable . increment ( ) ) , <NUM_LIT:4> ) <EOL> self . assertEquals ( ( yield awaitable . value ( ) ) , <NUM_LIT:4> ) <EOL> loop = IOLoop ( ) <EOL> loop . make_current ( ) <EOL> loop . run_sync ( yield_tasks ) <EOL> self . assertEquals ( instance . value ( ) , <NUM_LIT:4> ) <EOL> def test_instance_pool ( self ) : <EOL> instance1 = _Instance ( ) <EOL> instance2 = _Instance ( ) <EOL> pool = InstancePool ( [ instance1 , instance2 ] ) <EOL> pool . increment ( ) <EOL> pool . increment ( ) <EOL> self . assertEquals ( instance1 . value ( ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( instance2 . value ( ) , <NUM_LIT:1> ) <EOL> pool . transaction ( lambda i : i . increment ( ) ) <EOL> pool . transaction ( lambda i : i . increment ( ) ) <EOL> self . assertEquals ( instance1 . value ( ) , <NUM_LIT:2> ) <EOL> self . assertEquals ( instance2 . value ( ) , <NUM_LIT:2> ) <EOL> @ coroutine <EOL> def yield_tasks ( ) : <EOL> self . assertEquals ( ( yield pool . await ( ) . increment ( ) ) , <NUM_LIT:3> ) <EOL> self . assertEquals ( ( yield pool . await ( ) . increment ( ) ) , <NUM_LIT:3> ) <EOL> self . assertEquals ( instance1 . value ( ) , <NUM_LIT:3> ) <EOL> self . assertEquals ( instance2 . value ( ) , <NUM_LIT:3> ) <EOL> self . assertEquals ( ( yield pool . await_transaction ( lambda i : i . increment ( ) ) ) , <NUM_LIT:4> ) <EOL> self . assertEquals ( ( yield pool . await_transaction ( lambda i : i . increment ( ) ) ) , <NUM_LIT:4> ) <EOL> loop = IOLoop ( ) <EOL> loop . make_current ( ) <EOL> loop . run_sync ( yield_tasks ) <EOL> self . assertEquals ( instance1 . value ( ) , <NUM_LIT:4> ) <EOL> self . assertEquals ( instance2 . value ( ) , <NUM_LIT:4> ) </s>
