aimdb-core 3.0.0

Type-safe async data pipelines — one Rust codebase from MCU to cloud
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
use super::{capture, report, SessionEvent};
use crate::session::*;
use alloc::{boxed::Box, collections::VecDeque, sync::Arc, vec, vec::Vec};
use core::future::{pending, Future};
use std::sync::Mutex;

type Seen = Arc<Mutex<Vec<(&'static str, u64)>>>;
fn observer() -> (capture::Observer, Seen) {
    let seen: Seen = Arc::new(Mutex::new(Vec::new()));
    let out = seen.clone();
    let observer = Arc::new(move |event: &SessionEvent<'_>| {
        let entry = match event {
            SessionEvent::HandshakeReset {
                ended_subscriptions,
            } => ("reset", *ended_subscriptions as u64),
            SessionEvent::SubscribeRefused { id } => {
                assert_eq!(*id, "1");
                ("refused", 1)
            }
            SessionEvent::SubscriptionEnded { id } => {
                assert_eq!(*id, "2");
                ("ended", 2)
            }
            SessionEvent::SnapshotEncodeFailed { id, topic, error } => {
                assert_eq!((*id, *topic), ("3", "snapshot"));
                assert_eq!(**error, CodecError::Malformed);
                ("snapshot", 3)
            }
            SessionEvent::AcceptFailed { error } => {
                assert_eq!(*error, TransportError::Io);
                ("failed", 1)
            }
            SessionEvent::AcceptStillFailing { attempts, error } => {
                assert_eq!(*error, TransportError::Io);
                ("failing", *attempts)
            }
            SessionEvent::AcceptRecovered { after } => ("recovered", *after),
            SessionEvent::ConnectionLimitReached { limit } => ("limit", *limit as u64),
            SessionEvent::ListenerClosed { open_connections } => {
                ("closed", *open_connections as u64)
            }
            SessionEvent::AuthenticationRejected { error } => {
                assert_eq!(**error, AuthError::Unauthorized);
                ("auth", 0)
            }
            SessionEvent::DialFailed { error } => {
                assert_eq!(**error, TransportError::Io);
                ("dial", 0)
            }
            SessionEvent::ReconnectExhausted { attempts } => ("exhausted", *attempts as u64),
            _ => panic!("unexpected event: {event:?}"),
        };
        out.lock().unwrap().push(entry);
    });
    (observer, seen)
}

