Skip to main content

day_part_http/
simulation.rs

1// Copyright © The Daybrite Project
2// SPDX-License-Identifier: MPL-2.0
3
4//! Scoped, programmable HTTP and WebSocket interception. No sockets, UI or native stack needed.
5use crate::{
6    Head, HttpError, Message,
7    session::{lock, simulated_capabilities},
8    transport::{
9        Answer, Completion, Event, Events, Prepared, PreparedBody, QuestionId, Socket, Transfer,
10        Transport, WsEvent, WsEvents,
11    },
12};
13use std::{
14    collections::{HashMap, VecDeque},
15    sync::{Arc, Mutex, Weak},
16    time::Duration,
17};
18
19/// A scheduler usable from any executor. A manual clock makes fault tests independent of sleep.
20pub trait Clock: Send + Sync + 'static {
21    fn now(&self) -> Duration;
22    fn schedule(&self, delay: Duration, job: Box<dyn FnOnce() + Send>);
23}
24#[derive(Default)]
25pub struct RealClock;
26impl Clock for RealClock {
27    fn now(&self) -> Duration {
28        crate::statistics::now()
29    }
30    fn schedule(&self, delay: Duration, job: Box<dyn FnOnce() + Send>) {
31        #[cfg(not(target_arch = "wasm32"))]
32        {
33            day_async::schedule(delay, job);
34        }
35        #[cfg(target_arch = "wasm32")]
36        {
37            crate::bridge::simulation_delay(delay.as_secs_f64() * 1000.0, job);
38        }
39    }
40}
41type Job = Box<dyn FnOnce() + Send>;
42#[derive(Default)]
43struct Manual {
44    now: Duration,
45    next: u64,
46    jobs: Vec<(Duration, u64, Job)>,
47}
48/// Advances scheduled network events synchronously. Jobs at equal times run in insertion order.
49#[derive(Clone, Default)]
50pub struct ManualClock(Arc<Mutex<Manual>>);
51impl ManualClock {
52    pub fn advance(&self, by: Duration) {
53        let end = self.now() + by;
54        loop {
55            let job = {
56                let mut s = lock(&self.0);
57                let next = s
58                    .jobs
59                    .iter()
60                    .enumerate()
61                    .filter(|(_, j)| j.0 <= end)
62                    .min_by_key(|(_, j)| (j.0, j.1))
63                    .map(|(i, _)| i);
64                if let Some(i) = next {
65                    let (at, _, job) = s.jobs.remove(i);
66                    s.now = at;
67                    Some(job)
68                } else {
69                    s.now = end;
70                    None
71                }
72            };
73            match job {
74                Some(job) => job(),
75                None => break,
76            }
77        }
78    }
79}
80impl Clock for ManualClock {
81    fn now(&self) -> Duration {
82        lock(&self.0).now
83    }
84    fn schedule(&self, delay: Duration, job: Job) {
85        let mut s = lock(&self.0);
86        let at = s.now + delay;
87        let id = s.next;
88        s.next += 1;
89        s.jobs.push((at, id, job));
90    }
91}
92/// Application-visible link conditions, not packet-level emulation. Changes affect active
93/// transfers at their next delivery boundary. Pausing does not pause request timeout clocks.
94#[derive(Clone, Debug)]
95pub struct Conditions {
96    pub latency: Duration,
97    /// Payload bytes/sec, each direction per transfer. None means unlimited.
98    pub bytes_per_second: Option<u64>,
99    /// Probability in `[0, 1]` of failing a request/WS send. Seed and per-key ordinal determine it.
100    pub failure_rate: f64,
101    pub seed: u64,
102    pub paused: bool,
103    pub offline: bool,
104}
105impl Default for Conditions {
106    fn default() -> Self {
107        Self {
108            latency: Duration::ZERO,
109            bytes_per_second: None,
110            failure_rate: 0.0,
111            seed: 1,
112            paused: false,
113            offline: false,
114        }
115    }
116}
117/// A response generated by a handler. Body chunks are delivered only when the reader asks.
118#[derive(Clone, Debug)]
119pub struct Reply {
120    pub status: u16,
121    pub headers: Vec<(String, String)>,
122    pub chunks: Vec<Vec<u8>>,
123    pub delay: Duration,
124    /// Fail instead of completing, after this many body chunks (zero fails before the head).
125    pub fail_after_chunks: Option<usize>,
126}
127impl Reply {
128    pub fn new(status: u16, body: impl Into<Vec<u8>>) -> Self {
129        let body = body.into();
130        Self {
131            status,
132            headers: vec![],
133            chunks: body.chunks(16 * 1024).map(<[u8]>::to_vec).collect(),
134            delay: Duration::ZERO,
135            fail_after_chunks: None,
136        }
137    }
138    #[must_use]
139    pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
140        self.headers.push((name.into(), value.into()));
141        self
142    }
143}
144/// Captured request facts. Bodies are available to handlers through `PreparedBody`; they are
145/// not retained in history. Do not put credentials in URLs. History is bounded to 256 entries.
146#[derive(Clone, Debug)]
147pub struct RequestRecord {
148    pub method: String,
149    pub url: String,
150    pub ordinal: u64,
151    pub injected_failure: bool,
152}
153type Predicate = dyn Fn(&str, &str) -> bool + Send + Sync;
154type Handler = dyn Fn(Prepared) -> Result<Reply, HttpError> + Send + Sync;
155type WsHandler = dyn Fn(Message) -> Result<Message, HttpError> + Send + Sync;
156struct Rule {
157    matches: Arc<Predicate>,
158    handler: Arc<Handler>,
159}
160struct State {
161    rules: Vec<Rule>,
162    ws: Vec<(Arc<Predicate>, Arc<WsHandler>)>,
163    conditions: Conditions,
164    strict: bool,
165    counts: HashMap<String, u64>,
166    history: VecDeque<RequestRecord>,
167}
168struct Inner {
169    state: Mutex<State>,
170    clock: Arc<dyn Clock>,
171}
172#[derive(Clone)]
173pub struct Simulation(Arc<Inner>);
174impl Default for Simulation {
175    fn default() -> Self {
176        Self::new()
177    }
178}
179impl Simulation {
180    #[must_use]
181    pub fn new() -> Self {
182        Self::with_clock(Arc::new(RealClock))
183    }
184    pub fn with_clock(clock: Arc<dyn Clock>) -> Self {
185        Self(Arc::new(Inner {
186            state: Mutex::new(State {
187                rules: vec![],
188                ws: vec![],
189                conditions: Conditions::default(),
190                strict: true,
191                counts: HashMap::new(),
192                history: VecDeque::new(),
193            }),
194            clock,
195        }))
196    }
197    /// Strict by default: unmatched requests fail. Set false to deliberately pass them through.
198    pub fn set_strict(&self, strict: bool) {
199        lock(&self.0.state).strict = strict;
200    }
201    pub fn set_conditions(&self, mut conditions: Conditions) {
202        conditions.failure_rate = if conditions.failure_rate.is_finite() {
203            conditions.failure_rate.clamp(0.0, 1.0)
204        } else {
205            0.0
206        };
207        lock(&self.0.state).conditions = conditions;
208    }
209    #[must_use]
210    pub fn conditions(&self) -> Conditions {
211        lock(&self.0.state).conditions.clone()
212    }
213    /// First matching rule wins. Predicate receives method and full URL; match origins
214    /// explicitly rather than relying on a loose substring when passthrough is enabled.
215    pub fn handle(
216        &self,
217        matches: impl Fn(&str, &str) -> bool + Send + Sync + 'static,
218        handler: impl Fn(Prepared) -> Result<Reply, HttpError> + Send + Sync + 'static,
219    ) {
220        lock(&self.0.state).rules.push(Rule {
221            matches: Arc::new(matches),
222            handler: Arc::new(handler),
223        });
224    }
225    pub fn route(
226        &self,
227        method: &str,
228        url: &str,
229        handler: impl Fn(Prepared) -> Result<Reply, HttpError> + Send + Sync + 'static,
230    ) {
231        let method = method.to_owned();
232        let url = url.to_owned();
233        self.handle(move |m, u| m == method && u == url, handler);
234    }
235    pub fn websocket(
236        &self,
237        matches: impl Fn(&str, &str) -> bool + Send + Sync + 'static,
238        handler: impl Fn(Message) -> Result<Message, HttpError> + Send + Sync + 'static,
239    ) {
240        lock(&self.0.state)
241            .ws
242            .push((Arc::new(matches), Arc::new(handler)));
243    }
244    pub fn websocket_echo(&self, url: &str) {
245        let url = url.to_owned();
246        self.websocket(move |_, u| u == url, Ok);
247    }
248    #[must_use]
249    pub fn requests(&self) -> Vec<RequestRecord> {
250        lock(&self.0.state).history.iter().cloned().collect()
251    }
252    #[must_use]
253    pub fn request_count(&self, method: &str, url: &str) -> u64 {
254        *lock(&self.0.state)
255            .counts
256            .get(&format!("{method} {url}"))
257            .unwrap_or(&0)
258    }
259    pub(crate) fn intercepts(&self, method: &str, url: &str) -> bool {
260        let (strict, rules) = {
261            let s = lock(&self.0.state);
262            (
263                s.strict,
264                s.rules
265                    .iter()
266                    .map(|r| r.matches.clone())
267                    .chain(s.ws.iter().map(|r| r.0.clone()))
268                    .collect::<Vec<_>>(),
269            )
270        };
271        strict || rules.iter().any(|m| m(method, url))
272    }
273    pub(crate) fn clock(&self) -> Arc<dyn Clock> {
274        self.0.clock.clone()
275    }
276    pub(crate) fn transport(&self) -> Arc<dyn Transport> {
277        Arc::new(self.clone())
278    }
279    fn fault(&self, method: &str, url: &str) -> bool {
280        let mut s = lock(&self.0.state);
281        let key = format!("{method} {url}");
282        let ordinal = {
283            let n = s.counts.entry(key.clone()).or_default();
284            *n += 1;
285            *n
286        };
287        // FNV key followed by SplitMix64. Different URLs do not consume each other's stream.
288        let mut x = key.bytes().fold(0xcbf2_9ce4_8422_2325_u64, |h, b| {
289            (h ^ u64::from(b)).wrapping_mul(0x0100_0000_01b3)
290        }) ^ s.conditions.seed
291            ^ ordinal.wrapping_mul(0x9e37_79b9_7f4a_7c15);
292        x = (x ^ (x >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
293        x = (x ^ (x >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
294        x ^= x >> 31;
295        let fail = s.conditions.offline
296            || (x >> 11) as f64 / ((1u64 << 53) as f64) < s.conditions.failure_rate;
297        s.history.push_back(RequestRecord {
298            method: method.into(),
299            url: url.into(),
300            ordinal,
301            injected_failure: fail,
302        });
303        if s.history.len() > 256 {
304            s.history.pop_front();
305        }
306        fail
307    }
308}
309fn fault_error() -> HttpError {
310    HttpError::Io("simulated connection failure".into())
311}
312fn delay(c: &Conditions, bytes: u64) -> Duration {
313    c.bytes_per_second
314        .map_or_default(|rate| Duration::from_secs_f64(bytes as f64 / rate.max(1) as f64))
315}
316impl Transport for Simulation {
317    fn capabilities(&self) -> crate::Capabilities {
318        simulated_capabilities()
319    }
320    fn start(&self, req: Prepared, events: Events) -> Arc<dyn Transfer> {
321        let failed = self.fault(&req.method, &req.url);
322        let url = req.url.clone();
323        let timeout = req.timeout_idle;
324        let uploaded = match &req.body {
325            PreparedBody::Bytes(b) => b.len() as u64,
326            PreparedBody::File { len, .. } => *len,
327            PreparedBody::Stream { len, .. } => len.unwrap_or(0),
328            PreparedBody::Empty => 0,
329        };
330        let rules = lock(&self.0.state)
331            .rules
332            .iter()
333            .map(|r| (r.matches.clone(), r.handler.clone()))
334            .collect::<Vec<_>>();
335        let bodyless = req.method == "HEAD";
336        let result = if failed {
337            Err(fault_error())
338        } else {
339            rules
340                .into_iter()
341                .find(|(m, _)| m(&req.method, &req.url))
342                .map_or_else(
343                    || Err(HttpError::Io(format!("unexpected HTTP request: {url}"))),
344                    |(_, h)| h(req),
345                )
346        };
347        let result = result.map(|mut reply| {
348            if bodyless || matches!(reply.status, 204 | 304) {
349                reply.chunks.clear();
350            }
351            reply
352        });
353        let initial = self.conditions().latency
354            + delay(&self.conditions(), uploaded)
355            + result.as_ref().map_or_default(|r| r.delay);
356        let transfer = Arc::new_cyclic(|weak| SimTransfer {
357            this: weak.clone(),
358            sim: self.clone(),
359            events,
360            state: Mutex::new(HttpState {
361                result: Some(result),
362                chunks: VecDeque::new(),
363                head: false,
364                demand: 0,
365                over: false,
366                busy: true,
367                sent: 0,
368                fail_after: None,
369                last_activity: self.0.clock.now(),
370            }),
371            url,
372            uploaded,
373            timeout,
374        });
375        let weak = Arc::downgrade(&transfer);
376        self.0.clock.schedule(
377            initial,
378            Box::new(move || {
379                if let Some(t) = weak.upgrade() {
380                    t.deliver();
381                }
382            }),
383        );
384        transfer.watch_timeout(timeout);
385        transfer
386    }
387    fn websocket(&self, req: Prepared, events: WsEvents) -> Arc<dyn Socket> {
388        let rules = lock(&self.0.state).ws.clone();
389        let handler = rules
390            .into_iter()
391            .find(|(m, _)| m(&req.method, &req.url))
392            .map(|(_, h)| h);
393        let failed = self.fault(&req.method, &req.url);
394        let socket = Arc::new_cyclic(|weak| SimSocket {
395            this: weak.clone(),
396            sim: self.clone(),
397            events,
398            handler,
399            state: Mutex::new(WsState {
400                queue: VecDeque::new(),
401                demand: 0,
402                over: false,
403                busy: false,
404            }),
405            url: req.url,
406        });
407        socket.open_after(
408            self.conditions().latency,
409            req.protocols.first().cloned(),
410            failed,
411            self.0.clock.now() + req.timeout_idle,
412        );
413        socket
414    }
415}
416struct HttpState {
417    result: Option<Result<Reply, HttpError>>,
418    chunks: VecDeque<Vec<u8>>,
419    head: bool,
420    demand: u32,
421    over: bool,
422    busy: bool,
423    sent: usize,
424    fail_after: Option<usize>,
425    last_activity: Duration,
426}
427struct SimTransfer {
428    this: Weak<Self>,
429    sim: Simulation,
430    events: Events,
431    state: Mutex<HttpState>,
432    url: String,
433    uploaded: u64,
434    timeout: Duration,
435}
436impl SimTransfer {
437    fn watch_timeout(&self, delay: Duration) {
438        let weak = self.this.clone();
439        self.sim.0.clock.schedule(
440            delay,
441            Box::new(move || {
442                if let Some(t) = weak.upgrade() {
443                    let (over, elapsed) = {
444                        let s = lock(&t.state);
445                        (s.over, t.sim.0.clock.now().saturating_sub(s.last_activity))
446                    };
447                    if over {
448                        return;
449                    }
450                    if elapsed >= t.timeout {
451                        t.finish(HttpError::Timeout);
452                    } else {
453                        t.watch_timeout(t.timeout.checked_sub(elapsed).unwrap());
454                    }
455                }
456            }),
457        );
458    }
459
460    fn finish(&self, error: HttpError) {
461        let notify = {
462            let mut s = lock(&self.state);
463            if s.over {
464                false
465            } else {
466                s.over = true;
467                s.chunks.clear();
468                s.result = None;
469                true
470            }
471        };
472        if notify {
473            (self.events)(Event::Failed(error));
474        }
475    }
476    fn schedule(&self, wait: Duration) {
477        let weak = self.this.clone();
478        self.sim.0.clock.schedule(
479            wait,
480            Box::new(move || {
481                if let Some(t) = weak.upgrade() {
482                    t.deliver();
483                }
484            }),
485        );
486    }
487    fn deliver(&self) {
488        if lock(&self.state).over {
489            return;
490        }
491        let c = self.sim.conditions();
492        if c.offline {
493            return self.finish(fault_error());
494        }
495        if c.paused {
496            self.schedule(Duration::from_millis(10));
497            return;
498        }
499        let event = {
500            let mut s = lock(&self.state);
501            s.busy = false;
502            if let Some(result) = s.result.take() {
503                match result {
504                    Err(e) => {
505                        s.over = true;
506                        Event::Failed(e)
507                    }
508                    Ok(r) => {
509                        s.fail_after = r.fail_after_chunks;
510                        if s.fail_after == Some(0) {
511                            s.over = true;
512                            Event::Failed(fault_error())
513                        } else {
514                            let expected = Some(r.chunks.iter().map(|b| b.len() as u64).sum());
515                            s.chunks = r.chunks.into();
516                            s.head = true;
517                            Event::Head(Head {
518                                status: r.status,
519                                headers: r.headers,
520                                url: self.url.clone(),
521                                expected_length: expected,
522                            })
523                        }
524                    }
525                }
526            } else if s.fail_after == Some(s.sent) {
527                s.over = true;
528                s.chunks.clear();
529                Event::Failed(fault_error())
530            } else if s.chunks.is_empty() {
531                s.over = true;
532                Event::End
533            } else if s.demand > 0 {
534                s.demand -= 1;
535                s.sent += 1;
536                Event::Chunk(s.chunks.pop_front().unwrap())
537            } else {
538                return;
539            }
540        };
541        if matches!(event, Event::Head(_)) && self.uploaded > 0 {
542            (self.events)(Event::Sent {
543                sent: self.uploaded,
544                total: Some(self.uploaded),
545            });
546        }
547        lock(&self.state).last_activity = self.sim.0.clock.now();
548        (self.events)(event);
549        self.kick();
550    }
551    fn kick(&self) {
552        let c = self.sim.conditions();
553        let wait = {
554            let mut s = lock(&self.state);
555            if s.over || s.busy || !s.head || (!s.chunks.is_empty() && s.demand == 0) {
556                return;
557            }
558            s.busy = true;
559            delay(&c, s.chunks.front().map_or(0, |b| b.len() as u64))
560        };
561        self.schedule(wait);
562    }
563}
564impl Transfer for SimTransfer {
565    fn demand(&self, n: u32) {
566        let mut s = lock(&self.state);
567        s.demand = s.demand.saturating_add(n);
568        drop(s);
569        self.kick();
570    }
571    fn answer(&self, _: QuestionId, _: Answer) {}
572    fn cancel(&self) {
573        self.finish(HttpError::Cancelled);
574    }
575}
576struct WsState {
577    queue: VecDeque<Message>,
578    demand: u32,
579    over: bool,
580    busy: bool,
581}
582struct SimSocket {
583    this: Weak<Self>,
584    sim: Simulation,
585    events: WsEvents,
586    handler: Option<Arc<WsHandler>>,
587    state: Mutex<WsState>,
588    url: String,
589}
590impl SimSocket {
591    fn open_after(
592        &self,
593        delay: Duration,
594        protocol: Option<String>,
595        failed: bool,
596        deadline: Duration,
597    ) {
598        let weak = self.this.clone();
599        self.sim.0.clock.schedule(
600            delay,
601            Box::new(move || {
602                if let Some(s) = weak.upgrade() {
603                    if lock(&s.state).over {
604                        return;
605                    }
606                    let conditions = s.sim.conditions();
607                    if s.sim.0.clock.now() >= deadline {
608                        s.fail(HttpError::Timeout);
609                    } else if failed || conditions.offline || s.handler.is_none() {
610                        s.fail(fault_error());
611                    } else if conditions.paused {
612                        s.open_after(Duration::from_millis(10), protocol, failed, deadline);
613                    } else {
614                        (s.events)(WsEvent::Open { protocol });
615                    }
616                }
617            }),
618        );
619    }
620
621    fn fail(&self, e: HttpError) {
622        let mut s = lock(&self.state);
623        if s.over {
624            return;
625        }
626        s.over = true;
627        s.queue.clear();
628        drop(s);
629        (self.events)(WsEvent::Failed(e));
630    }
631    fn kick(&self) {
632        let wait = {
633            let mut s = lock(&self.state);
634            if s.over || s.busy || s.demand == 0 || s.queue.is_empty() {
635                return;
636            }
637            s.busy = true;
638            delay(
639                &self.sim.conditions(),
640                crate::statistics::message_len(s.queue.front().unwrap()),
641            )
642        };
643        let weak = self.this.clone();
644        self.sim.0.clock.schedule(
645            wait,
646            Box::new(move || {
647                if let Some(s) = weak.upgrade() {
648                    s.deliver();
649                }
650            }),
651        );
652    }
653    fn deliver(&self) {
654        let c = self.sim.conditions();
655        if c.offline {
656            return self.fail(fault_error());
657        }
658        if c.paused {
659            let weak = self.this.clone();
660            self.sim.0.clock.schedule(
661                Duration::from_millis(10),
662                Box::new(move || {
663                    if let Some(s) = weak.upgrade() {
664                        s.deliver();
665                    }
666                }),
667            );
668            return;
669        }
670        let msg = {
671            let mut s = lock(&self.state);
672            s.busy = false;
673            if s.over || s.demand == 0 {
674                return;
675            }
676            s.demand -= 1;
677            s.queue.pop_front()
678        };
679        if let Some(m) = msg {
680            (self.events)(WsEvent::Message(m));
681        }
682        self.kick();
683    }
684}
685impl Socket for SimSocket {
686    fn send(&self, message: Message, done: Completion) {
687        if lock(&self.state).over {
688            done(Err(HttpError::Cancelled));
689            return;
690        }
691        if self.sim.fault("WS-SEND", &self.url) {
692            self.fail(fault_error());
693            done(Err(fault_error()));
694            return;
695        }
696        match self
697            .handler
698            .as_ref()
699            .ok_or(HttpError::Unsupported)
700            .and_then(|h| h(message))
701        {
702            Ok(reply) => {
703                if let Message::Close { code, reason } = reply {
704                    self.close(code, &reason);
705                    done(Ok(()));
706                    return;
707                }
708                let mut s = lock(&self.state);
709                if s.queue.len() >= 64 {
710                    drop(s);
711                    done(Err(HttpError::Io("simulated WebSocket queue full".into())));
712                    return;
713                }
714                s.queue.push_back(reply);
715                drop(s);
716                done(Ok(()));
717                self.kick();
718            }
719            Err(e) => {
720                self.fail(e.clone());
721                done(Err(e));
722            }
723        }
724    }
725    fn ping(&self, done: Completion) {
726        done(if lock(&self.state).over {
727            Err(HttpError::Cancelled)
728        } else {
729            Ok(())
730        });
731    }
732    fn close(&self, code: u16, reason: &str) {
733        let mut s = lock(&self.state);
734        if s.over {
735            return;
736        }
737        s.over = true;
738        s.queue.clear();
739        drop(s);
740        (self.events)(WsEvent::Closed {
741            code,
742            reason: reason.into(),
743        });
744    }
745    fn demand(&self, n: u32) {
746        let mut s = lock(&self.state);
747        s.demand = s.demand.saturating_add(n);
748        drop(s);
749        self.kick();
750    }
751}