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_change_dispatch_catch_up.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, 18 minutes ago History
test_change_dispatch_catch_up.py · 4.4 KB · 116 lines
1"""Enabling change dispatch on a box that already has history.
2
3Found by simulation: DBBASIC_ENABLE_CHANGE_DISPATCH turned on after payments
4had landed silently skipped every earlier write -- paid invoices stayed
5"sent", paying subscribers went past_due. The first pass still starts at
6"now" by default (replaying years of history through the handlers would,
7among other things, queue a confirmation email for every order ever
8confirmed: system_order_email's idempotency marker only knows about messages
9that handler itself composed), but it now says loudly what it skipped, and
10the operator can choose a replay point.
11"""
12
13import json
14
15import object_change_dispatch as ocd
16from test_change_dispatch import create_widget, setup_env
17
18
19def _log_lines(path):
20 return path.read_text(encoding="utf-8").strip().splitlines() if path.exists() else []
21
22
23def test_the_first_pass_reports_the_history_it_did_not_dispatch(tmp_path, monkeypatch):
24 _, data_dir, log_path = setup_env(tmp_path, monkeypatch)
25 create_widget(data_dir, "w1")
26 create_widget(data_dir, "w2")
27
28 ocd.dispatch_pending(base_dir=data_dir)
29
30 baseline = ocd.first_pass_report(base_dir=data_dir)
31 assert baseline["mode"] == "from_now"
32 assert baseline["skipped_total"] == 2
33 assert baseline["skipped_before"] == {"widgets": 2}
34 assert "DBBASIC_CHANGE_DISPATCH_FROM" in baseline["message"]
35 assert _log_lines(log_path) == []
36
37
38def test_a_clean_install_reports_nothing_skipped(tmp_path, monkeypatch):
39 _, data_dir, _ = setup_env(tmp_path, monkeypatch)
40 ocd.dispatch_pending(base_dir=data_dir)
41 assert ocd.first_pass_report(base_dir=data_dir)["skipped_total"] == 0
42
43
44def test_the_operator_can_replay_from_the_beginning(tmp_path, monkeypatch):
45 _, data_dir, log_path = setup_env(tmp_path, monkeypatch)
46 create_widget(data_dir, "w1")
47 create_widget(data_dir, "w2")
48 monkeypatch.setenv(ocd.FROM_ENV, "beginning")
49
50 first = ocd.dispatch_pending(base_dir=data_dir)
51
52 assert first["dispatched"] == 2
53 assert len(_log_lines(log_path)) == 2
54 baseline = ocd.first_pass_report(base_dir=data_dir)
55 assert baseline["mode"] == "replay"
56 assert baseline["skipped_total"] == 0
57 # and it is a one-off: the next pass dispatches nothing twice
58 assert ocd.dispatch_pending(base_dir=data_dir)["dispatched"] == 0
59 assert len(_log_lines(log_path)) == 2
60
61
62def test_the_operator_can_replay_from_a_timestamp(tmp_path, monkeypatch):
63 _, data_dir, log_path = setup_env(tmp_path, monkeypatch)
64 create_widget(data_dir, "old")
65 changes = ocd.object_record_changes.list_record_changes("widgets", base_dir=data_dir)
66 cutoff = changes["changes"][0]["timestamp"]
67 # A later write, strictly after the cutoff.
68 import time
69 time.sleep(1.1)
70 create_widget(data_dir, "new")
71 monkeypatch.setenv(ocd.FROM_ENV, cutoff)
72
73 first = ocd.dispatch_pending(base_dir=data_dir)
74
75 assert first["dispatched"] == 1
76 assert json.loads(_log_lines(log_path)[0])["record_id"] == "new"
77
78
79def test_a_bad_replay_point_is_loud_and_falls_back_to_now(tmp_path, monkeypatch):
80 _, data_dir, log_path = setup_env(tmp_path, monkeypatch)
81 create_widget(data_dir)
82 monkeypatch.setenv(ocd.FROM_ENV, "last tuesday")
83
84 ocd.dispatch_pending(base_dir=data_dir)
85
86 baseline = ocd.first_pass_report(base_dir=data_dir)
87 assert baseline["mode"] == "from_now"
88 assert "last tuesday" in baseline["message"]
89 assert _log_lines(log_path) == []
90
91
92def test_more_changes_than_one_page_are_not_dropped(tmp_path, monkeypatch):
93 """The cursor used to jump to the newest change even when only the
94 newest `limit` had been read, so a backlog bigger than one page lost its
95 older entries for good."""
96 _, data_dir, log_path = setup_env(tmp_path, monkeypatch)
97 ocd.dispatch_pending(base_dir=data_dir)
98 for i in range(12):
99 create_widget(data_dir, f"w{i:02d}")
100
101 result = ocd.dispatch_pending(base_dir=data_dir, limit=5)
102
103 assert result["dispatched"] == 12
104 assert len(_log_lines(log_path)) == 12
105
106
107def test_daemon_status_warns_about_skipped_history(tmp_path, monkeypatch):
108 import object_daemon_status
109 objects_dir, data_dir, _ = setup_env(tmp_path, monkeypatch)
110 create_widget(data_dir)
111 ocd.dispatch_pending(base_dir=data_dir)
112
113 status = object_daemon_status.reactions_status(base_dir=data_dir, object_roots=[objects_dir])
114 ids = [p["id"] for p in status["problems"]]
115 assert "change_dispatch_skipped_history" in ids
116 assert status["handlers"]["change_dispatch_baseline"]["skipped_total"] == 1