Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_o3db_sync/src/test/setup.rs

4.6 KiB, 31 runs

created by r1870400018:817, 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

1use crate::{
2 prelude::*,
3 base::cfg::OzoneConfig,
4 data::core::RestSchemesInput,
5};
6
7use oxedyne_fe2o3_jdat::{
8 prelude::*,
9 id::IdDat,
10};
11
12use std::{
13 mem,
14 path::PathBuf,
15};
16
17pub fn default_cfg() -> Outcome<OzoneConfig> {
18 Ok(OzoneConfig {
19 format_version: constant::CURRENT_FORMAT_VERSION,
20 // Key hashing
21 bytes_before_hashing: 32,
22 // Caches
23 cache_size_limit_bytes: 100_000_000,
24 init_load_caches: true,
25 // Files
26 data_file_max_bytes: 2_000,//1_000_000,
27 // Chunking
28 rest_chunk_threshold: 1_500,
29 rest_chunk_bytes: 64,
30 // Bots
31 num_cbots_per_zone: 2,
32 num_fbots_per_zone: 2,
33 num_igbots_per_zone: 2,
34 num_rbots_per_zone: 2,
35 num_wbots_per_zone: 1,
36 num_sbots: 2,
37 num_scbots_per_zone: 1,
38 // Zones
39 num_zones: 2,
40 zone_state_update_secs: 1,
41 zone_overrides: mapdat!{
42 1u16 => mapdat!{
43 "dir" => "../test_db_zone_container",
44 "max_size" => 1_000_000u64,
45 },
46 //2u16 => res!(mapdat!{
47 // "dir" => "",
48 // "max_size" => 100u64,
49 //}),
50 3u16 => mapdat!{
51 "dir" => "",
52 "max_size" => 1_000_000u64,
53 },
54 }.get_map().unwrap(),
55 // Durability barrier off by default in tests so the existing
56 // suites run at their previous throughput. The
57 // `durability_barrier` integration test flips `sync_on_write`
58 // on its own copy of this config.
59 sync_on_write: false,
60 sync_every_n_writes: 0,
61 sync_interval_ms: 0,
62 })
63}
64
65pub type UidTyp = u128; // Concrete underlying user id type for testing.
66pub const UID_LEN: usize = mem::size_of::<UidTyp>();
67pub type Uid = IdDat<{ UID_LEN }, UidTyp>;
68
69pub fn start_db<
70 ENC: Encrypter + 'static,
71 KH: Hasher + 'static,
72 PR: Hasher + 'static,
73 CS: Checksummer + 'static,
74>(
75 db_root: PathBuf,
76 cfg_opt: Option<OzoneConfig>,
77 schms_input: RestSchemesInput<ENC, KH, PR, CS>,
78 zone_path: Option<String>, // a separate zone container, created here if missing
79 gc_on: bool,
80 wipe: bool,
81)
82 -> Outcome<O3db<
83 UID_LEN,
84 Uid,
85 ENC,
86 KH,
87 PR,
88 CS,
89 >>
90{
91 // Pre-wipe the persisted config so tests survive format
92 // changes (e.g. a new `OzoneConfig` field added to the
93 // struct). O3db::new loads config.jdat unconditionally if the
94 // file exists, and a stale file that predates a schema bump
95 // makes the load fail with a "missing field" error. Removing
96 // the file up front lets the supplied `cfg_opt` recreate a
97 // fresh config on disk.
98 if wipe {
99 let cfg_path = OzoneConfig::config_path(&db_root);
100 let _ = std::fs::remove_file(&cfg_path);
101 }
102 // The store refuses a zone container that does not exist, and a start fails on it.
103 if let Some(dir) = &zone_path {
104 res!(std::fs::create_dir_all(dir));
105 }
106 let mut db = res!(O3db::new(
107 db_root,
108 cfg_opt,
109 schms_input,
110 Uid::default(),
111 ));
112 let files = res!(db.find_all_data_files());
113 test!(sync_log::stream(), "Found {} existing data and index files.", files.len());
114 if wipe {
115 for file in files {
116 test!(sync_log::stream(), " Deleting {:?}", file);
117 res!(std::fs::remove_file(file));
118 }
119 }
120 test!(sync_log::stream(), "Starting db...");
121 // `start` returns once every zone is ready, so nothing has to be slept off here.
122 res!(db.start("test"));
123 res!(ok!(db.updated_api()).activate_gc(gc_on));
124
125 // Ping all bots.
126 let (start, msgs) = res!(db.api().ping_bots(constant::USER_REQUEST_WAIT));
127 test!(sync_log::stream(), "{} ping replies received in {:?}.", msgs.len(), start.elapsed());
128 Ok(db)
129}
130