<s> """<STR_LIT>""" <EOL> import unittest <EOL> from google . cloud . dataflow . utils import retry <EOL> from apitools . base . py . exceptions import HttpError <EOL> class FakeClock ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . calls = [ ] <EOL> def sleep ( self , value ) : <EOL> self . calls . append ( value ) <EOL> class FakeLogger ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . calls = [ ] <EOL> def log ( self , message , interval , func_name , exn_name , exn_traceback ) : <EOL> _ = interval , exn_traceback <EOL> self . calls . append ( ( message , func_name , exn_name ) ) <EOL> @ retry . with_exponential_backoff ( clock = FakeClock ( ) ) <EOL> def test_function ( a , b ) : <EOL> _ = a , b <EOL> raise NotImplementedError <EOL> @ retry . with_exponential_backoff ( initial_delay_secs = <NUM_LIT:1.0> , num_retries = <NUM_LIT:1> ) <EOL> def test_function_with_real_clock ( a , b ) : <EOL> _ = a , b <EOL> raise NotImplementedError <EOL> @ retry . no_retries <EOL> def test_no_retry_function ( a , b ) : <EOL> _ = a , b <EOL> raise NotImplementedError <EOL> class RetryTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . clock = FakeClock ( ) <EOL> self . logger = FakeLogger ( ) <EOL> self . calls = <NUM_LIT:0> <EOL> def permanent_failure ( self , a , b ) : <EOL> raise NotImplementedError <EOL> def transient_failure ( self , a , b ) : <EOL> self . calls += <NUM_LIT:1> <EOL> if self . calls > <NUM_LIT:8> : <EOL> return a + b <EOL> raise NotImplementedError <EOL> def http_error ( self , code ) : <EOL> raise HttpError ( { '<STR_LIT:status>' : str ( code ) } , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_with_explicit_decorator ( self ) : <EOL> self . assertRaises ( NotImplementedError , test_function , <NUM_LIT:10> , b = <NUM_LIT:20> ) <EOL> def test_with_no_retry_decorator ( self ) : <EOL> self . assertRaises ( NotImplementedError , test_no_retry_function , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> def test_with_real_clock ( self ) : <EOL> self . assertRaises ( NotImplementedError , <EOL> test_function_with_real_clock , <NUM_LIT:10> , b = <NUM_LIT:20> ) <EOL> def test_with_default_number_of_retries ( self ) : <EOL> self . assertRaises ( NotImplementedError , <EOL> retry . with_exponential_backoff ( clock = self . clock ) ( <EOL> self . permanent_failure ) , <EOL> <NUM_LIT:10> , b = <NUM_LIT:20> ) <EOL> self . assertEqual ( len ( self . clock . calls ) , <NUM_LIT:16> ) <EOL> def test_with_explicit_number_of_retries ( self ) : <EOL> self . assertRaises ( NotImplementedError , <EOL> retry . with_exponential_backoff ( <EOL> clock = self . clock , num_retries = <NUM_LIT:10> ) ( <EOL> self . permanent_failure ) , <EOL> <NUM_LIT:10> , b = <NUM_LIT:20> ) <EOL> self . assertEqual ( len ( self . clock . calls ) , <NUM_LIT:10> ) <EOL> def test_with_http_error_that_should_not_be_retried ( self ) : <EOL> self . assertRaises ( HttpError , <EOL> retry . with_exponential_backoff ( <EOL> clock = self . clock , num_retries = <NUM_LIT:10> ) ( <EOL> self . http_error ) , <EOL> <NUM_LIT> ) <EOL> self . assertEqual ( len ( self . clock . calls ) , <NUM_LIT:0> ) <EOL> def test_with_http_error_that_should_be_retried ( self ) : <EOL> self . assertRaises ( HttpError , <EOL> retry . with_exponential_backoff ( <EOL> clock = self . clock , num_retries = <NUM_LIT:10> ) ( <EOL> self . http_error ) , <EOL> <NUM_LIT> ) <EOL> self . assertEqual ( len ( self . clock . calls ) , <NUM_LIT:10> ) <EOL> def test_with_explicit_initial_delay ( self ) : <EOL> self . assertRaises ( NotImplementedError , <EOL> retry . with_exponential_backoff ( <EOL> initial_delay_secs = <NUM_LIT> , clock = self . clock , <EOL> fuzz = False ) ( <EOL> self . permanent_failure ) , <EOL> <NUM_LIT:10> , b = <NUM_LIT:20> ) <EOL> self . assertEqual ( len ( self . clock . calls ) , <NUM_LIT:16> ) <EOL> self . assertEqual ( self . clock . calls [ <NUM_LIT:0> ] , <NUM_LIT> ) <EOL> def test_log_calls_for_permanent_failure ( self ) : <EOL> self . assertRaises ( NotImplementedError , <EOL> retry . with_exponential_backoff ( <EOL> clock = self . clock , logger = self . logger . log ) ( <EOL> self . permanent_failure ) , <EOL> <NUM_LIT:10> , b = <NUM_LIT:20> ) <EOL> self . assertEqual ( len ( self . logger . calls ) , <NUM_LIT:16> ) <EOL> for message , func_name , exn_name in self . logger . calls : <EOL> self . assertTrue ( message . startswith ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( exn_name , '<STR_LIT>' ) <EOL> self . assertEqual ( func_name , '<STR_LIT>' ) <EOL> def test_log_calls_for_transient_failure ( self ) : <EOL> result = retry . with_exponential_backoff ( <EOL> clock = self . clock , logger = self . logger . log , fuzz = False ) ( <EOL> self . transient_failure ) ( <NUM_LIT:10> , b = <NUM_LIT:20> ) <EOL> self . assertEqual ( result , <NUM_LIT:30> ) <EOL> self . assertEqual ( len ( self . clock . calls ) , <NUM_LIT:8> ) <EOL> self . assertEqual ( self . clock . calls , <EOL> [ <NUM_LIT> * <NUM_LIT:1> , <NUM_LIT> * <NUM_LIT:2> , <NUM_LIT> * <NUM_LIT:4> , <NUM_LIT> * <NUM_LIT:8> , <EOL> <NUM_LIT> * <NUM_LIT:16> , <NUM_LIT> * <NUM_LIT:32> , <NUM_LIT> * <NUM_LIT:64> , <NUM_LIT> * <NUM_LIT> ] ) <EOL> self . assertEqual ( len ( self . logger . calls ) , <NUM_LIT:8> ) <EOL> for message , func_name , exn_name in self . logger . calls : <EOL> self . assertTrue ( message . startswith ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( exn_name , '<STR_LIT>' ) <EOL> self . assertEqual ( func_name , '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