struct ScriptConnection {
    frames: VecDeque<u8>,
    peer: PeerInfo,
}
impl ScriptConnection {
    fn boxed(frames: &[u8]) -> Box<dyn Connection> {
        Box::new(Self {
            frames: frames.iter().copied().collect(),
            peer: PeerInfo::default(),
        })
    }
}
impl Connection for ScriptConnection {
    fn recv(&mut self) -> BoxFut<'_, TransportResult<Option<Vec<u8>>>> {
        Box::pin(async move {
            // Give subscription pumps a turn between scripted inbound frames.
            tokio::task::yield_now().await;
            Ok(self.frames.pop_front().map(|b| vec![b]))
        })
    }
    fn send<'a>(&'a mut self, _: &'a [u8]) -> BoxFut<'a, TransportResult<()>> {
        Box::pin(async { Ok(()) })
    }
    fn peer(&self) -> &PeerInfo {
        &self.peer
    }
}
struct ScriptCodec;
impl EnvelopeCodec for ScriptCodec {
    fn decode(&self, frame: &[u8]) -> Result<Inbound, CodecError> {
        Ok(match frame[0] {
            b'h' => Inbound::Request {
                id: 0,
                method: "hello".into(),
                params: Arc::from([]),
            },
            b's' => Inbound::Subscribe {
                id: 1,
                topic: "live".into(),
            },
            b'e' => Inbound::Subscribe {
                id: 2,
                topic: "empty".into(),
            },
            b'x' => Inbound::Subscribe {
                id: 3,
                topic: "snapshot".into(),
            },
            _ => return Err(CodecError::Malformed),
        })
    }
    fn encode(&self, msg: Outbound<'_>, _: &mut Vec<u8>) -> Result<(), CodecError> {
        if matches!(msg, Outbound::Snapshot { .. }) {
            Err(CodecError::Malformed)
        } else {
            Ok(())
        }
    }
    fn encode_inbound(&self, _: Inbound, _: &mut Vec<u8>) -> Result<(), CodecError> {
        Ok(())
    }
    fn decode_outbound<'a>(&self, _: &'a [u8]) -> Result<Outbound<'a>, CodecError> {
        Err(CodecError::Malformed)
    }
}
struct ScriptDispatch {
    reject: bool,
}
impl Dispatch for ScriptDispatch {
    fn authenticate<'a>(
        &'a self,
        _: &'a PeerInfo,
        _: Option<&'a [u8]>,
    ) -> BoxFut<'a, Result<SessionCtx, AuthError>> {
        Box::pin(async move {
            if self.reject {
                Err(AuthError::Unauthorized)
            } else {
                Ok(SessionCtx::default())
            }
        })
    }
    fn open(&self, _: &SessionCtx) -> Box<dyn Session> {
        Box::new(ScriptSession)
    }
}
struct ScriptSession;
impl Session for ScriptSession {
    fn call<'a>(
        &'a mut self,
        _: &'a str,
        params: Payload,
    ) -> BoxFut<'a, Result<Payload, RpcError>> {
        Box::pin(async { Ok(params) })
    }
    fn starts_session(&self, method: &str) -> bool {
        method == "hello"
    }
    fn subscribe<'a>(
        &'a mut self,
        topic: &'a str,
    ) -> BoxFut<'a, Result<BoxStream<'static, SubUpdate>, RpcError>> {
        Box::pin(async move {
            let stream: BoxStream<'static, SubUpdate> = if topic == "empty" {
                Box::pin(futures_util::stream::empty())
            } else {
                Box::pin(futures_util::stream::pending())
            };
            Ok(stream)
        })
    }
    fn snapshots(&mut self, topic: &str) -> Vec<(alloc::string::String, Payload, crate::Stamp)> {
        if topic == "snapshot" {
            vec![(topic.into(), Arc::from([]), crate::Stamp::from_nanos(0))]
        } else {
            Vec::new()
        }
    }
    fn write<'a>(&'a mut self, _: &'a str, _: Payload) -> BoxFut<'a, Result<(), RpcError>> {
        Box::pin(async { Ok(()) })
    }
}

#[tokio::test]
async fn server_emits_domain_events_at_the_existing_decisions() {
    let (observer, seen) = observer();
    capture::future(
        observer,
        run_session(
            ScriptConnection::boxed(b"hsshex"),
            &ScriptCodec,
            &ScriptDispatch { reject: false },
            &SessionConfig::default(),
        ),
    )
    .await;
    assert_eq!(
        *seen.lock().unwrap(),
        [("refused", 1), ("reset", 1), ("ended", 2), ("snapshot", 3)]
    );
}

#[tokio::test]
async fn authentication_rejection_is_captured_without_logging_features() {
    let (observer, seen) = observer();
    capture::future(
        observer,
        run_session(
            ScriptConnection::boxed(b""),
            &ScriptCodec,
            &ScriptDispatch { reject: true },
            &SessionConfig::default(),
        ),
    )
    .await;
    assert_eq!(*seen.lock().unwrap(), [("auth", 0)]);
}

