Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_core/tests/time_clock.rs

5.8 KiB, 2 runs

created by r1870400018:74277, 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//! `ClockSource`: a driven clock read back, torn values, clamping, a short
2//! file, strictly increasing nanoseconds and the system clock. Each test holds
3//! a local source, so the process-wide `install` is left alone here; its
4//! once-only rule is tested in `time_clock_install.rs`, a binary of its own.
5
6#![cfg(not(target_arch = "wasm32"))]
7
8use oxedyne_fe2o3_core::{
9 prelude::*,
10 time::ClockSource,
11};
12
13use std::{
14 fs,
15 path::PathBuf,
16 sync::{
17 Arc,
18 atomic::{
19 AtomicBool,
20 Ordering,
21 },
22 },
23 thread,
24 time::{
25 SystemTime,
26 UNIX_EPOCH,
27 },
28};
29
30fn scratch(name: &str) -> Outcome<PathBuf> {
31 let dir = std::env::temp_dir().join(fmt!("fe2o3_time_clock_{}_{}", std::process::id(), name));
32 res!(fs::create_dir_all(&dir));
33 Ok(dir)
34}
35
36// The writer's way: a whole value into a temporary file, then a rename.
37fn put(path: &PathBuf, ms: u64) -> Outcome<()> {
38 let tmp = path.with_extension("tmp");
39 res!(fs::write(&tmp, ms.to_le_bytes()));
40 res!(fs::rename(&tmp, path));
41 Ok(())
42}
43
44fn driven(path: &PathBuf) -> Outcome<ClockSource> {
45 let spec = fmt!("driven:{}", path.display());
46 ClockSource::from_spec(Some(&spec))
47}
48
49#[test]
50fn test_driven_value_read_back_01() -> Outcome<()> {
51 let dir = res!(scratch("read_back"));
52 let path = dir.join("clock");
53 res!(put(&path, 1_700_000_123_456));
54 let src = res!(driven(&path));
55 assert!(src.is_driven());
56 assert_eq!(res!(src.now_millis()), 1_700_000_123_456);
57 assert_eq!(res!(src.now_secs()), 1_700_000_123);
58 res!(fs::remove_dir_all(&dir));
59 Ok(())
60}
61
62#[test]
63fn test_renamed_value_never_torn_02() -> Outcome<()> {
64 let dir = res!(scratch("torn"));
65 let path = dir.join("clock");
66 // Every written value is a multiple of this, so a torn read would show.
67 const BASE: u64 = 1_700_000_000_000;
68 const STEP: u64 = 0x0101_0101;
69 res!(put(&path, BASE));
70 let done = Arc::new(AtomicBool::new(false));
71 let done_w = done.clone();
72 let path_w = path.clone();
73 let writer = thread::spawn(move || -> Outcome<()> {
74 for i in 1..=10_000u64 {
75 res!(put(&path_w, BASE + i * STEP));
76 }
77 done_w.store(true, Ordering::SeqCst);
78 Ok(())
79 });
80 let src = res!(driven(&path));
81 let mut reads = 0u64;
82 while !done.load(Ordering::SeqCst) {
83 let ms = res!(src.now_millis());
84 let off = ms - BASE;
85 assert!(off % STEP == 0 && off / STEP <= 10_000, "value {} is not one that was written", ms);
86 reads += 1;
87 }
88 match writer.join() {
89 Ok(r) => res!(r),
90 Err(_) => return Err(err!("Writer thread panicked."; Thread)),
91 }
92 assert!(reads > 0);
93 assert_eq!(res!(src.now_millis()), BASE + 10_000 * STEP);
94 res!(fs::remove_dir_all(&dir));
95 Ok(())
96}
97
98#[test]
99fn test_smaller_value_clamps_03() -> Outcome<()> {
100 let dir = res!(scratch("clamp"));
101 let path = dir.join("clock");
102 res!(put(&path, 5_000));
103 let src = res!(driven(&path));
104 assert_eq!(res!(src.now_millis()), 5_000);
105 res!(put(&path, 4_000));
106 assert_eq!(res!(src.now_millis()), 5_000);
107 res!(put(&path, 6_000));
108 assert_eq!(res!(src.now_millis()), 6_000);
109 res!(fs::remove_dir_all(&dir));
110 Ok(())
111}
112
113#[test]
114fn test_seven_byte_file_errors_04() -> Outcome<()> {
115 let dir = res!(scratch("short"));
116 let path = dir.join("clock");
117 res!(fs::write(&path, [1u8; 7]));
118 let src = res!(driven(&path));
119 match src.now_millis() {
120 Ok(v) => return Err(err!("A 7-byte clock file read as {}.", v; Test)),
121 Err(e) => {
122 let s = fmt!("{}", e);
123 assert!(s.contains("7 bytes"), "error does not name the length: {}", s);
124 assert!(s.contains("clock"), "error does not name the path: {}", s);
125 }
126 }
127 res!(fs::remove_dir_all(&dir));
128 Ok(())
129}
130
131#[test]
132fn test_nanos_strictly_increase_05() -> Outcome<()> {
133 let dir = res!(scratch("nanos"));
134 let path = dir.join("clock");
135 res!(put(&path, 1_700_000_000_000));
136 let src = res!(driven(&path));
137 let mut prev = res!(src.now_nanos());
138 for _ in 0..10_000 {
139 let n = res!(src.now_nanos());
140 assert!(n > prev, "now_nanos went from {} to {}", prev, n);
141 assert_eq!(n / 1_000_000, 1_700_000_000_000);
142 prev = n;
143 }
144 res!(fs::remove_dir_all(&dir));
145 Ok(())
146}
147
148#[test]
149fn test_system_matches_systemtime_06() -> Outcome<()> {
150 let src = res!(ClockSource::from_spec(None));
151 assert!(!src.is_driven());
152 assert!(!res!(ClockSource::from_spec(Some("system"))).is_driven());
153 let a = res!(src.now_millis());
154 let b = res!(SystemTime::now().duration_since(UNIX_EPOCH)).as_millis() as u64;
155 assert!(a.abs_diff(b) <= 50, "System {} vs SystemTime {}", a, b);
156 let n = res!(src.now_nanos());
157 assert!((n / 1_000_000) as u64 >= a);
158 assert!(ClockSource::from_spec(Some("sundial")).is_err());
159 assert!(ClockSource::from_spec(Some("driven:")).is_err());
160 Ok(())
161}
162
163#[test]
164fn test_system_time_follows_the_source_07() -> Outcome<()> {
165 let dir = res!(scratch("systime"));
166 let path = dir.join("clock");
167 res!(put(&path, 1_700_000_000_123));
168 let src = res!(driven(&path));
169 let want = UNIX_EPOCH + std::time::Duration::from_millis(1_700_000_000_123);
170 assert_eq!(res!(src.now_system_time()), want);
171 res!(put(&path, 1_700_000_060_123));
172 let moved = res!(res!(src.now_system_time()).duration_since(want));
173 assert_eq!(moved.as_millis(), 60_000, "a stamp did not follow the file");
174
175 let sys = res!(ClockSource::from_spec(None));
176 let a = res!(res!(sys.now_system_time()).duration_since(UNIX_EPOCH)).as_millis();
177 let b = res!(SystemTime::now().duration_since(UNIX_EPOCH)).as_millis();
178 assert!(a.abs_diff(b) <= 50, "System {} vs SystemTime {}", a, b);
179 let _ = fs::remove_dir_all(&dir);
180 Ok(())
181}