rift-mock-core 0.25.0

Rift core engine: imposter lifecycle, matching, behaviors, scripting and flow-state — usable in-process without the CLI or admin server.
Documentation
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! An imposter's own clock (issue #1754): wall time until a test freezes or steps it.
//!
//! A test whose code runs on a fake clock (ZIO `TestClock`, a simulation's virtual time) can move
//! the mock's time with it: `PUT /imposters/{port}/clock` freezes it at an instant or steps it
//! forward, and with `_rift.scriptEngine.clock: "header"` one request can name its own instant in
//! `X-Rift-Now`. What reads it: the built-in flow store's TTLs, a wasm component's `wasi:clocks`,
//! and (#1756, through `scripting::env`) Rhai's `timestamp_header()`, JavaScript's `Date` and the
//! template `now`. What deliberately does not: `Last-Modified` and `LoadClock` (HTTP cache semantics
//! are about real time), journal timestamps (observability), scenario transition stamps, proxy
//! latency and timers.
//!
//! Runtime state, like script state: never persisted, wall again on restart and on recreate.

use arc_swap::ArcSwap;
use std::time::{Duration, SystemTime};

/// Where an imposter's time comes from.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClockMode {
    /// The system clock.
    Wall,
    /// Stopped at this instant, in milliseconds since the Unix epoch, until set again.
    Frozen { now_ms: i64 },
}

/// Why a clock change was refused.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ClockError {
    #[error("advanceMs must not be negative (got {0}); set nowMs to go back")]
    NegativeAdvance(i64),
    #[error("the clock cannot be advanced past the latest instant it holds")]
    Overflow,
    #[error("{0} ms is outside the instants the clock holds (years 1 to 9999)")]
    OutOfRange(i64),
}

/// The earliest instant a clock holds, in milliseconds since the Unix epoch: year 1. With
/// [`MAX_MS`] (year 9999), a range every platform's `SystemTime` can represent, where Windows'
/// cannot hold most of `i64` milliseconds.
pub const MIN_MS: i64 = -62_135_596_800_000;
/// The latest instant a clock holds: the end of year 9999.
pub const MAX_MS: i64 = 253_402_300_799_999;

fn in_range(ms: i64) -> Result<i64, ClockError> {
    if (MIN_MS..=MAX_MS).contains(&ms) {
        Ok(ms)
    } else {
        Err(ClockError::OutOfRange(ms))
    }
}

/// One imposter's clock.
#[derive(Debug)]
pub struct ImposterClock {
    mode: ArcSwap<ClockMode>,
}

impl Default for ImposterClock {
    fn default() -> Self {
        Self {
            mode: ArcSwap::from_pointee(ClockMode::Wall),
        }
    }
}

/// The system clock in milliseconds since the Unix epoch.
#[must_use]
pub fn wall_ms() -> i64 {
    let since = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or(Duration::ZERO);
    i64::try_from(since.as_millis()).unwrap_or(i64::MAX)
}

/// `ms` since the Unix epoch as a [`SystemTime`]; a negative instant is before the epoch. Every
/// instant a clock holds fits; one outside [`MIN_MS`]..=[`MAX_MS`] (never stored) reads as the
/// epoch rather than panicking.
#[must_use]
pub fn system_time_of(ms: i64) -> SystemTime {
    let magnitude = Duration::from_millis(ms.unsigned_abs());
    let at = if ms >= 0 {
        SystemTime::UNIX_EPOCH.checked_add(magnitude)
    } else {
        SystemTime::UNIX_EPOCH.checked_sub(magnitude)
    };
    at.unwrap_or(SystemTime::UNIX_EPOCH)
}

impl ImposterClock {
    #[must_use]
    pub fn mode(&self) -> ClockMode {
        **self.mode.load()
    }

    /// The current instant, in milliseconds since the Unix epoch.
    #[must_use]
    pub fn now_ms(&self) -> i64 {
        match self.mode() {
            ClockMode::Wall => wall_ms(),
            ClockMode::Frozen { now_ms } => now_ms,
        }
    }

    /// Stop the clock at `now_ms`, which must be within [`MIN_MS`]..=[`MAX_MS`].
    pub fn freeze(&self, now_ms: i64) -> Result<ClockMode, ClockError> {
        let mode = ClockMode::Frozen {
            now_ms: in_range(now_ms)?,
        };
        self.mode.store(std::sync::Arc::new(mode));
        Ok(mode)
    }

