ulo 0.1.0-alpha.3

Fast and modular web framework for scalable applications
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! Loading a module after startup (section 8.6).
//!
//! Capabilities pinned: 38 (a module is wired against the frozen graph and connected through its
//! own constructors, checks and init hooks; an equal identity is loaded once; a failed load leaves
//! the app as it found it; what the graph cannot take is refused with the `LoadRefusal` that names
//! it; a lazy module's hooks run first at shutdown, latest load first; a load whose module, or an
//! import of it, the graph holds under an equal identity with other settings is refused).
//!
//! `LoadError::Closed` from Stopping on, and after the shutdown, is pinned by the `load` lines of
//! `phases.rs`.

#[macro_use]
mod common;

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use common::*;
use ulo::scope::Auto;
use ulo::{
    AppHandle, ConnectError, Dep, FailureReason, Key, LoadError, LoadRefusal, LookupError, LookupKind, Meta, Module,
    ModuleDef, ModuleIdentity, ModuleSettings, ModuleSite, Signal, WiringError,
};

struct A;
struct G;
struct L1;
struct L2;
struct Svc;
struct Cfg(u8);

async fn base_with(root: impl ulo::Module) -> (ulo::App<ulo::Connected>, AppHandle) {
    let app = connects(root).await;
    let handle = app.handle();
    (app, handle)
}

async fn refused(handle: &AppHandle, module: impl ulo::Module) -> LoadRefusal {
    match handle.load(module).await {
        Err(LoadError::Refused(refusal)) => refusal,
        Err(other) => panic!("expected a refusal, got: {other}"),
        Ok(_) => panic!("the load was not refused"),
    }
}

#[tokio::test]
async fn a_module_loaded_after_startup_is_built_checked_and_started_then_read_through_its_handle() {
    let log = Log::new();
    let built = Count::new();
    let (l, b) = (log.clone(), built.clone());
    let (app, handle) = base_with(Mod::<A>::new(|m| {
        m.import(Mod::<G>::new(|m| {
            m.global();
            m.value(Cfg(9));
            m.export::<Cfg>();
        }));
    }))
    .await;

    let lazy = Mod::<L1>::new(move |m| {
        let (built, checked, init) = (b.clone(), l.clone(), l.clone());
        // Reads a global export of the base graph, which a lazy module sees like any module.
        m.singleton(move |cfg: Dep<Cfg>| {
            built.bump();
            let n = cfg.0;
            async move { (Svc, n) }
        })
        .ready(move |_s: Dep<(Svc, u8)>| {
            let log = checked.clone();
            async move {
                log.push("checked");
                Ok::<(), Boom>(())
            }
        })
        .on_init(log_init(init));
    });
    assert_eq!(built.get(), 0, "nothing of the module exists before it is loaded");

    let module = handle.load(lazy).await.unwrap_or_else(|error| panic!("load failed: {error}"));
    assert_eq!(built.get(), 1, "the module's singleton was built by the load");
    assert_eq!(log.events(), ["checked", "init"], "its check and init hook ran during the load");
    assert_eq!(module.name().to_string(), mod_name::<L1>());
    let svc = module.get::<(Svc, u8)>().await.expect("the module reads its own binding");
    assert_eq!(svc.1, 9, "and the global export it was built from");
    assert!(
        matches!(app.get::<(Svc, u8)>().await, Err(LookupError::NotFound { kind: LookupKind::Binding, .. })),
        "the base graph's modules do not see a lazy module's bindings"
    );
    close(app).await;
}

fn log_init(log: Log) -> impl Fn() -> std::future::Ready<Result<(), Boom>> + Send + Sync + 'static {
    move || {
        log.push("init");
        std::future::ready(Ok(()))
    }
}

#[tokio::test]
async fn an_equal_identity_loads_once_and_a_module_the_graph_already_holds_returns_its_handle() {
    let built = Count::new();
    let (app, handle) = base_with(Mod::<A>::new(|m| {
        m.import(Mod::<G>::new(|m| {
            m.value(Cfg(1));
        }));
    }))
    .await;
    let make = |c: &Count| {
        let c = c.clone();
        Mod::<L1>::new(move |m| {
            let c = c.clone();
            m.singleton(move || {
                c.bump();
                async { Svc }
            });
        })
    };
    let first = handle.load(make(&built)).await.unwrap_or_else(|error| panic!("load failed: {error}"));
    let second = handle.load(make(&built)).await.unwrap_or_else(|error| panic!("load failed: {error}"));
    assert_eq!(built.get(), 1, "the second load of an equal identity built nothing");
    assert_eq!(first.name().to_string(), second.name().to_string());

    let held = handle.load(Mod::<G>::bare()).await.unwrap_or_else(|error| panic!("load failed: {error}"));
    assert_eq!(held.name().to_string(), mod_name::<G>(), "a module the base graph imports answers its existing handle");
    assert!(held.get::<Cfg>().await.is_ok(), "and is the base module, not an empty replacement");
    close(app).await;
}

