askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_state_increment.py · 4.6 KB · 138 lines
| 1 | """ObjectStateManager.increment: old semantics, exact under concurrency.""" |
| 2 | import subprocess |
| 3 | import sys |
| 4 | import threading |
| 5 | from pathlib import Path |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | import object_state |
| 10 | |
| 11 | ROOT = Path(__file__).resolve().parents[1] |
| 12 | |
| 13 | |
| 14 | def 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 | |
| 24 | def 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 | |
| 29 | def 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 | |
| 41 | def 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 | |
| 57 | def 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 | |
| 76 | def 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 | |
| 91 | def 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 | |
| 102 | def 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 | |
| 124 | def 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 |