Debian Codegraph @

tests/functional_tests/test_windows_timezone.py

raw
1 from __future__ import annotations
2
3 import sys
4 import threading
5 from typing import Awaitable, Callable
6
7 import pytest
8 from textual.message import Message
9 from textual.pilot import Pilot
10
11 from harlequin import Harlequin
12 from harlequin.adapter import HarlequinAdapter
13 from harlequin.app import QuerySubmitted, ResultsFetched, TzDataDownloadStarted
14 from harlequin.exception import HarlequinTzDataError
15 from tests.functional_tests.helpers import wait_for_any_table, wait_for_editor
16 from tests.waiting import settle_app, wait_for_messages
17
18
19 async def _await_results(
20 app: Harlequin, pilot: Pilot, messages: list[Message]
21 ) -> ResultsFetched:
22 """The fetch's message, once its tab has mounted.
23
24 `wait_for_workers()` cannot stand in for this: a `QuerySubmitted` that has
25 not been handled yet has started no worker, so it returns with nothing to
26 wait for. And `TabbedContent.add_pane` activates the tab it just added,
27 which `Tabs` refuses until that tab is in its list -- so a test that stops
28 pumping in between can tear down mid-mount.
29 """
30 await wait_for_messages(pilot, messages, ResultsFetched)
31 await wait_for_any_table(pilot, app)
32 return [m for m in messages if isinstance(m, ResultsFetched)][-1]
33
34
35 @pytest.fixture
36 def windows_app(
37 monkeypatch: pytest.MonkeyPatch, duckdb_adapter: type[HarlequinAdapter]
38 ) -> Harlequin:
39 """An app that believes it is on Windows, which is the only platform that
40 looks for a timezone database.
41
42 Built inside the fixture after the patch, because `__init__` is what reads
43 `sys.platform` -- the app fixture is constructed before a test body runs.
44 """
45 monkeypatch.setattr(sys, "platform", "win32")
46 return Harlequin(duckdb_adapter([":memory:"], no_init=True), connection_hash="foo")
47
48
49 @pytest.mark.asyncio
50 async def test_the_app_starts_while_the_tzdata_download_is_in_flight(
51 windows_app: Harlequin,
52 monkeypatch: pytest.MonkeyPatch,
53 ) -> None:
54 """The editor is up before the download returns, but a fetch waits for it,
55 since Arrow needs the database to build a timestamptz column."""
56 app = windows_app
57 download_started = threading.Event()
58 finish_download = threading.Event()
59
60 def blocking_download(stop: threading.Event | None = None) -> None:
61 download_started.set()
62 assert finish_download.wait(timeout=10.0), "the fetch never released it"
63
64 monkeypatch.setattr("harlequin.app.locate_tzdata", lambda: False)
65 monkeypatch.setattr("harlequin.app.download_tzdata", blocking_download)
66
67 messages: list[Message] = []
68 async with app.run_test(message_hook=messages.append) as pilot:
69 await wait_for_editor(pilot, app)
70 assert download_started.wait(timeout=10.0)
71 assert [m for m in messages if isinstance(m, TzDataDownloadStarted)]
72
73 app.post_message(QuerySubmitted(queries=["select 1 as foo"], limit=None))
74 await settle_app(pilot)
75 assert not [m for m in messages if isinstance(m, ResultsFetched)]
76
77 finish_download.set()
78 assert (await _await_results(app, pilot, messages)).errors == []
79
80
81 @pytest.mark.asyncio
82 async def test_a_fetch_gives_up_on_a_download_that_never_finishes(
83 windows_app: Harlequin,
84 monkeypatch: pytest.MonkeyPatch,
85 ) -> None:
86 """The wait is bounded: a stalled download delays a query, it does not
87 strand the thread or the user."""
88 app = windows_app
89 release_download = threading.Event()
90
91 def stalled_download(stop: threading.Event | None = None) -> None:
92 assert release_download.wait(timeout=10.0)
93
94 monkeypatch.setattr("harlequin.app.locate_tzdata", lambda: False)
95 monkeypatch.setattr("harlequin.app.download_tzdata", stalled_download)
96 monkeypatch.setattr("harlequin.app.TZDATA_WAIT_SECONDS", 0.25)
97
98 messages: list[Message] = []
99 try:
100 async with app.run_test(message_hook=messages.append) as pilot:
101 await wait_for_editor(pilot, app)
102 app.post_message(QuerySubmitted(queries=["select 1 as foo"], limit=None))
103 assert (await _await_results(app, pilot, messages)).errors == []
104 assert not app._tzdata_ready.is_set()
105 finally:
106 release_download.set()
107
108
109 @pytest.mark.asyncio
110 async def test_quitting_abandons_a_download_in_flight(
111 windows_app: Harlequin,
112 monkeypatch: pytest.MonkeyPatch,
113 ) -> None:
114 """Quit sets the stop flag, so the download thread is not joined at exit."""
115 app = windows_app
116 download_started = threading.Event()
117 stop_seen = threading.Event()
118
119 def watching_download(stop: threading.Event | None = None) -> None:
120 download_started.set()
121 assert stop is not None
122 assert stop.wait(timeout=10.0), "quit never set the stop flag"
123 stop_seen.set()
124
125 monkeypatch.setattr("harlequin.app.locate_tzdata", lambda: False)
126 monkeypatch.setattr("harlequin.app.download_tzdata", watching_download)
127 # the autouse cache stub takes no keywords, and this is the one test that
128 # runs the real `action_quit`
129 monkeypatch.setattr("harlequin.app.update_catalog_cache", lambda **_: None)
130
131 async with app.run_test() as pilot:
132 await wait_for_editor(pilot, app)
133 assert download_started.wait(timeout=10.0)
134 await app.action_quit()
135 assert stop_seen.wait(timeout=10.0)
136
137
138 @pytest.mark.asyncio
139 async def test_a_failed_tzdata_download_is_a_warning(
140 windows_app: Harlequin,
141 monkeypatch: pytest.MonkeyPatch,
142 wait_for_workers: Callable[[Harlequin], Awaitable[None]],
143 ) -> None:
144 """A session with no timezone database is usable, so it is not an error
145 modal and it does not stop a query from running."""
146 app = windows_app
147
148 def failed_download(stop: threading.Event | None = None) -> None:
149 raise HarlequinTzDataError(msg="No network.", title="Harlequin Timezone Error")
150
151 monkeypatch.setattr("harlequin.app.locate_tzdata", lambda: False)
152 monkeypatch.setattr("harlequin.app.download_tzdata", failed_download)
153
154 messages: list[Message] = []
155 async with app.run_test(message_hook=messages.append) as pilot:
156 await wait_for_editor(pilot, app)
157 await wait_for_workers(app)
158 await pilot.pause()
159 assert len(app.screen_stack) == 1
160 assert app._tzdata_ready.is_set()
161 failures = [
162 n
163 for n in app._notifications
164 if n.title == "Harlequin Timezone Error" and n.severity == "warning"
165 ]
166 assert [n for n in failures if "No network." in n.message]
167
168 app.post_message(QuerySubmitted(queries=["select 1 as foo"], limit=None))
169 assert (await _await_results(app, pilot, messages)).errors == []