<s> from mrjob . retry import RetryGoRound <EOL> from mrjob . retry import RetryWrapper <EOL> from tests . py2 import Mock <EOL> from tests . py2 import TestCase <EOL> class RetryGoRoundTestCase ( TestCase ) : <EOL> def test_empty ( self ) : <EOL> self . assertRaises ( <EOL> ValueError , RetryGoRound , [ ] , lambda ex : isinstance ( ex , IOError ) ) <EOL> def test_success ( self ) : <EOL> a1 = Mock ( ) <EOL> a1 . f = Mock ( __name__ = '<STR_LIT:f>' , return_value = <NUM_LIT:1> ) <EOL> a2 = Mock ( ) <EOL> a2 . f = Mock ( __name__ = '<STR_LIT:f>' , return_value = <NUM_LIT:2> ) <EOL> a = RetryGoRound ( [ a1 , a2 ] , lambda ex : isinstance ( ex , IOError ) ) <EOL> self . assertEqual ( a . f ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( a1 . f . call_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( a2 . f . call_count , <NUM_LIT:0> ) <EOL> def test_one_failure ( self ) : <EOL> a1 = Mock ( ) <EOL> a1 . f = Mock ( __name__ = '<STR_LIT:f>' , side_effect = IOError ) <EOL> a1 . x = <NUM_LIT:100> <EOL> a2 = Mock ( ) <EOL> a2 . f = Mock ( __name__ = '<STR_LIT:f>' , return_value = <NUM_LIT:2> ) <EOL> a2 . x = <NUM_LIT:200> <EOL> a = RetryGoRound ( [ a1 , a2 ] , lambda ex : isinstance ( ex , IOError ) ) <EOL> self . assertEqual ( a . x , <NUM_LIT:100> ) <EOL> self . assertEqual ( a . f ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( a . x , <NUM_LIT:200> ) <EOL> self . assertEqual ( a . f ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( a1 . f . call_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( a2 . f . call_count , <NUM_LIT:2> ) <EOL> def test_all_fail ( self ) : <EOL> a1 = Mock ( ) <EOL> a1 . f = Mock ( __name__ = '<STR_LIT:f>' , side_effect = IOError ) <EOL> a1 . x = <NUM_LIT:100> <EOL> a2 = Mock ( ) <EOL> a2 . f = Mock ( __name__ = '<STR_LIT:f>' , side_effect = IOError ) <EOL> a2 . x = <NUM_LIT:200> <EOL> a = RetryGoRound ( [ a1 , a2 ] , lambda ex : isinstance ( ex , IOError ) ) <EOL> self . assertEqual ( a . x , <NUM_LIT:100> ) <EOL> self . assertRaises ( IOError , a . f ) <EOL> self . assertEqual ( a . x , <NUM_LIT:100> ) <EOL> self . assertRaises ( IOError , a . f ) <EOL> self . assertEqual ( a1 . f . call_count , <NUM_LIT:2> ) <EOL> self . assertEqual ( a2 . f . call_count , <NUM_LIT:2> ) <EOL> def test_unrecoverable_error ( self ) : <EOL> a1 = Mock ( ) <EOL> a1 . f = Mock ( __name__ = '<STR_LIT:f>' , side_effect = ValueError ) <EOL> a2 = Mock ( ) <EOL> a2 . f = Mock ( __name__ = '<STR_LIT:f>' , return_value = <NUM_LIT:2> ) <EOL> a = RetryGoRound ( [ a1 , a2 ] , lambda ex : isinstance ( ex , IOError ) ) <EOL> self . assertRaises ( ValueError , a . f ) <EOL> self . assertRaises ( ValueError , a . f ) <EOL> self . assertEqual ( a1 . f . call_count , <NUM_LIT:2> ) <EOL> self . assertEqual ( a2 . f . call_count , <NUM_LIT:0> ) <EOL> def test_can_wrap_around ( self ) : <EOL> a1 = Mock ( ) <EOL> a1 . f = Mock ( __name__ = '<STR_LIT:f>' , side_effect = [ IOError , <NUM_LIT:1> ] ) <EOL> a2 = Mock ( ) <EOL> a2 . f = Mock ( __name__ = '<STR_LIT:f>' , side_effect = [ <NUM_LIT:2> , IOError ] ) <EOL> a = RetryGoRound ( [ a1 , a2 ] , lambda ex : isinstance ( ex , IOError ) ) <EOL> self . assertEqual ( a . f ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( a . f ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( a1 . f . call_count , <NUM_LIT:2> ) <EOL> self . assertEqual ( a2 . f . call_count , <NUM_LIT:2> ) <EOL> def test_wrapping ( self ) : <EOL> a1 = Mock ( ) <EOL> a1 . f = Mock ( __name__ = '<STR_LIT:f>' , side_effect = IOError ) <EOL> a2 = Mock ( ) <EOL> a2 . f = Mock ( __name__ = '<STR_LIT:f>' , return_value = <NUM_LIT:2> ) <EOL> a = RetryGoRound ( [ a1 , a2 ] , lambda ex : isinstance ( ex , IOError ) ) <EOL> self . assertEqual ( a . f ( '<STR_LIT:foo>' , bar = '<STR_LIT>' ) , <NUM_LIT:2> ) <EOL> a1 . f . assert_called_once_with ( '<STR_LIT:foo>' , bar = '<STR_LIT>' ) <EOL> a2 . f . assert_called_once_with ( '<STR_LIT:foo>' , bar = '<STR_LIT>' ) <EOL> self . assertEqual ( a . f . __name__ , '<STR_LIT:f>' ) <EOL> class RetryWrapperTestCase ( TestCase ) : <EOL> def test_success ( self ) : <EOL> a1 = Mock ( ) <EOL> a1 . f = Mock ( __name__ = '<STR_LIT:f>' , side_effect = None ) <EOL> a = RetryWrapper ( <EOL> a1 , <EOL> retry_if = lambda x : True , <EOL> backoff = <NUM_LIT> , <EOL> max_tries = <NUM_LIT:2> <EOL> ) <EOL> a . f ( ) <EOL> a1 . f . assert_called_once_with ( ) <EOL> def test_failure ( self ) : <EOL> a1 = Mock ( ) <EOL> a1 . f = Mock ( __name__ = '<STR_LIT:f>' , side_effect = [ IOError , <NUM_LIT:1> ] ) <EOL> a = RetryWrapper ( <EOL> a1 , <EOL> retry_if = lambda x : True , <EOL> backoff = <NUM_LIT> , <EOL> max_tries = <NUM_LIT:2> <EOL> ) <EOL> self . assertEqual ( a . f ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( a1 . f . call_count , <NUM_LIT:2> ) <EOL> def test_failure_raises_if_all_tries_fail ( self ) : <EOL> a1 = Mock ( ) <EOL> a1 . f = Mock ( __name__ = '<STR_LIT:f>' , side_effect = [ IOError , IOError ] ) <EOL> a = RetryWrapper ( <EOL> a1 , <EOL> retry_if = lambda x : True , <EOL> backoff = <NUM_LIT> , <EOL> max_tries = <NUM_LIT:2> <EOL> ) <EOL> with self . assertRaises ( IOError ) : <EOL> a . f ( ) <EOL> self . assertEqual ( a1 . f . call_count , <NUM_LIT:2> ) <EOL> def test_try_till_success ( self ) : <EOL> a1 = Mock ( ) <EOL> a1 . f = Mock ( __name__ = '<STR_LIT:f>' , side_effect = [ IOError , IOError , None ] ) <EOL> a = RetryWrapper ( <EOL> a1 , <EOL> retry_if = lambda x : True , <EOL> backoff = <NUM_LIT> , <EOL> max_tries = <NUM_LIT:0> <EOL> ) <EOL> a . f ( ) <EOL> self . assertEqual ( a1 . f . call_count , <NUM_LIT:3> ) </s>
