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_state_increment.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_state_increment.py · 4.6 KB · 138 lines
1"""ObjectStateManager.increment: old semantics, exact under concurrency."""
2import subprocess
3import sys
4import threading
5from pathlib import Path
6
7import pytest
8
9import object_state
10
11ROOT = Path(__file__).resolve().parents[1]
12
13
14def test_missing_key_starts_at_zero_and_returns_new_value(tmp_path):
15 mgr = object_state.ObjectStateManager("counter", base_dir=tmp_path)
16 assert mgr.increment("count") == 1
17 assert mgr.increment("count") == 2
18 assert mgr.increment("count", 5) == 7
19 assert mgr.increment("count", -3) == 4
20 assert mgr.get("count") == 4
21 assert object_state.get_object_state("counter", tmp_path)["count"] == 4
22
23
24def test_float_delta(tmp_path):
25 mgr = object_state.ObjectStateManager("counter", base_dir=tmp_path)
26 assert mgr.increment("x", 0.5) == 0.5
27
28
29def test_non_numeric_value_refused_and_untouched(tmp_path):
30 mgr = object_state.ObjectStateManager("counter", base_dir=tmp_path)
31 mgr.set("name", "bob")
32 with pytest.raises(TypeError):
33 mgr.increment("name")
34 with pytest.raises(TypeError):
35 mgr.increment("n", "1")
36 with pytest.raises(ValueError):
37 mgr.increment("bad\tkey")
38 assert mgr.get("name") == "bob"
39
40
41def test_threads_with_separate_managers_get_exact_total(tmp_path):
42 threads_n, per = 8, 50
43
44 def work():
45 mgr = object_state.ObjectStateManager("counter", base_dir=tmp_path)
46 for _ in range(per):
47 mgr.increment("count")
48
49 threads = [threading.Thread(target=work) for _ in range(threads_n)]
50 for t in threads:
51 t.start()
52 for t in threads:
53 t.join()
54 assert object_state.get_object_state("counter", tmp_path)["count"] == threads_n * per
55
56
57def test_increment_not_lost_against_concurrent_set_of_other_keys(tmp_path):
58 def inc():
59 mgr = object_state.ObjectStateManager("counter", base_dir=tmp_path)
60 for _ in range(60):
61 mgr.increment("count")
62
63 def setter():
64 mgr = object_state.ObjectStateManager("counter", base_dir=tmp_path)
65 for i in range(60):
66 mgr.set("other", i)
67
68 threads = [threading.Thread(target=inc) for _ in range(3)] + [threading.Thread(target=setter)]
69 for t in threads:
70 t.start()
71 for t in threads:
72 t.join()
73 assert object_state.get_object_state("counter", tmp_path)["count"] == 180
74
75
76def test_processes_get_exact_total(tmp_path):
77 code = (
78 "import sys; sys.path.insert(0, sys.argv[1]);"
79 "import object_state;"
80 "m = object_state.ObjectStateManager('counter', base_dir=sys.argv[2]);"
81 "[m.increment('count') for _ in range(40)]"
82 )
83 procs = [
84 subprocess.Popen([sys.executable, "-c", code, str(ROOT), str(tmp_path)])
85 for _ in range(4)
86 ]
87 assert all(p.wait(timeout=60) == 0 for p in procs)
88 assert object_state.get_object_state("counter", tmp_path)["count"] == 160
89
90
91def test_update_rereads_under_lock_so_increments_are_not_lost(tmp_path):
92 # A manager built BEFORE the increments must not write its stale copy back.
93 stale = object_state.ObjectStateManager("counter", base_dir=tmp_path)
94 fresh = object_state.ObjectStateManager("counter", base_dir=tmp_path)
95 for _ in range(5):
96 fresh.increment("count")
97 stale.update({"other": 1})
98 state = object_state.get_object_state("counter", tmp_path)
99 assert state == {"count": 5, "other": 1}
100
101
102def test_update_and_increment_threads_lose_nothing(tmp_path):
103 def inc():
104 mgr = object_state.ObjectStateManager("counter", base_dir=tmp_path)
105 for _ in range(60):
106 mgr.increment("count")
107
108 def updater(n):
109 mgr = object_state.ObjectStateManager("counter", base_dir=tmp_path, load=False)
110 for i in range(60):
111 mgr.update({f"u{n}_{i}": i})
112
113 threads = [threading.Thread(target=inc) for _ in range(3)]
114 threads += [threading.Thread(target=updater, args=(n,)) for n in range(2)]
115 for t in threads:
116 t.start()
117 for t in threads:
118 t.join()
119 state = object_state.get_object_state("counter", tmp_path)
120 assert state["count"] == 180
121 assert sum(1 for k in state if k.startswith("u")) == 120
122
123
124def test_concurrent_publish_event_keeps_every_event(tmp_path):
125 import object_events
126
127 def publish(n):
128 for i in range(25):
129 object_events.publish_event("test.ping", payload={"n": n, "i": i}, base_dir=tmp_path)
130
131 threads = [threading.Thread(target=publish, args=(n,)) for n in range(4)]
132 for t in threads:
133 t.start()
134 for t in threads:
135 t.join()
136 listed = object_events.list_events(event_type="test.ping", base_dir=tmp_path, limit=1000)
137 events = listed.get("events", listed)
138 assert len(events) == 100