<s> from __future__ import absolute_import <EOL> from mock import ANY , patch <EOL> from cell . actors import Actor <EOL> from cell . agents import dAgent <EOL> from cell . exceptions import CellError , NoReplyError <EOL> from cell . results import AsyncResult <EOL> from cell . tests . utils import Case , Mock , with_in_memory_connection <EOL> from kombu . utils import uuid <EOL> __author__ = '<STR_LIT>' <EOL> class A ( Actor ) : <EOL> pass <EOL> class Ag ( dAgent ) : <EOL> def get_default_scatter_limit ( self ) : <EOL> return <NUM_LIT:5> <EOL> class test_AsyncResuls ( Case ) : <EOL> def get_async_result ( self ) : <EOL> ticket = uuid ( ) <EOL> actor = Mock ( ) <EOL> ares = AsyncResult ( ticket , actor ) <EOL> return ares <EOL> def test_init ( self ) : <EOL> ticket = uuid ( ) <EOL> actor = Mock ( ) <EOL> ares = AsyncResult ( ticket , actor ) <EOL> self . assertEquals ( ares . ticket , ticket ) <EOL> self . assertEqual ( ares . actor , actor ) <EOL> self . assertIsNone ( ares . _result ) <EOL> self . assertEqual ( ares . Error , CellError ) <EOL> self . assertEqual ( ares . NoReplyError , NoReplyError ) <EOL> with self . assertRaises ( TypeError ) : <EOL> AsyncResult ( ticket ) <EOL> def test_result_when_result_is_set ( self ) : <EOL> val = '<STR_LIT>' <EOL> ares = self . get_async_result ( ) <EOL> ares . get = Mock ( ) <EOL> ares . _result = val <EOL> res = ares . result ( ) <EOL> self . assertEqual ( res , val ) <EOL> self . assertEqual ( ares . get . call_count , <NUM_LIT:0> ) <EOL> def test_result_when_result_is_not_set ( self ) : <EOL> val = '<STR_LIT>' <EOL> ares = self . get_async_result ( ) <EOL> ares . get = Mock ( return_value = val ) <EOL> res = ares . result ( ) <EOL> self . assertEqual ( res , val ) <EOL> self . assertEqual ( ares . _result , res ) <EOL> ares . get . assert_called_once_with ( ) <EOL> def test_to_python ( self ) : <EOL> ok_message = { '<STR_LIT>' : '<STR_LIT>' } <EOL> ares = self . get_async_result ( ) <EOL> res = ares . to_python ( ok_message ) <EOL> self . assertEqual ( res , ok_message [ '<STR_LIT>' ] ) <EOL> ok_message = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:foo>' : '<STR_LIT>' } <EOL> res = ares . to_python ( ok_message ) <EOL> self . assertEqual ( res , ok_message [ '<STR_LIT>' ] ) <EOL> ok_message = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> res = ares . to_python ( ok_message ) <EOL> self . assertEqual ( res , ok_message [ '<STR_LIT>' ] ) <EOL> error_message = { '<STR_LIT>' : [ Exception ( '<STR_LIT>' ) ] } <EOL> with self . assertRaises ( ares . Error ) : <EOL> ares . to_python ( error_message ) <EOL> with self . assertRaises ( ares . Error ) : <EOL> ares . to_python ( error_message , propagate = True ) <EOL> error_message = { '<STR_LIT>' : [ '<STR_LIT>' , None ] } <EOL> res = ares . to_python ( error_message , propagate = False ) <EOL> self . assertEquals ( res . __dict__ , <EOL> ares . Error ( * error_message . get ( '<STR_LIT>' ) ) . __dict__ ) <EOL> error_message = { '<STR_LIT:foo>' : [ '<STR_LIT>' ] } <EOL> with self . assertRaises ( ares . Error ) : <EOL> ares . to_python ( error_message ) <EOL> error_message = { '<STR_LIT:foo>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } <EOL> res = ares . to_python ( error_message , propagate = False ) <EOL> self . assertEqual ( res . __dict__ , <EOL> ares . Error ( * error_message [ '<STR_LIT>' ] ) . __dict__ ) <EOL> def test_get ( self ) : <EOL> id1 , id2 = uuid ( ) , uuid ( ) <EOL> def gather ( ) : <EOL> yield id1 <EOL> yield id2 <EOL> ares = self . get_async_result ( ) <EOL> ares . gather = Mock ( return_value = [ '<STR_LIT:1>' ] ) <EOL> ares . get ( ) <EOL> ares . gather . assert_called_once_with ( limit = <NUM_LIT:1> ) <EOL> ares . gather . reset_mock ( ) <EOL> kwargs = { '<STR_LIT>' : <NUM_LIT:100> , '<STR_LIT>' : False , <EOL> '<STR_LIT:foo>' : '<STR_LIT:bar>' , '<STR_LIT>' : True } <EOL> ares . get ( ** kwargs ) <EOL> ares . gather . assert_called_once_with ( ** dict ( kwargs , limit = <NUM_LIT:1> ) ) <EOL> ares . gather . reset_mock ( ) <EOL> kwargs = { '<STR_LIT>' : <NUM_LIT:100> , '<STR_LIT>' : False , '<STR_LIT>' : <NUM_LIT:10> } <EOL> ares . get ( ** kwargs ) <EOL> ares . gather . assert_called_once_with ( ** kwargs ) <EOL> ares . gather . reset_mock ( ) <EOL> ares . gather = Mock ( return_value = gather ( ) ) <EOL> res = ares . get ( ) <EOL> self . assertEqual ( res , id1 ) <EOL> ares . gather = Mock ( return_value = None ) <EOL> with self . assertRaises ( ares . NoReplyError ) : <EOL> ares . get ( ) <EOL> ares . gather . reset_mock ( ) <EOL> ares . gather = Mock ( return_value = { } ) <EOL> with self . assertRaises ( ares . NoReplyError ) : <EOL> ares . get ( ) <EOL> @ with_in_memory_connection <EOL> def test_gather ( self , conn ) : <EOL> def collect_replies ( ) : <EOL> yield <NUM_LIT:1> <EOL> yield <NUM_LIT:2> <EOL> yield <NUM_LIT:3> <EOL> ticket = uuid ( ) <EOL> actor = Actor ( conn ) <EOL> actor . _collect_replies = Mock ( return_value = collect_replies ( ) ) <EOL> ares = AsyncResult ( ticket , actor ) <EOL> ares . to_python = Mock ( ) <EOL> all = ares . gather ( ) <EOL> list ( all ) <EOL> actor . _collect_replies . assert_caleld_once_with ( conn , ANY , ticket ) <EOL> self . assertEqual ( ares . to_python . call_count , <EOL> len ( list ( collect_replies ( ) ) ) ) <EOL> actor . _collect_replies . reset_mock ( ) <EOL> actor . _collect_replies = Mock ( return_value = collect_replies ( ) ) <EOL> prev_to_python = ares . to_python <EOL> new_to_python = lambda x , propagate = True : '<STR_LIT>' % x <EOL> ares . to_python = new_to_python <EOL> all = ares . gather ( ) <EOL> vals = list ( all ) <EOL> expected_vals = [ new_to_python ( i ) for i in collect_replies ( ) ] <EOL> actor . _collect_replies . assert_caleld_once_with ( conn , ANY , ticket ) <EOL> self . assertEqual ( vals , expected_vals ) <EOL> ares . to_python = prev_to_python <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ with_in_memory_connection <EOL> def test_gather_kwargs ( self , conn , collect ) : <EOL> actor = Actor ( conn ) <EOL> ares = AsyncResult ( uuid ( ) , actor ) <EOL> prev_to_python = ares . to_python <EOL> new_to_python = lambda x , propagate = True : x <EOL> ares . to_python = new_to_python <EOL> self . assert_gather_kwargs ( <EOL> ares , collect , { } , <EOL> timeout = actor . default_timeout , ignore_timeout = False ) <EOL> actor . agent = dAgent ( conn ) <EOL> self . assert_gather_kwargs ( <EOL> ares , collect , { } , <EOL> timeout = actor . default_timeout , limit = None , ignore_timeout = False ) <EOL> ag = Ag ( conn ) <EOL> actor . agent = ag <EOL> self . assert_gather_kwargs ( <EOL> ares , collect , { } , timeout = actor . default_timeout , <EOL> limit = ag . get_default_scatter_limit ( ) ) <EOL> actor . agent = Ag ( conn ) <EOL> timeout , ignore_timeout , limit = <NUM_LIT> , False , uuid ( ) <EOL> self . assert_gather_kwargs ( <EOL> ares , collect , <EOL> { '<STR_LIT>' : timeout , '<STR_LIT>' : ignore_timeout , <EOL> '<STR_LIT>' : limit } , <EOL> timeout = timeout , limit = limit , ignore_timeout = ignore_timeout ) <EOL> actor . agent = None <EOL> timeout , ignore_timeout = <NUM_LIT> , True <EOL> self . assert_gather_kwargs ( <EOL> ares , collect , <EOL> { '<STR_LIT>' : timeout , '<STR_LIT>' : ignore_timeout } , <EOL> timeout = timeout , ignore_timeout = ignore_timeout ) <EOL> ares . to_python = prev_to_python <EOL> def assert_gather_kwargs ( self , ares , collect , args , ** kwargs ) : <EOL> def drain ( ) : <EOL> yield <NUM_LIT:1> <EOL> yield <NUM_LIT:2> <EOL> collect . return_value = drain ( ) <EOL> all = ares . gather ( ** args ) <EOL> self . assertEqual ( list ( all ) , list ( drain ( ) ) ) <EOL> collect . assert_called_once_with ( ANY , ANY , ANY , ** kwargs ) <EOL> collect . reset_mock ( ) </s>