    /// Step the clock forward by `ms`: a frozen clock moves by `ms`, a wall clock freezes at wall
    /// time plus `ms`. Atomic against a concurrent step: two advances add up.
    pub fn advance(&self, ms: i64) -> Result<ClockMode, ClockError> {
        if ms < 0 {
            return Err(ClockError::NegativeAdvance(ms));
        }
        let mut out = Err(ClockError::Overflow);
        self.mode.rcu(|current| {
            let base = match **current {
                ClockMode::Wall => wall_ms(),
                ClockMode::Frozen { now_ms } => now_ms,
            };
            match base.checked_add(ms).filter(|next| *next <= MAX_MS) {
                Some(now_ms) => {
                    out = Ok(ClockMode::Frozen { now_ms });
                    std::sync::Arc::new(ClockMode::Frozen { now_ms })
                }
                None => {
                    out = Err(ClockError::Overflow);
                    std::sync::Arc::clone(current)
                }
            }
        });
        out
    }

    /// Back to the system clock.
    pub fn reset(&self) {
        self.mode.store(std::sync::Arc::new(ClockMode::Wall));
    }
}

/// `_rift.scriptEngine.clock`: whether a request may name its own instant (issue #1754).
///
/// Decoded tolerantly: engines before #1754 admitted any value under this key and ignored it, so a
/// stored config carrying one must still replay (rift-cluster's log, `--datadir`). Any other string
/// decodes as [`Self::Unrecognized`], runs as `wall`, and is logged when the imposter is built.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "lowercase")]
pub enum ClockSource {
    /// The imposter's clock only; `X-Rift-Now` is ignored.
    #[default]
    Wall,
    /// A request's `X-Rift-Now: <ms since epoch>` is its own instant, ahead of the imposter's clock.
    Header,
    /// Any other string: runs as `wall`.
    #[serde(other)]
    #[cfg_attr(feature = "schema", schemars(skip))]
    Unrecognized,
}

/// Where a recorded request's `_clockMs` came from (issue #1796). Only a request whose instant is not
/// the wall clock carries one: a wall-clock imposter records exactly what it did before.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RecordedClock {
    /// The imposter's clock was frozen (or stepped) when the request arrived.
    Frozen,
    /// The request named its own instant in `X-Rift-Now`.
    Header,
    /// A value a later release wrote: kept decodable, as a journal outlives the engine that wrote it.
    #[serde(other)]
    Unrecognized,
}

/// The instant a request arrives at, when it is not the wall clock (issue #1796): its own
/// `X-Rift-Now`, else the imposter's frozen clock, else `None`. Read on the request's task.
#[must_use]
pub fn recorded_instant(clock: &ImposterClock) -> Option<(i64, RecordedClock)> {
    match request_override() {
        Some(ms) => Some((ms, RecordedClock::Header)),
        None => match clock.mode() {
            ClockMode::Wall => None,
            ClockMode::Frozen { now_ms } => Some((now_ms, RecordedClock::Frozen)),
        },
    }
}

/// The header a request names its own instant in, under `clock: "header"`.
pub const NOW_HEADER: &str = "x-rift-now";

/// An `X-Rift-Now` that is not a whole number of milliseconds within the clock's range.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error(
    "X-Rift-Now must be milliseconds since the Unix epoch, an integer from {MIN_MS} to {MAX_MS} \
     (got {0:?})"
)]
pub struct BadNowHeader(pub String);

/// A request's own instant (issue #1754): its `X-Rift-Now` under `clock: "header"`, else `None`.
/// Resolved once, when the request is admitted. Everything else reads the imposter's clock live, so
/// a step taken while the request runs is seen; a request's own instant stays fixed.
pub fn resolve_request_now(
    source: ClockSource,
    header: Option<&str>,
) -> Result<Option<i64>, BadNowHeader> {
    match (source, header) {
        (ClockSource::Header, Some(raw)) => raw
            .trim()
            .parse::<i64>()
            .ok()
            .filter(|ms| in_range(*ms).is_ok())
            .map(Some)
            .ok_or_else(|| BadNowHeader(raw.to_owned())),
        _ => Ok(None),
    }
}

