Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_steel/tests/publish_update.rs

3.5 KiB, 1 run

created by r1870400018:68864, 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//! The read-modify-write primitive itself, against a real Ozone instance.
2//!
3//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
4//! Anthropic Claude
5
6use oxedyne_fe2o3_core::prelude::*;
7use oxedyne_fe2o3_iop_db::api::Database;
8use oxedyne_fe2o3_jdat::prelude::*;
9use oxedyne_fe2o3_steel::srv::publish::store::{
10 self,
11 Edit,
12};
13
14use std::sync::Barrier;
15
16mod common;
17
18const THREADS: usize = 32;
19
20fn text<T>(r: Outcome<T>) -> Result<T, String> {
21 match r {
22 Ok(v) => Ok(v),
23 Err(e) => Err(fmt!("{}", e)),
24 }
25}
26
27// Runs `f(0..n)` on n threads released together, so they meet in the store at once.
28fn together<R, F>(n: usize, f: F) -> Vec<Result<R, String>>
29where
30 R: Send,
31 F: Fn(usize) -> Result<R, String> + Sync,
32{
33 let gate = Barrier::new(n);
34 std::thread::scope(|s| {
35 let handles: Vec<_> = (0..n).map(|i| {
36 let gate = &gate;
37 let f = &f;
38 s.spawn(move || {
39 gate.wait();
40 f(i)
41 })
42 }).collect();
43 handles.into_iter()
44 .map(|h| h.join().unwrap_or_else(|_| Err(fmt!("a thread panicked"))))
45 .collect()
46 })
47}
48
49fn all_ok<R>(results: Vec<Result<R, String>>) -> Outcome<Vec<R>> {
50 let mut out = Vec::new();
51 for r in results {
52 match r {
53 Ok(v) => out.push(v),
54 Err(e) => return Err(err!("a thread failed: {}", e; Test, Invalid)),
55 }
56 }
57 Ok(out)
58}
59
60/// The primitive itself: thirty-two increments of one key arrive as thirty-two.
61#[test]
62fn update_loses_no_change() -> Outcome<()> {
63 let (db, uid, _tmp) = res!(common::test_db());
64 let handle = (db, uid);
65 let key = dat!("test/counter");
66 let results = together(THREADS, |_| {
67 text(store::update(&handle, &key, |old| -> Outcome<(Edit, ())> {
68 let n = match old {
69 Some(Dat::Str(s)) => res!(s.parse::<u64>().map_err(|e| err!("{}", e; Invalid, Input))),
70 _ => 0,
71 };
72 Ok((Edit::Set(dat!(fmt!("{}", n + 1))), ()))
73 }))
74 });
75 res!(all_ok(results));
76 let (db_arc, _) = &handle;
77 let guard = lock_read!(db_arc);
78 match res!(guard.get(&key, None)) {
79 Some((Dat::Str(s), _)) => assert_eq!(s, fmt!("{}", THREADS)),
80 other => return Err(err!("unexpected counter value {:?}", other; Test, Invalid)),
81 }
82 Ok(())
83}
84
85/// `Keep` writes nothing, and `Clear` removes the key.
86#[test]
87fn update_keeps_and_clears() -> Outcome<()> {
88 let (db, uid, _tmp) = res!(common::test_db());
89 let handle = (db, uid);
90 let key = dat!("test/cell");
91 // An absent key is handed over as `None`, and `Keep` leaves it absent.
92 let saw = res!(store::update(&handle, &key, |old| -> Outcome<(Edit, bool)> {
93 Ok((Edit::Keep, old.is_none()))
94 }));
95 assert!(saw);
96 {
97 let (db_arc, _) = &handle;
98 let guard = lock_read!(db_arc);
99 assert!(res!(guard.get(&key, None)).is_none(), "Keep wrote a value");
100 }
101 res!(store::update(&handle, &key, |_| -> Outcome<(Edit, ())> { Ok((Edit::Set(dat!("v")), ())) }));
102 let saw = res!(store::update(&handle, &key, |old| -> Outcome<(Edit, bool)> {
103 Ok((Edit::Clear, matches!(old, Some(Dat::Str(ref s)) if s == "v")))
104 }));
105 assert!(saw, "the value written was not handed back");
106 let (db_arc, _) = &handle;
107 let guard = lock_read!(db_arc);
108 assert!(res!(guard.get(&key, None)).is_none(), "Clear left the value");
109 Ok(())
110}