diff --git a/pydrive2/test/test_fs.py b/pydrive2/test/test_fs.py index 1e853a3a..7a9c281e 100644 --- a/pydrive2/test/test_fs.py +++ b/pydrive2/test/test_fs.py @@ -1,11 +1,11 @@ import errno +import json import os import posixpath import secrets import uuid from concurrent import futures from io import StringIO -from itertools import count from urllib.parse import parse_qsl, urlsplit import fsspec @@ -542,19 +542,32 @@ def test_get_file_callback(fs, tmpdir, remote_dir): assert callback.value == 40 +def carried_out(response, content): + if 200 <= response.status < 300: + return True + if response.status != 409: + return False + error = json.loads(content)["error"]["errors"][0] + return error["reason"] == "fileIdInUse" + + @pytest.fixture def lose_answers(service_auth, monkeypatch): """Send requests of this thread to Drive and lose their answers. Drive has done what a request asked by then, as when a connection drops while the answer is on its way. One answer is lost unless - `times` says more, after `skip` matching requests. + `times` says more. + + Only answers to requests Drive carried out are lost and counted: a + success, or the 409 `fileIdInUse` that says an earlier try did the + work. Any other answer, such as a rate limit under load or a 308 that + asks for more of an upload, reaches the filesystem as it is. """ - def install(lost_method, path_end, skip=0, times=1): + def install(lost_method, path_end, times=1): http = service_auth.Get_Http_Object() send = http.request - matches = count() lost = [] def request(uri, method="GET", **kwargs): @@ -562,7 +575,8 @@ def request(uri, method="GET", **kwargs): if ( method == lost_method and urlsplit(uri).path.endswith(path_end) - and skip <= next(matches) < skip + times + and carried_out(*answer) + and len(lost) < times ): lost.append(uri) raise ConnectionResetError("the answer was lost") @@ -729,8 +743,7 @@ def test_cp_file_of_a_shortcut_after_the_answer_was_lost_leaves_one_copy( fs, fs_factory, remote_dir, workspace_files, lose_answers ): fs.makedirs(remote_dir) - # Drive refuses the first copy, which carries an ID meant for files. - lost = lose_answers("POST", "/copy", skip=1) + lost = lose_answers("POST", "/copy") fs.cp_file(f"root/{workspace_files}-link", remote_dir + "/copy") assert len(lost) == 1 assert items_below(fs_factory()[0], remote_dir) == ["copy"]