okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
061a28583
main
1"""A feed read that hangs once is tried once more: about one request in 25 to github.com hung for the whole 30 s while the next answered in 0.4 s, 2and the app's first-run card said "offline" a moment before the install's own read worked.""" 3import io 4import unittest 5import urllib .error 6from unittest import mock 7 8from strata_control import transfer 9 10 11class Answer (io .BytesIO ): 12headers = {} 13 14def __enter__ (self ): 15return self 16 17def __exit__ (self ,* a ): 18return False 19 20 21class ReadSmallRetry (unittest .TestCase ): 22def read (self ,side_effect ): 23with mock .patch .object (transfer ,"check_url_policy" ,lambda url :None ),mock .patch .object (transfer ._opener ,"open" ,side_effect = side_effect )as op : 24try : 25return transfer .read_small ("https://updates.example.org/feed.json" ,timeout = 30 ),op 26except transfer .TransferError as exc : 27return exc ,op 28 29def test_one_timeout_is_forgiven_and_the_first_wait_is_short (self ): 30got ,op = self .read ([urllib .error .URLError (TimeoutError ("timed out" )),Answer (b"{}" )]) 31self .assertEqual (got ,b"{}" ) 32self .assertEqual ([c .kwargs ["timeout" ]for c in op .call_args_list ], [transfer .FIRST_TRY_SECONDS ,30 ]) 33 34def test_two_timeouts_still_say_so (self ): 35got ,op = self .read ([urllib .error .URLError (TimeoutError ("timed out" ))]* 2 ) 36self .assertIsInstance (got ,transfer .TransferError ) 37self .assertEqual (op .call_count ,2 ) 38 39def test_an_answer_of_404_is_not_tried_again (self ): 40err = urllib .error .HTTPError ("https://x" ,404 ,"no" , {},None ) 41got ,op = self .read ([err ]) 42self .assertIsInstance (got ,transfer .TransferError ) 43self .assertEqual (op .call_count ,1 ) 44 45def test_a_good_first_answer_is_one_request (self ): 46got ,op = self .read ([Answer (b"{}" )]) 47self .assertEqual ((got ,op .call_count ), (b"{}" ,1 )) 48 49 50if __name__ == "__main__" : 51unittest .main ()