struct ScriptListener(VecDeque<TransportResult<Box<dyn Connection>>>);
impl Listener for ScriptListener {
    fn accept(&mut self) -> BoxFut<'_, TransportResult<Box<dyn Connection>>> {
        Box::pin(async move { self.0.pop_front().unwrap_or(Err(TransportError::Closed)) })
    }
}
#[tokio::test]
async fn accept_loop_reports_throttled_failures_recovery_limit_and_closure() {
    let mut script: VecDeque<_> = (0..1000).map(|_| Err(TransportError::Io)).collect();
    script.push_back(Ok(ScriptConnection::boxed(b"")));
    script.push_back(Err(TransportError::Io));
    let config = SessionConfig {
        limits: SessionLimits {
            max_connections: 0,
            ..Default::default()
        },
        ..Default::default()
    };
    let (observer, seen) = observer();
    capture::future(
        observer,
        serve(
            ScriptListener(script),
            Arc::new(ScriptCodec),
            Arc::new(ScriptDispatch { reject: false }),
            config,
        ),
    )
    .await;
    assert_eq!(
        *seen.lock().unwrap(),
        [
            ("failed", 1),
            ("failing", 10),
            ("failing", 100),
            ("failing", 1000),
            ("recovered", 1000),
            ("limit", 0),
            ("failed", 1),
            ("closed", 0)
        ]
    );
}

#[tokio::test]
async fn listener_closure_reports_the_connections_it_drains() {
    let (observer, seen) = observer();
    let listener = ScriptListener(VecDeque::from([Ok(ScriptConnection::boxed(b""))]));
    capture::future(
        observer,
        serve(
            listener,
            Arc::new(ScriptCodec),
            Arc::new(ScriptDispatch { reject: false }),
            SessionConfig::default(),
        ),
    )
    .await;
    assert_eq!(*seen.lock().unwrap(), [("closed", 1)]);
}

struct FailingDialer;
impl Dialer for FailingDialer {
    fn connect(&self) -> BoxFut<'_, TransportResult<Box<dyn Connection>>> {
        Box::pin(async { Err(TransportError::Io) })
    }
}
#[tokio::test]
async fn client_reports_dial_failure_and_reconnect_exhaustion() {
    let (observer, seen) = observer();
    let ctx = crate::RuntimeContext::new(Arc::new(crate::executor::test_support::NoopRuntimeOps));
    let (_handle, engine) = run_client(
        FailingDialer,
        ScriptCodec,
        ClientConfig {
            max_reconnect_attempts: 1,
            reconnect: true,
            ..Default::default()
        },
        ctx,
    );
    capture::future(observer, engine).await;
    assert_eq!(*seen.lock().unwrap(), [("dial", 0), ("exhausted", 1)]);
}

#[test]
fn capture_restores_nested_observers_and_unwinds() {
    let (outer, outer_seen) = observer();
    let (inner, inner_seen) = observer();
    capture::with(outer, || {
        report(SessionEvent::AcceptRecovered { after: 1 });
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            capture::with(inner, || {
                report(SessionEvent::AcceptRecovered { after: 2 });
                panic!("test unwind");
            })
        }));
        assert!(result.is_err());
        report(SessionEvent::AcceptRecovered { after: 3 });
    });
    report(SessionEvent::AcceptRecovered { after: 4 });
    assert_eq!(
        *outer_seen.lock().unwrap(),
        [("recovered", 1), ("recovered", 3)]
    );
    assert_eq!(*inner_seen.lock().unwrap(), [("recovered", 2)]);
}

#[tokio::test]
async fn capture_is_scoped_to_each_poll_and_isolates_concurrent_futures() {
    let (a, a_seen) = observer();
    let (b, b_seen) = observer();
    let task = |n| async move {
        report(SessionEvent::AcceptRecovered { after: n });
        tokio::task::yield_now().await;
        report(SessionEvent::AcceptRecovered { after: n + 1 });
    };
    futures_util::future::join(
        capture::future(a.clone(), task(1)),
        capture::future(b, task(10)),
    )
    .await;
    let mut parked = Box::pin(capture::future(a, pending::<()>()));
    core::future::poll_fn(|cx| {
        assert!(parked.as_mut().poll(cx).is_pending());
        core::task::Poll::Ready(())
    })
    .await;
    report(SessionEvent::AcceptRecovered { after: 100 });
    drop(parked);
    assert_eq!(
        *a_seen.lock().unwrap(),
        [("recovered", 1), ("recovered", 2)]
    );
    assert_eq!(
        *b_seen.lock().unwrap(),
        [("recovered", 10), ("recovered", 11)]
    );
}

