modrun 1.0.1

Modular application composer for Tokio: domain modules, typed wiring, lifecycle
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
//! Provide / invoke / supply / graph-validation tests.

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

use modrun::Modrun;
use std::error::Error;
use std::result::Result;
use tokio::task::yield_now;

#[tokio::test]
async fn provide_result_and_deps() {
    #[derive(Clone)]
    struct Config {
        port: u16,
    }

    #[derive(Clone)]
    struct Server {
        cfg: Config,
    }

    fn new_config() -> Result<Config, &'static str> {
        Ok(Config { port: 9090 })
    }

    fn new_server(cfg: Config) -> Server {
        Server { cfg }
    }

    fn check(server: Server) {
        assert_eq!(server.cfg.port, 9090);
    }

    Modrun::builder()
        .provide_result(new_config)
        .provide(new_server)
        .invoke(check)
        .start()
        .await
        .unwrap()
        .stop()
        .await
        .unwrap();
}

#[tokio::test]
async fn missing_provider_errors() {
    #[derive(Clone)]
    struct Missing;
    fn needs(_m: Missing) {}

    let err = Modrun::builder().invoke(needs).start().await.unwrap_err();
    let msg = format!("{err}");
    assert!(
        msg.contains("Missing") || msg.contains("missing"),
        "unexpected error: {msg}"
    );
}

#[tokio::test]
async fn duplicate_provide_errors() {
    fn a() -> u32 {
        1
    }
    fn b() -> u32 {
        2
    }

    let err = Modrun::builder()
        .provide(a)
        .provide(b)
        .invoke(|_: u32| {})
        .start()
        .await
        .unwrap_err();
    let msg = format!("{err}");
    assert!(msg.contains("already provided"), "unexpected error: {msg}");
}

#[tokio::test]
async fn async_constructors_are_awaited_in_dependency_order() {
    #[derive(Clone)]
    struct Pool(&'static str);
    #[derive(Clone)]
    struct Repo(&'static str);

    async fn connect() -> Pool {
        yield_now().await;
        Pool("connected")
    }

    async fn new_repo(pool: Pool) -> Result<Repo, &'static str> {
        yield_now().await;
        Ok(Repo(pool.0))
    }

    fn check(repo: Repo) {
        assert_eq!(repo.0, "connected");
    }

    Modrun::builder()
        .provide_async(connect)
        .provide_result_async(new_repo)
        .invoke(check)
        .start()
        .await
        .unwrap()
        .stop()
        .await
        .unwrap();
}

#[tokio::test]
async fn async_constructor_failure_surfaces() {
    #[derive(Clone)]
    struct Pool;

    async fn connect() -> Result<Pool, &'static str> {
        Err("connection refused")
    }

    let err = Modrun::builder()
        .provide_result_async(connect)
        .invoke(|_p: Pool| {})
        .start()
        .await
        .unwrap_err();
    let msg = format!("{err}");
    assert!(msg.contains("connection refused"), "unexpected: {msg}");
    let src = Error::source(&err).expect("source");
    assert!(
        src.to_string().contains("connection refused"),
        "source was {src}"
    );
}

/// Construction is lazy, so without an up-front check a provider that nothing
/// depends on could carry a missing dependency forever without complaint.
#[tokio::test]
async fn unused_provider_with_missing_dependency_fails_build() {
    #[derive(Clone)]
    struct Absent;
    #[derive(Clone)]
    struct Unused;

    fn new_unused(_absent: Absent) -> Unused {
        Unused
    }

    let err = Modrun::builder()
        .provide(new_unused)
        .start()
        .await
        .unwrap_err();
    let msg = format!("{err}");
    assert!(
        msg.contains("nothing provides") && msg.contains("Absent"),
        "unexpected: {msg}"
    );
}

