Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_attention_rollup.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_attention_rollup.py · 10.7 KB · 287 lines
1"""The rollup: counts computed on a timer, not on the front page.
2
3`object_daemon.process_attention` runs every declared provider's COUNT
4method and writes `attention_counts`, which the home page and the app
5list read. The reason it is a pass and not a helper the page calls is
6concrete: this box went to 675MB resident and started swapping because
7something folded a big collection on a timer, and a home page that folds
8a dozen collections on every render is that same mistake with a nicer
9name. So the properties worth holding are about what the stored numbers
10are allowed to SAY.
11
12The one that matters most is the failure case. A provider that raises and
13gets recorded as zero would report an empty queue when what happened is
14that nobody looked -- a queue that looks handled, which is the worst
15thing this table could do. A broken provider must look broken.
16"""
17
18import pathlib
19
20import pytest
21
22import object_daemon
23import object_packages
24import object_records
25
26PACKAGES_ROOT = pathlib.Path(__file__).resolve().parents[1] / "packages"
27
28COUNTS = "attention_counts"
29SOURCES = "attention_sources"
30
31GOOD = "def COUNT(request):\n return {'count': 3, 'detail': '1 over a week old'}\n"
32EMPTY = "def COUNT(request):\n return {'count': 0}\n"
33ANGRY = "def COUNT(request):\n raise RuntimeError('the shed is on fire')\n"
34NONSENSE = "def COUNT(request):\n return {'count': 'lots'}\n"
35MUTE = "def COUNT(request):\n return {'count': 9}\n"
36
37
38@pytest.fixture
39def box(tmp_path, monkeypatch):
40 """app-nav installed into empty roots, with the environment pointed at
41 them: the pass resolves providers through the same roots the server
42 would use."""
43 data = tmp_path / "data"
44 objects = tmp_path / "objects"
45 objects.mkdir()
46 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data))
47 monkeypatch.setenv("DBBASIC_OBJECTS_DIR", str(objects))
48 monkeypatch.delenv("DBBASIC_ATTENTION_INTERVAL_SECONDS", raising=False)
49
50 object_packages.install_package(
51 "app-nav", root=PACKAGES_ROOT, base_dir=data, object_roots=[objects])
52 return {"data": data, "objects": objects}
53
54
55def provider(box, object_id, source):
56 (box["objects"] / f"{object_id}.py").write_text(source)
57
58
59def declare(box, source_id, object_id, **fields):
60 row = {"id": source_id, "package": "app-test", "object_id": object_id,
61 "label": source_id.replace("_", " ").title(), "path": f"/{source_id}",
62 "nav_id": "", "group": "Work", "severity": "normal",
63 "operator_muted": "false"}
64 row.update(fields)
65 object_records.create_collection_record(
66 SOURCES, row, base_dir=box["data"], actor="test")
67
68
69def counts(box):
70 try:
71 rows = object_records.read_collection_records(COUNTS, base_dir=box["data"])
72 except Exception:
73 return {}
74 return {row["id"]: row for row in rows}
75
76
77def run(box):
78 """One pass, with the interval marker cleared so it is always due.
79
80 Every test but the interval one is about what the pass WRITES, and
81 making each of them wait five minutes would be a slow way to test
82 nothing.
83 """
84 marker = box["data"] / object_daemon.ATTENTION_MARKER_NAME
85 if marker.exists():
86 marker.unlink()
87 return object_daemon.process_attention(base_dir=box["data"])
88
89
90# --- the pass ------------------------------------------------------------------
91
92def test_the_pass_writes_a_count_per_declared_queue(box):
93 provider(box, "system_demo_attention", GOOD)
94 declare(box, "demo_queue", "system_demo_attention",
95 label="Demo queue", path="/demo", nav_id="home", severity="warning")
96
97 result = run(box)
98 assert result == {"sources": 1, "counted": 1, "failed": 0, "muted": 0}
99
100 row = counts(box)["demo_queue"]
101 assert row["count"] == "3"
102 assert row["detail"] == "1 over a week old"
103 assert row["source_id"] == "demo_queue"
104 assert row["label"] == "Demo queue"
105 assert row["path"] == "/demo"
106 assert row["nav_id"] == "home"
107 assert row["severity"] == "warning"
108 assert row["error"] == ""
109 assert row["computed_at"]
110
111
112def test_a_second_pass_updates_rather_than_duplicating(box):
113 provider(box, "system_demo_attention", GOOD)
114 declare(box, "demo_queue", "system_demo_attention")
115 run(box)
116 provider(box, "system_demo_attention",
117 "def COUNT(request):\n return {'count': 1}\n")
118 run(box)
119
120 assert len(counts(box)) == 1
121 assert counts(box)["demo_queue"]["count"] == "1"
122 assert counts(box)["demo_queue"]["detail"] == "" # the old sentence is gone
123
124
125def test_a_zero_is_written_down_rather_than_left_unsaid(box):
126 """The absence of a row means nobody has ever run the provider, which
127 is a different fact from 'the queue is empty'. Only the surfaces
128 decide that zero is not news."""
129 provider(box, "system_quiet_attention", EMPTY)
130 declare(box, "quiet_queue", "system_quiet_attention")
131
132 run(box)
133 assert counts(box)["quiet_queue"]["count"] == "0"
134
135
136def test_one_bad_provider_does_not_blind_the_others(box):
137 provider(box, "system_demo_attention", GOOD)
138 provider(box, "system_angry_attention", ANGRY)
139 declare(box, "demo_queue", "system_demo_attention")
140 declare(box, "angry_queue", "system_angry_attention")
141
142 result = run(box)
143 assert (result["counted"], result["failed"]) == (1, 1)
144 assert counts(box)["demo_queue"]["count"] == "3"
145
146
147# --- a broken provider must look broken -----------------------------------------
148
149def test_a_provider_that_raises_records_the_error_and_keeps_the_count(box):
150 """Zeroing it would report an empty queue when what happened is that
151 nobody looked -- a queue that looks handled is the worst thing this
152 table could say."""
153 provider(box, "system_demo_attention", GOOD)
154 declare(box, "demo_queue", "system_demo_attention")
155 run(box)
156 was = counts(box)["demo_queue"]
157
158 provider(box, "system_demo_attention", ANGRY)
159 result = run(box)
160
161 assert result["failed"] == 1
162 row = counts(box)["demo_queue"]
163 assert row["count"] == "3" # the last real number survives
164 assert "the shed is on fire" in row["error"]
165 # And so does its timestamp: the age on the page describes the number,
166 # not the attempt.
167 assert row["computed_at"] == was["computed_at"]
168
169
170def test_a_provider_that_answers_with_nonsense_is_a_failure_not_a_zero(box):
171 provider(box, "system_demo_attention", GOOD)
172 declare(box, "demo_queue", "system_demo_attention")
173 run(box)
174
175 provider(box, "system_demo_attention", NONSENSE)
176 assert run(box)["failed"] == 1
177 row = counts(box)["demo_queue"]
178 assert row["count"] == "3"
179 assert "expected an integer" in row["error"]
180
181
182def test_a_provider_that_never_worked_records_the_error_against_zero(box):
183 """No previous number to keep is not a reason to invent one, and the
184 error is what says the zero is not an answer."""
185 provider(box, "system_angry_attention", ANGRY)
186 declare(box, "angry_queue", "system_angry_attention")
187 run(box)
188
189 row = counts(box)["angry_queue"]
190 assert row["count"] == "0"
191 assert row["error"]
192 assert row["computed_at"] == ""
193
194
195def test_a_missing_provider_object_is_an_error_not_a_silent_zero(box):
196 declare(box, "ghost_queue", "system_nowhere_attention")
197 assert run(box)["failed"] == 1
198 assert counts(box)["ghost_queue"]["error"]
199
200
201def test_a_recovered_provider_clears_its_error(box):
202 provider(box, "system_demo_attention", ANGRY)
203 declare(box, "demo_queue", "system_demo_attention")
204 run(box)
205 provider(box, "system_demo_attention", GOOD)
206 run(box)
207
208 row = counts(box)["demo_queue"]
209 assert row["error"] == ""
210 assert row["count"] == "3"
211
212
213# --- the operator's mute -----------------------------------------------------------
214
215def test_a_muted_source_is_not_run_and_leaves_no_stale_number(box):
216 """A muted queue should cost nothing and show nothing, and leaving
217 the last number behind would show a stale one."""
218 provider(box, "system_mute_attention", MUTE)
219 declare(box, "mute_queue", "system_mute_attention")
220 run(box)
221 assert counts(box)["mute_queue"]["count"] == "9"
222
223 object_records.update_collection_record(
224 SOURCES, "mute_queue", {"operator_muted": "true"},
225 base_dir=box["data"], actor="operator")
226 result = run(box)
227
228 assert result == {"sources": 1, "counted": 0, "failed": 0, "muted": 1}
229 assert "mute_queue" not in counts(box)
230
231
232# --- when to run at all ---------------------------------------------------------
233
234def test_the_interval_marker_is_honoured(box, monkeypatch):
235 """A count that is five minutes stale is fine; a pass that re-runs a
236 dozen providers on every one-second poll is not."""
237 provider(box, "system_demo_attention", GOOD)
238 declare(box, "demo_queue", "system_demo_attention")
239
240 assert run(box) is not None # first call: no marker yet
241 assert object_daemon.process_attention(base_dir=box["data"]) is None
242
243 monkeypatch.setenv("DBBASIC_ATTENTION_INTERVAL_SECONDS", "0")
244 assert object_daemon.process_attention(base_dir=box["data"]) is not None
245
246
247def test_nothing_declared_is_not_a_pass(box):
248 assert object_daemon.process_attention(base_dir=box["data"]) is None
249 assert not (box["data"] / object_daemon.ATTENTION_MARKER_NAME).exists()
250
251
252# --- the providers this repository actually ships ----------------------------------
253
254def _shipped_providers():
255 import json
256 for manifest_path in sorted(PACKAGES_ROOT.glob("*/dbbasic-package.json")):
257 manifest = json.loads(manifest_path.read_text())
258 paths = {entry["id"]: entry["path"] for entry in manifest.get("objects", [])}
259 for entry in manifest.get("attention") or []:
260 yield (manifest["id"], entry["object_id"],
261 manifest_path.parent / paths[entry["object_id"]])
262
263
264@pytest.mark.parametrize("package_id, object_id, source_path",
265 list(_shipped_providers()),
266 ids=lambda value: str(value)[:40])
267def test_every_shipped_provider_reads_zero_on_an_empty_box(
268 package_id, object_id, source_path, tmp_path, monkeypatch):
269 """A deployment that never installed the app must not produce an error
270 every five minutes. A provider degrades to zero when its collections
271 are absent; only a genuine failure raises."""
272 data = tmp_path / "data"
273 data.mkdir()
274 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data))
275
276 module = {}
277 exec(compile(source_path.read_text(), str(source_path), "exec"), module)
278 assert module["COUNT"]({}) == {"count": 0}
279
280
281def test_a_box_without_the_home_app_runs_nothing_and_says_nothing(tmp_path, monkeypatch):
282 """The pass must not raise, log, or create files on a deployment that
283 never installed app-nav."""
284 data = tmp_path / "data"
285 data.mkdir()
286 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data))
287 assert object_daemon.process_attention(base_dir=data) is None