#NEWLINE# @ responses . activate #NEWLINE# def test_retrier_catches_badstatusline_and_429( self ) : #NEWLINE# #INDENT# client = HubstorageClient ( auth = self . auth , endpoint = self . endpoint , max_retries = 3 , max_retry_time = 1 ) #NEWLINE# job_metadata = { #NEWLINE# #INDENT# 'project' : self . projectid , #NEWLINE# 'spider' : self . spidername , #NEWLINE# 'state' : 'pending' , #NEWLINE# #UNINDENT# } #NEWLINE# attempts_count = [ 0 ] #NEWLINE# def request_callback( request ) : #NEWLINE# #INDENT# attempts_count [ 0 ] += 1 #NEWLINE# if ( attempts_count [ 0 ] <= 2 ) : #NEWLINE# #INDENT# raise ConnectionError ( 'Connection aborted.' , BadStatusLine ( "''" ) ) #NEWLINE# #UNINDENT# if ( attempts_count [ 0 ] == 3 ) : #NEWLINE# #INDENT# return ( 429 , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# resp_body = dict ( job_metadata ) #NEWLINE# return ( 200 , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } , json . dumps ( resp_body ) ) #NEWLINE# #UNINDENT# #UNINDENT# self . mock_api ( callback = request_callback ) #NEWLINE# job = client . get_job ( ( '%s/%s/%s' % ( self . projectid , self . spiderid , 42 ) ) ) #NEWLINE# self . assertEqual ( dict ( self ) , dict ( job . metadata ) ) #NEWLINE# self . assertEqual ( attempts_count [ 0 ] , 4 )