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

9 downloads

dbbasic-object-server/tests/test_event_publish_single_pass.py
Install on DigitalOcean 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.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 19 minutes ago History
test_event_publish_single_pass.py · 4.6 KB · 110 lines
1"""publish_event reads the events file once and rewrites it once, and the rows
2it leaves behind are exactly those the old two-step "publish, then prune" left.
3
4The events file sits at its retention cap (1000 rows by default) in steady
5state, so every extra parse or rewrite per publish is paid on every write the
6server takes. These tests pin the cost, and pin that it did not buy a change in
7what is kept."""
8import json
9import shutil
10
11import pytest
12
13import object_events
14import object_ids
15import object_state
16
17NOW = 1_800_000_000
18
19
20@pytest.fixture(autouse=True)
21def _frozen(monkeypatch):
22 monkeypatch.setattr(object_events.time, "time", lambda: float(NOW))
23 counter = iter(range(10**6))
24 monkeypatch.setattr(object_ids, "new_uuid4", lambda: f"id-{next(counter)}")
25
26
27def _event_row(manager, event_id, ts):
28 manager.set(f"event_{ts}_{event_id}", json.dumps({"id": event_id, "event_type": "t", "timestamp": ts}))
29
30
31def _seed(data_dir, n=30):
32 manager = object_state.ObjectStateManager(object_events.EVENTS_OBJECT_ID, base_dir=data_dir)
33 for i in range(n):
34 _event_row(manager, f"old{i}", NOW - 5000 - i)
35 return manager
36
37
38def _keys(data_dir):
39 return set(object_state.get_object_state(object_events.EVENTS_OBJECT_ID, data_dir))
40
41
42def test_a_publish_that_prunes_reads_the_file_once_and_writes_it_once(tmp_path, monkeypatch):
43 _seed(tmp_path / "data")
44 reads, writes = [], []
45 real_read, real_write = object_state.get_object_state, object_state._write_object_state
46 monkeypatch.setattr(object_state, "get_object_state", lambda *a, **k: (reads.append(1), real_read(*a, **k))[1])
47 monkeypatch.setattr(object_state, "_write_object_state", lambda *a, **k: (writes.append(1), real_write(*a, **k))[1])
48
49 object_events.publish_event("record.created", base_dir=tmp_path / "data", keep_count=10)
50
51 assert (len(reads), len(writes)) == (1, 1)
52 assert len([k for k in _keys(tmp_path / "data") if k.startswith("event_")]) == 10
53
54
55@pytest.mark.parametrize("keep_count, keep_seconds", [(5, None), (None, 1000), (8, 4000), (1, 60), (100, None)])
56def test_publishing_with_retention_equals_publishing_then_pruning(tmp_path, monkeypatch, keep_count, keep_seconds):
57 monkeypatch.setattr(object_ids, "new_uuid4", lambda: "the-new-event") # same key in both runs
58 one_pass, two_step = tmp_path / "one", tmp_path / "two"
59 manager = _seed(one_pass)
60 # a corrupt row and a subscription that protects one old event
61 manager.set("event_corrupt", "{not json")
62 manager.set("sub_a", json.dumps({"last_event_id": "old3"}))
63 shutil.copytree(one_pass, two_step)
64
65 object_events.publish_event("record.created", base_dir=one_pass, keep_count=keep_count, keep_seconds=keep_seconds)
66
67 object_events.publish_event("record.created", base_dir=two_step) # no retention: only adds
68 object_events.prune_events(base_dir=two_step, keep_count=keep_count, keep_seconds=keep_seconds, now=NOW)
69
70 assert _keys(one_pass) == _keys(two_step)
71 assert any(k.endswith("_old3") for k in _keys(one_pass)) # the protected row survived
72
73
74def test_publish_without_retention_options_removes_nothing_not_even_corrupt_rows(tmp_path):
75 manager = _seed(tmp_path / "data", n=5)
76 manager.set("event_corrupt", "{not json")
77 before = _keys(tmp_path / "data")
78
79 object_events.publish_event("record.created", base_dir=tmp_path / "data")
80
81 after = _keys(tmp_path / "data")
82 assert before < after and len(after) == len(before) + 1
83
84
85def test_state_manager_update_sets_then_deletes_in_one_write(tmp_path, monkeypatch):
86 manager = object_state.ObjectStateManager("basics", base_dir=tmp_path / "data")
87 manager.set("keep", "1")
88 manager.set("drop_me", "2")
89 writes = []
90 real_write = object_state._write_object_state
91 monkeypatch.setattr(object_state, "_write_object_state", lambda *a, **k: (writes.append(1), real_write(*a, **k))[1])
92 seen = {}
93
94 def after_set(state):
95 seen.update(state) # sees the new key already applied
96 return ["drop_me", "never_existed", "drop_me"]
97
98 removed = manager.update({"fresh": "3"}, after_set=after_set)
99
100 assert removed == 1 and len(writes) == 1
101 assert seen["fresh"] == "3" and "drop_me" in seen
102 assert object_state.get_object_state("basics", tmp_path / "data") == {"keep": 1, "fresh": 3}
103
104
105def test_state_manager_update_rejects_bad_keys_before_writing_anything(tmp_path):
106 manager = object_state.ObjectStateManager("basics", base_dir=tmp_path / "data")
107 manager.set("keep", "1")
108 with pytest.raises(ValueError):
109 manager.update({"fine": "x", "bad\tkey": "y"})
110 assert object_state.get_object_state("basics", tmp_path / "data") == {"keep": 1}