tokio::task_local! {
    /// The instant the request this task serves named in `X-Rift-Now`, if it named one.
    static REQUEST_NOW: i64;
    /// The clock of the imposter serving the request on this task (issue #1756), so a script
    /// reads it without the imposter in hand.
    static IMPOSTER_CLOCK: std::sync::Arc<ImposterClock>;
}

std::thread_local! {
    /// The same, for a blocking-pool thread running a request's work.
    static THREAD_CLOCK: std::cell::RefCell<Option<std::sync::Arc<ImposterClock>>> =
        const { std::cell::RefCell::new(None) };
}

/// The clock of the imposter whose request is being served here, on its task or on a blocking
/// thread running its work.
#[must_use]
pub fn current_imposter_clock() -> Option<std::sync::Arc<ImposterClock>> {
    IMPOSTER_CLOCK
        .try_with(std::sync::Arc::clone)
        .ok()
        .or_else(|| THREAD_CLOCK.with(|slot| slot.borrow().clone()))
}

/// Run `fut` with `clock` as its imposter's clock.
pub async fn with_imposter_clock<F: std::future::Future>(
    clock: std::sync::Arc<ImposterClock>,
    fut: F,
) -> F::Output {
    IMPOSTER_CLOCK.scope(clock, fut).await
}

/// Run `f` on this thread with `clock` as [`current_imposter_clock`]; the thread's previous one
/// comes back after, a panicking `f` included.
pub fn carry_imposter_clock<T>(
    clock: Option<std::sync::Arc<ImposterClock>>,
    f: impl FnOnce() -> T,
) -> T {
    struct Restore(Option<Option<std::sync::Arc<ImposterClock>>>);
    impl Drop for Restore {
        fn drop(&mut self) {
            if let Some(previous) = self.0.take() {
                THREAD_CLOCK.with(|slot| *slot.borrow_mut() = previous);
            }
        }
    }
    let _restore = Restore(Some(THREAD_CLOCK.with(|slot| slot.replace(clock))));
    f()
}

std::thread_local! {
    /// The same, for a blocking-pool thread running a request's work.
    static THREAD_NOW: std::cell::Cell<Option<i64>> = const { std::cell::Cell::new(None) };
}

/// The `X-Rift-Now` instant of the request being served here, on its task or on a blocking thread
/// running its work.
#[must_use]
pub fn request_override() -> Option<i64> {
    REQUEST_NOW
        .try_with(|ms| *ms)
        .ok()
        .or_else(|| THREAD_NOW.with(std::cell::Cell::get))
}

/// Run `fut` with `ms` as its request's instant; `None` runs it as it is.
pub async fn with_request_override<F: std::future::Future>(ms: Option<i64>, fut: F) -> F::Output {
    match ms {
        Some(ms) => REQUEST_NOW.scope(ms, fut).await,
        None => fut.await,
    }
}