#[tokio::test]
async fn concurrent_loads_are_serialised_so_a_failing_load_cannot_undo_another() {
    let (app, handle) = base_with(Empty).await;
    // `L1` yields inside its init hook and then fails, so its rollback comes after `L2`'s whole load
    // unless the loads wait for one another.
    let failing = Mod::<L1>::new(|m| {
        m.singleton(|| async { Svc }).on_init(|| async {
            for _ in 0..5 {
                tokio::task::yield_now().await;
            }
            Err::<(), _>(Boom::new("refused"))
        });
    });
    let healthy = Mod::<L2>::new(|m| {
        m.singleton(|| async { Cfg(4) });
    });
    let (failed, loaded) = tokio::join!(handle.load(failing), handle.load(healthy));
    assert!(matches!(failed, Err(LoadError::Connect(ConnectError::Hook { .. }))), "the first load fails in its init hook");
    let module = loaded.unwrap_or_else(|error| panic!("the second load failed: {error}"));
    assert!(handle.module::<Mod<L2>>().is_ok(), "the failed load's rollback took the other module out of the graph");
    assert_eq!(module.get::<Cfg>().await.expect("the loaded module reads its binding").0, 4);
    close(app).await;
}

#[derive(Default)]
struct Routes;
impl Meta for Routes {}
struct Addr;
struct Plugin;
trait Plug: Send + Sync {}
impl Plug for Plugin {}

controller!(LazyCtl, Auto, [], |_m| {});

#[tokio::test]
async fn a_module_with_a_controller_is_refused_and_builds_nothing() {
    let built = Count::new();
    let b = built.clone();
    let (app, handle) = base_with(Empty).await;
    let refusal = refused(
        &handle,
        Mod::<L1>::new(move |m| {
            let b = b.clone();
            m.singleton(move || {
                b.bump();
                async { Svc }
            });
            m.controller::<LazyCtl>();
        }),
    )
    .await;
    assert!(matches!(&refusal, LoadRefusal::Controllers { module } if module.to_string() == mod_name::<L1>()), "{refusal:?}");
    assert_eq!(built.get(), 0, "the refusal comes before anything is built");
    close(app).await;
}

#[tokio::test]
async fn a_module_that_writes_module_metadata_is_refused() {
    let (app, handle) = base_with(Empty).await;
    let refusal = refused(&handle, Mod::<L1>::new(|m| {
        m.meta::<Routes>();
    }))
    .await;
    assert!(matches!(&refusal, LoadRefusal::Middleware { module } if module.to_string() == mod_name::<L1>()), "{refusal:?}");
    close(app).await;
}

#[tokio::test]
async fn a_module_that_declares_an_execution_input_is_refused() {
    let (app, handle) = base_with(Empty).await;
    let refusal = refused(&handle, Mod::<L1>::new(|m| {
        m.input::<Addr>().seeded_by::<TA>();
    }))
    .await;
    let LoadRefusal::Input { module, key } = &refusal else { panic!("expected an input refusal, got: {refusal:?}") };
    assert_eq!((module.to_string(), key.key()), (mod_name::<L1>(), Key::of::<Addr, ()>()));
    close(app).await;
}

#[tokio::test]
async fn a_global_module_that_exports_is_refused_and_one_that_exports_nothing_loads() {
    let (app, handle) = base_with(Empty).await;
    let refusal = refused(&handle, Mod::<L1>::new(|m| {
        m.global();
        m.value(Cfg(1));
        m.export::<Cfg>();
    }))
    .await;
    let LoadRefusal::GlobalExport { module, key } = &refusal else { panic!("expected a global-export refusal, got: {refusal:?}") };
    assert_eq!((module.to_string(), key.key()), (mod_name::<L1>(), Key::of::<Cfg, ()>()));

    let loaded = handle
        .load(Mod::<L2>::new(|m| {
            m.global();
            m.value(Cfg(2));
        }))
        .await;
    assert!(loaded.is_ok(), "a global module with nothing exported leaves nothing to fix at wiring");
    close(app).await;
}

