Repository navigation
Expand file tree
/
Copy pathfake.rs
More file actions
262 lines (240 loc) · 8.97 KB
/
Copy pathfake.rs
File metadata and controls
262 lines (240 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use std::collections::VecDeque;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;
use std::time::Duration;
use async_trait::async_trait;
use orx_host_proto::{LogInfo, MAX_PAGE_LEN};
use super::{
Backend, ByteRange, CancelOutcome, Fence, FileIdent, Handle, LaunchError, LogFile, LogPage,
Observation, Recovery, RunIntent, WatchedRun,
};
use crate::error::{anyhow, Result};
use crate::store::LaunchSpec;
/// Answers from scripted queues and records every call; an unscripted call panics.
#[derive(Default)]
pub struct FakeBackend {
pub launches: Mutex<VecDeque<std::result::Result<Handle, LaunchError>>>,
pub recoveries: Mutex<VecDeque<Recovery>>,
pub cancels: Mutex<VecDeque<CancelOutcome>>,
/// Observations the feed reports, in order, for whichever of them are watched.
pub feed: Mutex<VecDeque<(String, Observation)>>,
pub log: Mutex<HostFile>,
pub notify: Mutex<HostFile>,
/// Every launch intent the backend was asked to launch or recover.
pub intents: Mutex<Vec<LaunchSpec>>,
/// Child pid each launch records as its submitter.
pub submitter: Mutex<Option<u32>>,
/// Runs inside the next launch, after its fence and before its answer.
pub during_launch: Mutex<Option<Box<dyn FnOnce() + Send>>>,
/// Launches never answer, as when the caller's future is dropped mid-launch.
pub hang_launch: AtomicBool,
pub calls: Mutex<Vec<String>>,
pub unwatched: Mutex<Vec<String>>,
/// Runs whose logs are held for reading, until forgotten.
pub held: Mutex<Vec<String>>,
}
impl FakeBackend {
pub fn then_launch(&self, result: std::result::Result<Handle, LaunchError>) -> &Self {
self.launches.lock().unwrap().push_back(result);
self
}
pub fn then_recover(&self, recovery: Recovery) -> &Self {
self.recoveries.lock().unwrap().push_back(recovery);
self
}
pub fn then_cancel(&self, outcome: CancelOutcome) -> &Self {
self.cancels.lock().unwrap().push_back(outcome);
self
}
pub fn calls(&self) -> Vec<String> {
self.calls.lock().unwrap().clone()
}
/// Records the call and yields, so concurrent callers interleave as with a real backend.
async fn call(&self, name: String) {
self.calls.lock().unwrap().push(name);
tokio::task::yield_now().await;
}
}
fn next<T>(queue: &Mutex<VecDeque<T>>, call: &str) -> T {
queue
.lock()
.unwrap()
.pop_front()
.unwrap_or_else(|| panic!("unscripted {call}"))
}
#[async_trait]
impl Backend for FakeBackend {
async fn launch(
&self,
intent: &RunIntent,
fence: &Fence<'_>,
) -> std::result::Result<Handle, LaunchError> {
assert!(!intent.spec_digest.is_empty());
self.intents.lock().unwrap().push(intent.launch.clone());
self.call(format!("launch {} #{}", intent.run.id, intent.attempt))
.await;
let submitter = *self.submitter.lock().unwrap();
if let Some(pid) = submitter {
fence.record_submitter(pid).unwrap();
}
if self.hang_launch.load(Ordering::SeqCst) {
std::future::pending::<()>().await;
}
let during = self.during_launch.lock().unwrap().take();
if let Some(during) = during {
during();
}
next(&self.launches, "launch")
}
async fn recover(&self, intent: &RunIntent) -> Recovery {
self.intents.lock().unwrap().push(intent.launch.clone());
self.call(format!("recover {} #{}", intent.run.id, intent.attempt))
.await;
next(&self.recoveries, "recover")
}
async fn observe(&self, runs: &[WatchedRun], _: Duration) -> Vec<(String, Observation)> {
let mut feed = self.feed.lock().unwrap();
let (watched, rest) = feed
.drain(..)
.partition(|(id, _)| runs.iter().any(|run| run.id == *id));
*feed = rest;
watched.into()
}
async fn cancel(&self, run: &WatchedRun) -> CancelOutcome {
let handle = serde_json::to_value(&run.handle).unwrap();
self.call(format!(
"cancel {} {}",
run.id,
handle["type"].as_str().unwrap()
))
.await;
next(&self.cancels, "cancel")
}
async fn unwatch(&self, run: &WatchedRun) {
self.unwatched.lock().unwrap().push(run.id.clone());
}
fn forget_logs(&self, forgotten: &dyn Fn(&str) -> bool) {
self.held.lock().unwrap().retain(|run| !forgotten(run));
}
async fn read_log(
&self,
_: &WatchedRun,
file: LogFile,
range: ByteRange,
ident: Option<&FileIdent>,
) -> Result<LogPage> {
tokio::task::yield_now().await;
match file {
LogFile::Log => self.log.lock().unwrap().read(2, ident, &range),
LogFile::Notify => self.notify.lock().unwrap().read(3, ident, &range),
}
}
}
/// A run file as `orx-host` serves it (`crates/orx-host/src/log.rs`): its identity is its
/// inode, which a replacement bumps, and requests are validated the same way.
#[derive(Default)]
pub struct HostFile {
pub bytes: Vec<u8>,
pub ino: u64,
}
impl HostFile {
pub fn read(&self, dev: u64, ident: Option<&FileIdent>, range: &ByteRange) -> Result<LogPage> {
if range.end < range.start || range.end - range.start > MAX_PAGE_LEN {
return Err(anyhow!("invalid log range"));
}
let info = LogInfo {
ident: FileIdent { dev, ino: self.ino },
len: self.bytes.len() as u64,
};
if ident.is_some_and(|ident| *ident != info.ident) || info.len < range.start {
return Ok(LogPage::Reset { info: Some(info) });
}
let end = range.end.min(info.len);
Ok(LogPage::Data {
len: Some(info.len),
ident: info.ident,
start: range.start,
bytes: self.bytes[range.start as usize..end as usize].to_vec(),
eof: end >= info.len,
})
}
}
#[cfg(all(test, unix))]
mod tests {
use super::*;
/// Replays the engine's request shapes against the real host and compares page shapes,
/// with identities renamed by first appearance on each side.
#[test]
fn the_fake_validates_and_pages_like_orx_host() {
let dir = std::env::temp_dir().join(format!("orx-fake-host-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("notify");
let mut fake = HostFile::default();
let mut idents: [Vec<FileIdent>; 2] = Default::default();
let mut name = |side: usize, ident: &FileIdent| {
let seen = &mut idents[side];
if !seen.contains(ident) {
seen.push(ident.clone());
}
seen.iter().position(|known| known == ident).unwrap()
};
let mut shape = |side: usize, page: std::io::Result<LogPage>| match page {
Err(_) => "error".to_string(),
Ok(LogPage::Reset { info }) => {
format!(
"reset {:?}",
info.map(|info| (name(side, &info.ident), info.len))
)
}
Ok(LogPage::Data {
len: _,
ident,
start,
bytes,
eof,
}) => {
format!("data {} {start} {bytes:?} {eof}", name(side, &ident))
}
};
let mut last: [Option<FileIdent>; 2] = [None, None];
let steps: [(&str, u64, u64, bool); 8] = [
("hello", 0, 5, false),
("hello world", 5, 11, true),
("hello world", 11, 11, true),
("hello world", 12, 11, true),
("hello world", 20, 30, true),
("jello world", 11, 11, true),
("bye", 0, 3, true),
("bye", 0, 3, false),
];
for (content, start, end, with_ident) in steps {
std::fs::write(&path, content).unwrap();
fake.bytes = content.as_bytes().to_vec();
let range = ByteRange { start, end };
let real =
orx_host::log::read(&path, last[0].as_ref().filter(|_| with_ident), &range, None);
let modeled = fake.read(3, last[1].as_ref().filter(|_| with_ident), &range);
for (side, page) in [
(0, &real.as_ref().ok().cloned()),
(1, &modeled.as_ref().ok().cloned()),
] {
if let Some(
LogPage::Data { ident, .. }
| LogPage::Reset {
info: Some(LogInfo { ident, .. }),
},
) = page
{
last[side] = Some(ident.clone());
}
}
let modeled = modeled.map_err(|error| std::io::Error::other(error.to_string()));
assert_eq!(
shape(0, real),
shape(1, modeled),
"{content:?} {start}..{end}"
);
}
std::fs::remove_dir_all(dir).unwrap();
}
}