#[tokio::test]
async fn validation_errors_follow_registration_order() {
    #[derive(Clone)]
    struct MissingA;
    #[derive(Clone)]
    struct MissingB;
    #[derive(Clone)]
    struct A;
    #[derive(Clone)]
    struct B;

    fn new_a(_: MissingA) -> A {
        A
    }
    fn new_b(_: MissingB) -> B {
        B
    }

    for _ in 0..16 {
        let err = Modrun::builder()
            .provide(new_a)
            .provide(new_b)
            .start()
            .await
            .unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("MissingA") && !msg.contains("MissingB"),
            "unexpected validation order: {msg}"
        );
    }
}

#[tokio::test]
async fn dependency_cycle_fails_build() {
    #[derive(Clone)]
    struct A;
    #[derive(Clone)]
    struct B;

    fn new_a(_b: B) -> A {
        A
    }
    fn new_b(_a: A) -> B {
        B
    }

    let err = Modrun::builder()
        .provide(new_a)
        .provide(new_b)
        .start()
        .await
        .unwrap_err();
    let msg = format!("{err}");
    assert!(msg.contains("cycle"), "unexpected: {msg}");
    assert!(
        msg.contains("->"),
        "cycle error should list the path, got: {msg}"
    );
}

/// The same type provided privately in two sibling modules is two distinct
/// providers, not a cycle.
#[tokio::test]
async fn invoker_missing_dependency_fails_before_construction() {
    #[derive(Clone)]
    struct Built;
    #[derive(Clone)]
    struct Missing;

    fn needs(_b: Built, _m: Missing) {}

    let counter = Arc::new(AtomicUsize::new(0));
    let err = Modrun::builder()
        .provide({
            let counter = Arc::clone(&counter);
            move || {
                counter.fetch_add(1, Ordering::SeqCst);
                Built
            }
        })
        .invoke(needs)
        .start()
        .await
        .unwrap_err();
    let msg = format!("{err}");
    assert!(
        msg.contains("nothing provides") && msg.contains("Missing"),
        "unexpected: {msg}"
    );
    assert_eq!(
        counter.load(Ordering::SeqCst),
        0,
        "Built must not be constructed before validation fails"
    );
}

#[tokio::test]
async fn fallible_invoker_error_is_contextualized() {
    fn boom() -> Result<(), &'static str> {
        Err("nope")
    }

    let err = Modrun::builder().invoke(boom).start().await.unwrap_err();
    let msg = format!("{err}");
    assert!(
        msg.contains("invoker") && msg.contains("failed") && msg.contains("nope"),
        "unexpected: {msg}"
    );
    assert!(msg.contains("boom"), "unexpected: {msg}");
    let src = Error::source(&err).expect("source");
    assert!(src.to_string().contains("nope"), "source was {src}");
}

#[tokio::test]
async fn duplicate_supply_errors() {
    let err = Modrun::builder()
        .supply(1u32)
        .supply(2u32)
        .invoke(|_: u32| {})
        .start()
        .await
        .unwrap_err();
    assert!(
        format!("{err}").contains("already provided"),
        "unexpected: {err}"
    );
}

#[tokio::test]
async fn empty_builder_starts_and_stops() {
    Modrun::builder()
        .start()
        .await
        .unwrap()
        .stop()
        .await
        .unwrap();
}

#[tokio::test]
async fn provide_dyn_and_invoke_dyn() {
    use std::any::TypeId;

    use modrun::__wiring::{InvokeFn, ProviderFn};

    #[derive(Clone)]
    struct N(u8);

    fn new_n() -> N {
        N(7)
    }
    fn check(n: N) {
        assert_eq!(n.0, 7);
    }

    let provider = new_n.into_provider();
    assert_eq!(provider.result_type(), TypeId::of::<N>());
    assert!(provider.result_name().ends_with("::N"));
    assert!(provider.dep_types().is_empty());

    Modrun::builder()
        .provide_dyn(provider)
        .invoke_dyn(check.into_invoke())
        .start()
        .await
        .unwrap()
        .stop()
        .await
        .unwrap();
}

#[tokio::test]
async fn inject_arc_without_cloning_t() {
    struct Token;

    fn new_token() -> Token {
        Token
    }

    fn boot(token: Arc<Token>) {
        let _ = token;
    }

    Modrun::builder()
        .provide(new_token)
        .invoke(boot)
        .start()
        .await
        .unwrap()
        .stop()
        .await
        .unwrap();
}

