Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_steel/tests/publish_rmw.rs

5.6 KiB, 5 runs

created by r1870400018:68862, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Read-modify-write rows under contention, against a real Ozone instance.
2//!
3//! Every derived row in the publish module (the post and subscriber indexes, the rate counters, the
4//! site secret) is read, changed and written back. These tests start many threads together on one
5//! store and ask whether any of their changes was lost. They do not skip when no database starts:
6//! a skipped test cannot be shown red.
7//!
8//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
9//! Anthropic Claude
10
11use oxedyne_fe2o3_core::prelude::*;
12use oxedyne_fe2o3_steel::srv::publish::{
13 Markup,
14 PostState,
15 comment,
16 rate::Window,
17 store::{
18 self,
19 Record,
20 },
21 subscribe,
22};
23
24use std::sync::Barrier;
25
26mod common;
27
28const THREADS: usize = 32;
29
30fn text<T>(r: Outcome<T>) -> Result<T, String> {
31 match r {
32 Ok(v) => Ok(v),
33 Err(e) => Err(fmt!("{}", e)),
34 }
35}
36
37// Runs `f(0..n)` on n threads released together, so they meet in the store at once.
38fn together<R, F>(n: usize, f: F) -> Vec<Result<R, String>>
39where
40 R: Send,
41 F: Fn(usize) -> Result<R, String> + Sync,
42{
43 let gate = Barrier::new(n);
44 std::thread::scope(|s| {
45 let handles: Vec<_> = (0..n).map(|i| {
46 let gate = &gate;
47 let f = &f;
48 s.spawn(move || {
49 gate.wait();
50 f(i)
51 })
52 }).collect();
53 handles.into_iter()
54 .map(|h| h.join().unwrap_or_else(|_| Err(fmt!("a thread panicked"))))
55 .collect()
56 })
57}
58
59fn all_ok<R>(results: Vec<Result<R, String>>) -> Outcome<Vec<R>> {
60 let mut out = Vec::new();
61 for r in results {
62 match r {
63 Ok(v) => out.push(v),
64 Err(e) => return Err(err!("a thread failed: {}", e; Test, Invalid)),
65 }
66 }
67 Ok(out)
68}
69
70fn record(slug: &str) -> Record {
71 Record {
72 slug: slug.to_string(),
73 author: String::new(),
74 categories: Vec::new(),
75 state: PostState::Live,
76 markup: Markup::Markdown,
77 date: Some(fmt!("2026-07-17")),
78 source: fmt!("# {}\n\nA sentence.\n", slug),
79 deliveries: Vec::new(),
80 tags: Vec::new(),
81 ai_level: None,
82 }
83}
84
85/// Thirty-two sign-ups at once all reach the index.
86#[test]
87fn concurrent_signups_all_reach_the_index() -> Outcome<()> {
88 let (db, uid, _tmp) = res!(common::test_db());
89 let handle = (db, uid);
90 let results = together(THREADS, |i| {
91 text(subscribe::add_pending(&handle, &fmt!("reader{}@example.com", i), &Window::default(), 0))
92 });
93 let subs = res!(all_ok(results));
94 assert_eq!(subs.len(), THREADS);
95 let listed = res!(subscribe::list(&handle, "test"));
96 assert_eq!(listed.len(), THREADS, "the index lost {} sign-ups", THREADS - listed.len());
97 Ok(())
98}
99
100/// Thirty-two writes of different posts all reach the post index.
101#[test]
102fn concurrent_posts_all_reach_the_index() -> Outcome<()> {
103 let (db, uid, _tmp) = res!(common::test_db());
104 let handle = (db, uid);
105 let results = together(THREADS, |i| {
106 let rec = record(&fmt!("post-{}", i));
107 text(store::put(&handle, &rec, &rec.slug, "test").map(|_| ()))
108 });
109 res!(all_ok(results));
110 let recs = res!(store::list_records(&handle, "test"));
111 assert_eq!(recs.len(), THREADS, "the post index lost {} posts", THREADS - recs.len());
112 Ok(())
113}
114
115/// A counter allowing five an hour allows exactly five of thirty-two simultaneous attempts.
116#[test]
117fn concurrent_attempts_are_counted_exactly() -> Outcome<()> {
118 let (db, uid, _tmp) = res!(common::test_db());
119 let handle = (db, uid);
120 let results = together(THREADS, |_| {
121 text(comment::rate_allows(&handle, "one-sender", 0, 5))
122 });
123 let allowed = res!(all_ok(results)).into_iter().filter(|a| *a).count();
124 assert_eq!(allowed, 5, "{} attempts were allowed against a limit of 5", allowed);
125 Ok(())
126}
127
128/// Callers racing to make the site secret on an empty store all get the one that was kept.
129#[test]
130fn concurrent_first_callers_share_one_secret() -> Outcome<()> {
131 let (db, uid, _tmp) = res!(common::test_db());
132 let handle = (db, uid);
133 let results = together(8, |_| text(comment::site_secret(&handle)));
134 let secrets = res!(all_ok(results));
135 let kept = res!(comment::site_secret(&handle));
136 for (i, s) in secrets.iter().enumerate() {
137 assert_eq!(s, &kept, "caller {} was handed a secret that was not kept", i);
138 }
139 Ok(())
140}
141
142/// An author's edit and an admin's approval of the same comment, racing, never lose the edit.
143#[test]
144fn concurrent_edit_and_moderation_keep_the_edit() -> Outcome<()> {
145 let (db, uid, _tmp) = res!(common::test_db());
146 let handle = (db, uid);
147 for round in 0..40 {
148 let id = fmt!("c{}", round);
149 res!(comment::put(&handle, &comment::Comment {
150 id: id.clone(),
151 slug: fmt!("post"),
152 body: fmt!("the original words"),
153 created: fmt!("2026-10-09T00:00:00Z"),
154 ..Default::default()
155 }));
156 let results = together(8, |i| {
157 if i % 2 == 0 {
158 text(comment::edit(&handle, "post", &id, "the edited words").map(|_| ()))
159 } else {
160 text(comment::set_state(&handle, "post", &id, comment::CommentState::Approved, None).map(|_| ()))
161 }
162 });
163 res!(all_ok(results));
164 let got = res!(comment::get(&handle, "post", &id));
165 let got = res!(got.ok_or_else(|| err!("the comment vanished"; Test, Missing)));
166 assert_eq!(got.body, "the edited words", "round {}: a moderation wrote back the old body", round);
167 }
168 Ok(())
169}