#[tokio::test]
async fn a_contribution_to_a_collection_the_graph_has_is_refused_and_one_that_introduces_it_loads() {
    let (app, handle) = base_with(Mod::<A>::new(|m| {
        m.contribute::<dyn Plug>().value(Arc::new(Plugin));
    }))
    .await;
    let refusal = refused(&handle, Mod::<L1>::new(|m| {
        m.contribute::<dyn Plug>().value(Arc::new(Plugin));
    }))
    .await;
    let LoadRefusal::Contribution { module, key } = &refusal else { panic!("expected a contribution refusal, got: {refusal:?}") };
    assert_eq!((module.to_string(), key.key()), (mod_name::<L1>(), Key::of::<dyn Plug, ()>()));

    trait Fresh: Send + Sync {}
    impl Fresh for Plugin {}
    let introduced = handle
        .load(Mod::<L2>::new(|m| {
            m.contribute::<dyn Fresh>().value(Arc::new(Plugin));
        }))
        .await;
    assert!(introduced.is_ok(), "a module may introduce a collection nothing reads yet");
    close(app).await;
}

#[tokio::test]
async fn a_module_with_a_wiring_fault_is_refused_with_its_errors_and_builds_nothing() {
    let built = Count::new();
    let b = built.clone();
    let (app, handle) = base_with(Empty).await;
    let error = handle
        .load(Mod::<L1>::new(move |m| {
            let b = b.clone();
            m.singleton(move || {
                b.bump();
                async { Svc }
            });
            m.singleton(|_c: Dep<Cfg>| async { Addr });
        }))
        .await;
    let Err(LoadError::Wiring(errors)) = error else { panic!("expected wiring errors") };
    let (key, module) = entry!(errors, WiringError::Missing { key, module, .. } => (key.key(), module.to_string()));
    assert_eq!((key, module), (Key::of::<Cfg, ()>(), mod_name::<L1>()));
    assert_eq!(built.get(), 0, "a module that does not wire builds nothing");
    assert!(handle.module::<Mod<L1>>().is_err(), "a module that did not wire is not in the graph");
    close(app).await;
}

struct Guard(Log, &'static str);

impl Drop for Guard {
    fn drop(&mut self) {
        self.0.push(self.1);
    }
}

#[tokio::test]
async fn a_load_that_fails_to_connect_leaves_the_app_as_found_and_a_retry_wires_again() {
    let log = Log::new();
    let built = Count::new();
    let fail = Arc::new(AtomicBool::new(true));
    let (app, handle) = base_with(Empty).await;
    let make = |log: &Log, built: &Count, fail: &Arc<AtomicBool>| {
        let (log, built, fail) = (log.clone(), built.clone(), Arc::clone(fail));
        Mod::<L1>::new(move |m| {
            let (log, built) = (log.clone(), built.clone());
            m.singleton(move || {
                built.bump();
                let guard = Guard(log.clone(), "svc dropped");
                async move { (Svc, guard) }
            });
            let fail = Arc::clone(&fail);
            m.try_singleton(move || {
                let fail = fail.load(Ordering::SeqCst);
                async move { if fail { Err(Boom::new("down")) } else { Ok(Cfg(1)) } }
            });
        })
    };

    let error = handle.load(make(&log, &built, &fail)).await.err().expect("the load succeeded");
    let LoadError::Connect(ConnectError::Construct { key, module, reason }) = error else { panic!("expected a construction failure") };
    assert_eq!(key.key(), Key::of::<Cfg, ()>());
    assert_eq!(module.to_string(), mod_name::<L1>(), "the module the failing binding is in, not the root");
    assert!(matches!(reason, FailureReason::Errored(_)));
    assert_eq!(built.get(), 1, "the singleton before the failure was built");
    assert!(log.has("svc dropped"), "and dropped again when the load failed");
    assert!(
        matches!(handle.module::<Mod<L1>>(), Err(LookupError::NotFound { kind: LookupKind::Module, .. })),
        "the module is not in the graph"
    );

    fail.store(false, Ordering::SeqCst);
    let module = handle.load(make(&log, &built, &fail)).await.unwrap_or_else(|error| panic!("the retry failed: {error}"));
    assert_eq!(built.get(), 2, "the retry wired and built the module again from the start");
    assert!(module.get::<Cfg>().await.is_ok());
    close(app).await;
}

#[tokio::test]
async fn a_failing_check_or_init_hook_fails_the_load_as_a_connect_error() {
    let (app, handle) = base_with(Empty).await;
    let readiness = handle
        .load(Mod::<L1>::new(|m| {
            m.singleton(|| async { Svc }).ready(|_s: Dep<Svc>| async { Err::<(), _>(Boom::new("down")) });
        }))
        .await
        .err()
        .expect("the load succeeded");
    assert!(matches!(readiness, LoadError::Connect(ConnectError::Readiness { attempts: 1, .. })), "got: {readiness}");

    let hook = handle
        .load(Mod::<L2>::new(|m| {
            m.on_init(|| async { Err::<(), _>(Boom::new("refused")) });
        }))
        .await
        .err()
        .expect("the load succeeded");
    assert!(matches!(hook, LoadError::Connect(ConnectError::Hook { .. })), "got: {hook}");
    close(app).await;
}

#[tokio::test]
async fn lazy_modules_shut_down_first_latest_load_first_then_the_base_graph() {
    let log = Log::new();
    let l = log.clone();
    let (app, handle) = base_with(Mod::<A>::new(move |m| {
        m.singleton(|| async { Cfg(0) })
            .before_shutdown(shutdown_hook(&l, "base before"))
            .on_destroy(destroy_hook(&l, "base destroy"))
            .on_shutdown(shutdown_hook(&l, "base on"));
    }))
    .await;
    let lazy = |name: &'static str, log: &Log| {
        let log = log.clone();
        move |m: &mut ulo::ModuleDef<'_>| {
            m.singleton(|| async { Svc })
                .before_shutdown(shutdown_hook(&log, leak(format!("{name} before"))))
                .on_destroy(destroy_hook(&log, leak(format!("{name} destroy"))))
                .on_shutdown(shutdown_hook(&log, leak(format!("{name} on"))));
        }
    };
    handle.load(Mod::<L1>::new(lazy("l1", &log))).await.unwrap();
    handle.load(Mod::<L2>::new(lazy("l2", &log))).await.unwrap();

    app.close(Signal::new("done")).await.expect("shutdown reported a failure");
    assert_eq!(
        log.events(),
        ["l2 before", "l1 before", "base before", "l2 destroy", "l1 destroy", "base destroy", "l2 on", "l1 on", "base on"],
        "in every hook step the lazily loaded modules come first, the latest load first"
    );
}