#[tokio::test]
async fn constructor_can_depend_on_arc_of_provider_result() {
    struct Config;
    struct Service(Arc<Config>);

    fn new_config() -> Config {
        Config
    }
    fn new_service(config: Arc<Config>) -> Service {
        Service(config)
    }
    fn boot(service: Arc<Service>) {
        let _ = &service.0;
    }

    Modrun::builder()
        .provide(new_config)
        .provide(new_service)
        .invoke(boot)
        .start()
        .await
        .unwrap()
        .stop()
        .await
        .unwrap();
}

#[tokio::test]
async fn cycle_through_arc_dependency_is_reported_during_validation() {
    #[derive(Clone)]
    struct A;
    struct B;

    fn new_a(_b: Arc<B>) -> A {
        A
    }
    fn new_b(_a: A) -> B {
        B
    }

    let err = Modrun::builder()
        .provide(new_a)
        .provide(new_b)
        .start()
        .await
        .unwrap_err();
    let msg = err.to_string();
    assert!(
        msg.contains("cycle") && msg.contains("->"),
        "unexpected: {msg}"
    );
}

#[tokio::test]
async fn invoke_async_with_arc_dependency() {
    struct N(u8);

    fn new_n() -> N {
        N(3)
    }

    async fn check(n: Arc<N>) {
        assert_eq!(n.0, 3);
    }

    Modrun::builder()
        .provide(new_n)
        .invoke_async(check)
        .start()
        .await
        .unwrap()
        .stop()
        .await
        .unwrap();
}

#[tokio::test]
async fn three_node_cycle_is_reported() {
    #[derive(Clone)]
    struct NodeA;
    #[derive(Clone)]
    struct NodeB;
    #[derive(Clone)]
    struct NodeC;

    fn new_a(_b: NodeB) -> NodeA {
        NodeA
    }
    fn new_b(_c: NodeC) -> NodeB {
        NodeB
    }
    fn new_c(_a: NodeA) -> NodeC {
        NodeC
    }
    fn use_a(_a: NodeA) {}

    let err = Modrun::builder()
        .provide(new_a)
        .provide(new_b)
        .provide(new_c)
        .invoke(use_a)
        .start()
        .await
        .unwrap_err();
    let msg = format!("{err}");
    assert!(msg.contains("cycle"), "unexpected: {msg}");
    assert!(msg.contains("->"), "unexpected: {msg}");
    assert!(
        msg.contains("NodeA") && msg.contains("NodeB") && msg.contains("NodeC"),
        "unexpected: {msg}"
    );
}

#[tokio::test]
async fn eight_parameter_provide_and_invoke() {
    #[derive(Clone)]
    struct P1(u8);
    #[derive(Clone)]
    struct P2(u8);
    #[derive(Clone)]
    struct P3(u8);
    #[derive(Clone)]
    struct P4(u8);
    #[derive(Clone)]
    struct P5(u8);
    #[derive(Clone)]
    struct P6(u8);
    #[derive(Clone)]
    struct P7(u8);
    #[derive(Clone)]
    struct P8(u8);
    #[derive(Clone)]
    struct Eight(u8);

    #[allow(clippy::too_many_arguments)]
    fn new_eight(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8) -> Eight {
        Eight(p1.0 + p2.0 + p3.0 + p4.0 + p5.0 + p6.0 + p7.0 + p8.0)
    }

    fn boot(eight: Eight) {
        assert_eq!(eight.0, 36);
    }

    Modrun::builder()
        .supply(P1(1))
        .supply(P2(2))
        .supply(P3(3))
        .supply(P4(4))
        .supply(P5(5))
        .supply(P6(6))
        .supply(P7(7))
        .supply(P8(8))
        .provide(new_eight)
        .invoke(boot)
        .start()
        .await
        .unwrap()
        .stop()
        .await
        .unwrap();
}