okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ffb3d9b31
main
1"""The first range probe of a parallel download is tried again after a hiccup, and not after "no internet" (lane APP-LIN 0.2.14, from lane 8): 2one failed probe used to make a 50-100 GB download a single stream.""" 3from __future__import annotations 4 5import errno 6import http .client 7import socket 8import sys 9import unittest 10import urllib .error 11from pathlib import Path 12 13ROOT = Path (__file__ ).resolve ().parents [1 ] 14sys .path .insert (0 ,str (ROOT )) 15import setup # noqa: E402 16 17 18class FakeSource : 19def __init__ (self ,* outcomes ): 20self .outcomes ,self .calls = list (outcomes ),0 21 22def resolve (self ): 23self .calls += 1 24out = self .outcomes .pop (0 )if self .outcomes else None 25if isinstance (out ,BaseException ): 26raise out 27return "https://cdn/x" 28 29 30def probe (src ): 31waits = [] 32return setup ._probe_ranges (src ,waits = (1.0 ,3.0 ),sleep = waits .append ),waits 33 34 35class ProbeRetry (unittest .TestCase ): 36def test_a_hiccup_is_tried_again_and_the_parallel_download_goes_on (self ): 37for hiccup in (ConnectionResetError (),TimeoutError ("timed out" ),socket .timeout ("timed out" ),http .client .RemoteDisconnected ("closed" ), 38urllib .error .URLError (ConnectionResetError ()),urllib .error .HTTPError ("u" ,503 ,"busy" , {},None )): 39src = FakeSource (hiccup ) 40ok ,waits = probe (src ) 41self .assertTrue (ok ,repr (hiccup )) 42self .assertEqual ((src .calls ,waits ), (2 , [1.0 ])) 43 44def test_three_tries_at_most_then_one_stream (self ): 45src = FakeSource (ConnectionResetError (),ConnectionResetError (),ConnectionResetError (),None ) 46ok ,waits = probe (src ) 47self .assertFalse (ok ) 48self .assertEqual ((src .calls ,waits ), (3 , [1.0 ,3.0 ])) 49 50def test_no_internet_fails_at_once_without_waiting (self ): 51for offline in (urllib .error .URLError (socket .gaierror (- 3 ,"Temporary failure in name resolution" )),urllib .error .URLError (ConnectionRefusedError ()), 52urllib .error .URLError (OSError (errno .ENETUNREACH ,"Network is unreachable" )),OSError (errno .EHOSTUNREACH ,"No route to host" ), 53urllib .error .URLError ("unknown url type" ),urllib .error .HTTPError ("u" ,404 ,"missing" , {},None )): 54src = FakeSource (offline ) 55ok ,waits = probe (src ) 56self .assertFalse (ok ,repr (offline )) 57self .assertEqual ((src .calls ,waits ), (1 , []),repr (offline )) 58 59def test_a_server_without_ranges_is_not_asked_again (self ): 60src = FakeSource (setup ._NoRange ("HTTP 200" )) 61ok ,waits = probe (src ) 62self .assertEqual ((ok ,src .calls ,waits ), (False ,1 , [])) 63 64def test_parallel_download_uses_the_probe (self ): 65text = (ROOT / "setup.py" ).read_text (encoding = "utf-8" ) 66at = text .index ("def parallel_download(" ) 67self .assertIn ("if not _probe_ranges(src):" ,text [at :at + 800 ]) 68 69 70if __name__ == "__main__" : 71unittest .main ()