#[cfg(feature = "log")]
mod backend_tests {
    use super::*;

    #[cfg(feature = "tracing")]
    struct TraceSink(std::sync::Arc<std::sync::atomic::AtomicUsize>);
    #[cfg(feature = "tracing")]
    impl tracing::Subscriber for TraceSink {
        fn enabled(&self, _: &tracing::Metadata<'_>) -> bool {
            true
        }
        fn new_span(&self, _: &tracing::span::Attributes<'_>) -> tracing::span::Id {
            tracing::span::Id::from_u64(1)
        }
        fn record(&self, _: &tracing::span::Id, _: &tracing::span::Record<'_>) {}
        fn record_follows_from(&self, _: &tracing::span::Id, _: &tracing::span::Id) {}
        fn event(&self, event: &tracing::Event<'_>) {
            assert_eq!(event.metadata().target(), "aimdb_core::session::server");
            self.0.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        }
        fn enter(&self, _: &tracing::span::Id) {}
        fn exit(&self, _: &tracing::span::Id) {}
    }
    #[test]
    fn log_backend_preserves_text_levels_and_the_raising_module_target() {
        // Scope collection to this thread so parallel engine tests cannot contaminate it.
        std::thread_local! {
            static RECORDS: std::cell::RefCell<Option<Vec<(log::Level, alloc::string::String, alloc::string::String)>>> = const { std::cell::RefCell::new(None) };
        }
        struct Logger;
        impl log::Log for Logger {
            fn enabled(&self, _: &log::Metadata<'_>) -> bool {
                true
            }
            fn log(&self, record: &log::Record<'_>) {
                RECORDS.with(|records| {
                    if let Some(records) = records.borrow_mut().as_mut() {
                        records.push((
                            record.level(),
                            record.target().into(),
                            alloc::format!("{}", record.args()),
                        ));
                    }
                });
            }
            fn flush(&self) {}
        }
        static LOGGER: Logger = Logger;
        log::set_logger(&LOGGER).expect("no other unit test installs a logger");
        log::set_max_level(log::LevelFilter::Trace);
        RECORDS.with(|records| {
            records.replace(Some(Vec::new()));
        });
        #[cfg(feature = "tracing")]
        let trace_seen = Arc::new(core::sync::atomic::AtomicUsize::new(0));
        let emit = || {
            report(SessionEvent::AcceptFailed {
                error: TransportError::Io,
            });
            report(SessionEvent::AcceptStillFailing {
                attempts: 10,
                error: TransportError::Busy,
            });
            report(SessionEvent::AcceptRecovered { after: 12 });
        };
        // A real subscriber prevents tracing's optional log fallback from
        // duplicating the facade's explicit log destination.
        #[cfg(feature = "tracing")]
        tracing::subscriber::with_default(TraceSink(trace_seen.clone()), emit);
        #[cfg(not(feature = "tracing"))]
        emit();
        #[cfg(feature = "tracing")]
        assert_eq!(trace_seen.load(core::sync::atomic::Ordering::Relaxed), 3);
        let records = RECORDS.with(|records| records.take().unwrap());
        assert_eq!(
            records,
            [
                (
                    log::Level::Error,
                    "aimdb_core::session::server".into(),
                    "accept failed: Io; retrying, further failures are summarized".into()
                ),
                (
                    log::Level::Warn,
                    "aimdb_core::session::server".into(),
                    "accept still failing: 10 attempts in a row, last: Busy".into()
                ),
                (
                    log::Level::Info,
                    "aimdb_core::session::server".into(),
                    "accept recovered after 12 failed attempt(s)".into()
                ),
            ]
        );
    }
}