fn leak(text: String) -> &'static str {
    Box::leak(text.into_boxed_str())
}

fn shutdown_hook(log: &Log, text: &'static str) -> impl Fn(Signal) -> std::future::Ready<()> + Send + Sync + 'static {
    let log = log.clone();
    move |_s| {
        log.push(text);
        std::future::ready(())
    }
}

fn destroy_hook(log: &Log, text: &'static str) -> impl Fn() -> std::future::Ready<()> + Send + Sync + 'static {
    let log = log.clone();
    move || {
        log.push(text);
        std::future::ready(())
    }
}

/// A configured module whose identity is `options` and whose one setting is `knob`.
struct Pool {
    options: &'static str,
    knob: u32,
}

impl Module for Pool {
    fn identity(&self) -> ModuleIdentity {
        ModuleIdentity::of_owner::<Pool, _>(&self.options)
    }

    fn settings(&self) -> ModuleSettings {
        ModuleSettings::new().with("knob", self.knob)
    }

    fn register(&self, m: &mut ModuleDef<'_>) {
        m.value(Cfg(1));
        m.export::<Cfg>();
    }
}

/// The two sides of the one `DifferingSettings` entry a refused load reports, as text.
fn differing_sides(outcome: Result<ulo::ModuleRef, LoadError>) -> (String, String, Vec<&'static str>) {
    let errors = match outcome {
        Err(LoadError::Wiring(errors)) => errors,
        Err(other) => panic!("expected wiring errors, got: {other}"),
        Ok(_) => panic!("the load was accepted; other settings under an equal identity should be refused"),
    };
    let side = |site: &ModuleSite| match site {
        ModuleSite::Import { importer, .. } => format!("import by {importer}"),
        ModuleSite::Load { .. } => "load".to_owned(),
        ModuleSite::Root => "root".to_owned(),
        other => format!("{other:?}"),
    };
    let found = entry!(errors, WiringError::DifferingSettings { first, second, settings, .. } =>
        (side(first), side(second), settings.iter().map(|setting| setting.name).collect()));
    assert_eq!(errors.len(), 1, "{errors}");
    found
}

/// Planted break: `reload_differs` answering `None`, which returns the existing handle.
#[tokio::test]
async fn loading_an_identity_the_graph_holds_with_other_settings_is_refused() {
    let (app, handle) = base_with(Mod::<A>::new(|m| m.import(Pool { options: "db", knob: 1 }))).await;
    let sides = differing_sides(handle.load(Pool { options: "db", knob: 2 }).await);
    assert_eq!(sides, (format!("import by {}", mod_name::<A>()), "load".to_owned(), vec!["knob"]));

    let held = handle.load(Pool { options: "db", knob: 1 }).await.unwrap_or_else(|error| panic!("equal settings: {error}"));
    assert_eq!(held.name().to_string(), "Pool", "equal settings answer the existing module");
    close(app).await;
}

/// A load of an identity the graph holds whose settings repeat a name is refused, though its
/// settings equal the held module's. Planted break: `reload_differs` reporting no repeat.
#[tokio::test]
async fn loading_an_identity_the_graph_holds_with_a_repeated_setting_is_refused() {
    struct Repeating(bool);
    impl Module for Repeating {
        fn identity(&self) -> ModuleIdentity {
            ModuleIdentity::of_owner::<Repeating, _>(&"db")
        }

        fn settings(&self) -> ModuleSettings {
            let settings = ModuleSettings::new().with("knob", 1);
            if self.0 { settings.with("knob", 2) } else { settings }
        }

        fn register(&self, _: &mut ModuleDef<'_>) {}
    }
    let (app, handle) = base_with(Mod::<A>::new(|m| m.import(Repeating(false)))).await;
    let errors = match handle.load(Repeating(true)).await {
        Err(LoadError::Wiring(errors)) => errors,
        Err(other) => panic!("expected wiring errors, got: {other}"),
        Ok(_) => panic!("the load was accepted; a repeated setting should be refused"),
    };
    let (setting, module) = entry!(errors, WiringError::DuplicateSetting { setting, module, .. } => (*setting, module.to_string()));
    assert_eq!((setting, module.as_str()), ("knob", "Repeating"));
    assert_eq!(errors.len(), 1, "{errors}");
    close(app).await;
}

/// Planted break: `reach` returning `Import::Existing` without comparing settings.
#[tokio::test]
async fn a_loaded_module_importing_an_identity_the_graph_holds_with_other_settings_is_refused() {
    let (app, handle) = base_with(Mod::<A>::new(|m| m.import(Pool { options: "db", knob: 1 }))).await;
    let sides = differing_sides(handle.load(Mod::<L1>::new(|m| m.import(Pool { options: "db", knob: 2 }))).await);
    assert_eq!(sides, (format!("import by {}", mod_name::<A>()), format!("import by {}", mod_name::<L1>()), vec!["knob"]));

    handle
        .load(Mod::<L2>::new(|m| m.import(Pool { options: "db", knob: 1 })))
        .await
        .unwrap_or_else(|error| panic!("an import with equal settings links to the frozen module: {error}"));
    close(app).await;
}

/// Each side names its `load` call. Planted breaks: `register_lazy` walking from `Site::Root`, so
/// a loaded module is frozen as the root's registration; `AppHandle::load` without
/// `#[track_caller]`, which names the line inside it.
#[tokio::test]
async fn loading_a_loaded_identity_again_with_other_settings_names_both_loads() {
    let (app, handle) = base_with(Mod::<A>::new(|_| {})).await;
    let first_line = line!() + 1;
    handle.load(Pool { options: "db", knob: 1 }).await.unwrap_or_else(|error| panic!("the first load: {error}"));
    let second_line = line!() + 1;
    let outcome = handle.load(Pool { options: "db", knob: 2 }).await;
    let text = match &outcome {
        Err(error) => error.to_string(),
        Ok(_) => String::new(),
    };
    if let Err(LoadError::Wiring(errors)) = &outcome {
        let sites = entry!(errors, WiringError::DifferingSettings { first, second, .. } => (first.clone(), second.clone()));
        let at = |site: &ModuleSite| match site {
            ModuleSite::Load { at } => (at.file().to_owned(), at.line()),
            other => panic!("expected a load site, got {other:?}"),
        };
        assert_eq!(at(&sites.0), (file!().to_owned(), first_line), "the first `load` call");
        assert_eq!(at(&sites.1), (file!().to_owned(), second_line), "the second `load` call");
    }
    assert_eq!(differing_sides(outcome), ("load".to_owned(), "load".to_owned(), vec!["knob"]));
    let file = file!();
    assert!(text.contains(&format!("loaded by `AppHandle::load` at {file}:{first_line} with `knob` = 1")), "{text}");
    assert!(text.contains(&format!("loaded again by `AppHandle::load` at {file}:{second_line} with `knob` = 2")), "{text}");
    close(app).await;
}