Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_steel/tests/db_sync_on_write.rs

3.4 KiB, 1 run

created by r1870400018:71904, 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//! A vhost's `db_sync_on_write` reaches the Ozone its store is opened with.
2//!
3//! The key is read from JDAT as a boolean that defaults to off, so every config written before it
4//! existed opens its store on the library's interval sync as before. A vhost that sets it opens a
5//! store that fsyncs every write.
6
7use oxedyne_fe2o3_core::prelude::*;
8use oxedyne_fe2o3_jdat::prelude::*;
9use oxedyne_fe2o3_jdat::string::enc::EncoderConfig;
10use oxedyne_fe2o3_steel::srv::{
11 cfg::VhostConfig,
12 context,
13};
14
15/// The vhost a JDAT map gives, read once, written back to JDAT text and read again, with the two
16/// readings required to agree.
17fn read_twice(src: &str) -> Outcome<VhostConfig> {
18 let dat = res!(Dat::decode_string(src));
19 let map = match &dat {
20 Dat::Map(m) => m.clone(),
21 other => return Err(err!("Expected a map, got {:?}.", other; Invalid, Input)),
22 };
23 let first = res!(VhostConfig::from_datmap(&map));
24 let text = res!(dat.encode_string_with_config(&EncoderConfig::<(), ()>::default()));
25 let again = match res!(Dat::decode_string(&text)) {
26 Dat::Map(m) => m,
27 other => return Err(err!("Expected a map, got {:?}.", other; Invalid, Input)),
28 };
29 let second = res!(VhostConfig::from_datmap(&again));
30 assert_eq!(first.db_sync_on_write, second.db_sync_on_write,
31 "the key did not survive a JDAT round trip: {}", text);
32 Ok(second)
33}
34
35#[test]
36fn a_vhost_that_sets_the_key_opens_a_store_that_syncs_every_write() -> Outcome<()> {
37 let on = res!(read_twice(r#"{"hostnames": ["wiki.test"], "db_dir_rel": "./o3db", "db_sync_on_write": true}"#));
38 let off = res!(read_twice(r#"{"hostnames": ["site.test"], "db_dir_rel": "./o3db"}"#));
39 assert!(on.db_sync_on_write, "the key was set and not read");
40 assert!(!off.db_sync_on_write, "a vhost that never named the key must not sync every write");
41 assert!(!VhostConfig::default().db_sync_on_write);
42
43 // The configuration a store is opened with carries the choice, and nothing else moves.
44 assert!(context::db_config(on.db_sync_on_write).sync_on_write);
45 assert!(!context::db_config(off.db_sync_on_write).sync_on_write);
46 let (a, b) = (context::db_config(true), context::db_config(false));
47 assert_eq!(a.sync_interval_ms, b.sync_interval_ms);
48 assert_eq!(a.cache_size_limit_bytes, b.cache_size_limit_bytes);
49
50 // And the store itself is opened with it.
51 let root = std::env::temp_dir().join(fmt!("steel_db_sync_on_write_{}", std::process::id()));
52 for (flag, name) in [(true, "on"), (false, "off")] {
53 let dir = root.join(name);
54 res!(std::fs::create_dir_all(&dir));
55 let db = res!(context::new_db(&dir, &[7u8; 32], flag));
56 assert_eq!(db.cfg().sync_on_write, flag, "the {} store was opened with the wrong sync", name);
57 }
58 let _ = std::fs::remove_dir_all(&root);
59 Ok(())
60}
61
62#[test]
63fn a_key_that_is_not_a_boolean_is_refused() -> Outcome<()> {
64 let dat = res!(Dat::decode_string(r#"{"hostnames": ["x.test"], "db_sync_on_write": "yes"}"#));
65 let map = match dat {
66 Dat::Map(m) => m,
67 other => return Err(err!("Expected a map, got {:?}.", other; Invalid, Input)),
68 };
69 match VhostConfig::from_datmap(&map) {
70 Ok(_) => Err(err!("A string 'db_sync_on_write' was accepted."; Test)),
71 Err(e) => {
72 assert!(fmt!("{}", e).contains("db_sync_on_write"), "the error must name the key: {}", e);
73 Ok(())
74 }
75 }
76}