/// Run `f` on this thread with the instant [`request_override`] reads on the calling task, for a
/// closure handed to the blocking pool.
pub fn carry_request_override<T>(ms: Option<i64>, f: impl FnOnce() -> T) -> T {
    /// Puts the thread's previous instant back, a panicking `f` included: the blocking pool reuses
    /// the thread for another request.
    struct Restore(Option<i64>);
    impl Drop for Restore {
        fn drop(&mut self) {
            THREAD_NOW.with(|slot| slot.set(self.0));
        }
    }
    let _restore = Restore(THREAD_NOW.with(|slot| slot.replace(ms)));
    f()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn freeze_advance_and_reset() {
        let clock = ImposterClock::default();
        assert_eq!(clock.mode(), ClockMode::Wall);
        let before = wall_ms();
        assert!(clock.now_ms() >= before);
        assert_eq!(
            clock.freeze(1_700_000_000_000),
            Ok(ClockMode::Frozen {
                now_ms: 1_700_000_000_000
            })
        );
        assert_eq!(clock.now_ms(), 1_700_000_000_000);
        assert_eq!(
            clock.advance(86_400_000),
            Ok(ClockMode::Frozen {
                now_ms: 1_700_086_400_000
            })
        );
        assert_eq!(clock.now_ms(), 1_700_086_400_000);
        assert_eq!(clock.advance(-1), Err(ClockError::NegativeAdvance(-1)));
        clock.freeze(MAX_MS - 1).expect("in range");
        assert_eq!(clock.advance(2), Err(ClockError::Overflow));
        assert_eq!(
            clock.now_ms(),
            MAX_MS - 1,
            "a refused advance changes nothing"
        );
        assert_eq!(
            clock.freeze(MAX_MS + 1),
            Err(ClockError::OutOfRange(MAX_MS + 1))
        );
        assert_eq!(
            clock.freeze(i64::MIN),
            Err(ClockError::OutOfRange(i64::MIN))
        );
        assert_eq!(
            clock.now_ms(),
            MAX_MS - 1,
            "a refused freeze changes nothing"
        );
        assert!(clock.freeze(MIN_MS).is_ok());
        clock.reset();
        assert_eq!(clock.mode(), ClockMode::Wall);
    }

    // On a wall clock, an advance freezes at wall time plus the step.
    #[test]
    fn advancing_a_wall_clock_freezes_it_ahead() {
        let clock = ImposterClock::default();
        let before = wall_ms();
        let ClockMode::Frozen { now_ms } = clock.advance(60_000).expect("advance") else {
            panic!("an advanced clock is frozen");
        };
        assert!(
            now_ms >= before + 60_000 && now_ms <= wall_ms() + 60_000,
            "{now_ms}"
        );
    }

    #[test]
    fn only_a_header_in_header_mode_is_a_request_instant() {
        assert_eq!(resolve_request_now(ClockSource::Header, None), Ok(None));
        assert_eq!(
            resolve_request_now(ClockSource::Header, Some("1700000000000")),
            Ok(Some(1_700_000_000_000))
        );
        // `clock: "wall"` ignores the header, malformed or not; so does an unrecognized mode.
        assert_eq!(resolve_request_now(ClockSource::Wall, Some("7")), Ok(None));
        assert_eq!(
            resolve_request_now(ClockSource::Wall, Some("soon")),
            Ok(None)
        );
        assert_eq!(
            resolve_request_now(ClockSource::Unrecognized, Some("7")),
            Ok(None)
        );
        for bad in ["soon", "1.5", "9223372036854775807", "-62135596800001"] {
            assert_eq!(
                resolve_request_now(ClockSource::Header, Some(bad)),
                Err(BadNowHeader(bad.into())),
                "{bad}"
            );
        }
    }

    // An unknown `clock` value decodes, so a stored config that carries one replays.
    #[test]
    fn an_unknown_clock_source_decodes_as_unrecognized() {
        let source: ClockSource =
            serde_json::from_value(serde_json::json!("virtual")).expect("decodes");
        assert_eq!(source, ClockSource::Unrecognized);
        let header: ClockSource =
            serde_json::from_value(serde_json::json!("header")).expect("decodes");
        assert_eq!(header, ClockSource::Header);
    }

    #[test]
    fn system_time_of_handles_both_sides_of_the_epoch() {
        assert_eq!(
            system_time_of(1_000)
                .duration_since(SystemTime::UNIX_EPOCH)
                .expect("after"),
            Duration::from_secs(1)
        );
        assert_eq!(
            SystemTime::UNIX_EPOCH
                .duration_since(system_time_of(-1_000))
                .expect("before"),
            Duration::from_secs(1)
        );
        // Out of every platform's range: the epoch, not a panic.
        let _ = system_time_of(i64::MAX);
        let _ = system_time_of(i64::MIN);
    }

    #[tokio::test]
    async fn a_request_override_reaches_its_task_and_a_carried_thread_only() {
        assert_eq!(request_override(), None);
        let seen = with_request_override(Some(42), async {
            let on_task = request_override();
            let carried = request_override();
            let on_thread = tokio::task::spawn_blocking(move || {
                carry_request_override(carried, request_override)
            })
            .await
            .expect("join");
            let uncarried = tokio::task::spawn_blocking(request_override)
                .await
                .expect("join");
            (on_task, on_thread, uncarried)
        })
        .await;
        assert_eq!(seen, (Some(42), Some(42), None));
        assert_eq!(request_override(), None);
    }

    // A panicking closure on a blocking thread does not leave its instant for the next job there.
    #[test]
    fn a_panicking_carry_restores_the_thread() {
        let caught = std::panic::catch_unwind(|| {
            carry_request_override(Some(9), || -> () { panic!("boom") })
        });
        assert!(caught.is_err());
        assert_eq!(request_override(), None);
    }
}