okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0243-gui into f-0243-rel (Chat.tsx and DropZone.tsx: the guifix clipboard helpers kept at the conflicts, copyAndTell stays for Markdown and Serving)061a28583

main
1.9 KiB51 linesraw
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):
12    headers = {}
13
14    def __enter__(self):
15        return self
16
17    def __exit__(self, *a):
18        return False
19
20
21class ReadSmallRetry(unittest.TestCase):
22    def read(self, side_effect):
23        with mock.patch.object(transfer, "check_url_policy", lambda url: None), mock.patch.object(transfer._opener, "open", side_effect=side_effect) as op:
24            try:
25                return transfer.read_small("https://updates.example.org/feed.json", timeout=30), op
26            except transfer.TransferError as exc:
27                return exc, op
28
29    def test_one_timeout_is_forgiven_and_the_first_wait_is_short(self):
30        got, op = self.read([urllib.error.URLError(TimeoutError("timed out")), Answer(b"{}")])
31        self.assertEqual(got, b"{}")
32        self.assertEqual([c.kwargs["timeout"] for c in op.call_args_list], [transfer.FIRST_TRY_SECONDS, 30])
33
34    def test_two_timeouts_still_say_so(self):
35        got, op = self.read([urllib.error.URLError(TimeoutError("timed out"))] * 2)
36        self.assertIsInstance(got, transfer.TransferError)
37        self.assertEqual(op.call_count, 2)
38
39    def test_an_answer_of_404_is_not_tried_again(self):
40        err = urllib.error.HTTPError("https://x", 404, "no", {}, None)
41        got, op = self.read([err])
42        self.assertIsInstance(got, transfer.TransferError)
43        self.assertEqual(op.call_count, 1)
44
45    def test_a_good_first_answer_is_one_request(self):
46        got, op = self.read([Answer(b"{}")])
47        self.assertEqual((got, op.call_count), (b"{}", 1))
48
49
50if __name__ == "__main__":
51    unittest.main()