harmos-runtime 0.1.2

Harmos runtime: managed state, undo, recovery, background work and typed streams.
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
/* ***
 *
 * Harmos
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * ***
 */

/* === Modules === */

/* === Re-Exports === */

/* === Imports === */

// Standard Library Imports
use std::sync::{
    Arc,
    atomic::{AtomicUsize, Ordering::SeqCst},
};
// Third Party Imports
use tokio::sync::Notify;
// Local Imports
use super::{Bank, assembly};
use harmos_runtime::operations::OperationManager;
use harmos_runtime::services::ServiceDefinition;
use harmos_runtime::{Error, services::Fault, services::Service, services::ServiceStatus};

/* === Constants === */

/* === Macros === */

/* === Type Aliases === */

/* === Structs === */

#[derive(Default)]
struct Resources {
    started: AtomicUsize,
    finished: AtomicUsize,
    stopping: Notify,
    cleanup: Notify,
}

/* === Enums === */

/* === Traits === */

/* === Implementations === */

/* === Public Functions === */

/* === Private Functions === */

fn register(manager: &OperationManager) {
    manager
        .register_service(
            ServiceDefinition { id: "scoped".into(), policy: harmos_runtime::services::POLICY },
            |resources: Arc<Resources>| {
                move |mut service: Service<Bank>| {
                    let resources = resources.clone();
                    async move {
                        resources.started.fetch_add(1, SeqCst);
                        service.ready();
                        service.stopping().await;
                        resources.stopping.notify_one();
                        resources.cleanup.notified().await;
                        resources.finished.fetch_add(1, SeqCst);
                        Ok::<(), Fault<&'static str>>(())
                    }
                }
            },
        )
        .unwrap();
}

#[tokio::test]
async fn control_retains_join() {
    let runtime = assembly().runtime().await.unwrap();
    let manager = &runtime.operations;
    register(manager);
    let resources = Arc::new(Resources::default());
    let first = manager.submit_with_resources_at("scoped", resources.clone()).await.unwrap();
    let second = manager.submit_with_resources_at("scoped", resources.clone()).await.unwrap();
    first.ready().await.unwrap();
    assert_eq!(first.id(), second.id());
    assert_eq!(manager.resident_key(first.id()), manager.resident_key(second.id()));
    assert_eq!(manager.resident(&manager.resident_key(second.id()).unwrap()).unwrap().leases, 2);

    first.cancel();
    first.cancel();
    assert!(matches!(manager.resident(&manager.resident_key(second.id()).unwrap()).unwrap().health, Some(ServiceStatus::Stopping)));
    (resources.stopping.notified()).await;
    {
        let joining = first.settle();
        tokio::pin!(joining);
        std::future::poll_fn(|cx| {
            assert!(std::future::Future::poll(joining.as_mut(), cx).is_pending());
            std::task::Poll::Ready(())
        })
        .await;
    }
    assert_eq!(resources.finished.load(SeqCst), 0);
    resources.cleanup.notify_one();
    (first.settle()).await.unwrap();
    first.settle().await.unwrap();
    assert_eq!(resources.finished.load(SeqCst), 1);
    assert!(first.status().settled_at().is_some());

    let replacement = manager.submit_with_resources_at("scoped", resources.clone()).await.unwrap();
    replacement.ready().await.unwrap();
    assert_ne!(replacement.id(), first.id());
    first.cancel();
    first.settle().await.unwrap();
    first.release().await.unwrap();
    second.release().await.unwrap();
    assert_eq!(manager.resident(&manager.resident_key(replacement.id()).unwrap()).unwrap().leases, 1);
    assert!(matches!(
        manager.resident(&manager.resident_key(replacement.id()).unwrap()).unwrap().health,
        Some(ServiceStatus::Running { .. })
    ));
    resources.cleanup.notify_one();
    replacement.release().await.unwrap();
    runtime.stop().await.unwrap();
}

#[tokio::test]
async fn final_release_joins() {
    let runtime = assembly().runtime().await.unwrap();
    let manager = &runtime.operations;
    register(manager);
    let resources = Arc::new(Resources::default());
    let (first, second) = tokio::join!(
        manager.submit_with_resources_at("scoped", resources.clone()),
        manager.submit_with_resources_at("scoped", resources.clone())
    );
    let first = first.unwrap();
    let second = second.unwrap();
    first.ready().await.unwrap();
    assert_eq!(first.id(), second.id());
    assert_eq!(manager.resident(&manager.resident_key(first.id()).unwrap()).unwrap().instance, first.id());
    assert_eq!(resources.started.load(SeqCst), 1);
    assert_eq!(manager.resident(&manager.resident_key(first.id()).unwrap()).unwrap().leases, 2);
    first.release().await.unwrap();
    assert_eq!(manager.resident(&manager.resident_key(second.id()).unwrap()).unwrap().leases, 1);
    assert!(matches!(manager.resident(&manager.resident_key(second.id()).unwrap()).unwrap().health, Some(ServiceStatus::Running { .. })));

    let release = second.release();
    tokio::pin!(release);
    tokio::select! {
        result = &mut release => panic!("cleanup must be joined: {result:?}"),
        _ = resources.stopping.notified() => {}
    }
    resources.cleanup.notify_one();
    release.await.unwrap();
    assert_eq!(resources.finished.load(SeqCst), 1);
    runtime.stop().await.unwrap();
}

#[tokio::test]
async fn scope_retirement_revival() {
    let runtime = assembly().runtime().await.unwrap();
    let foreign = assembly().runtime().await.unwrap();
    let manager = &runtime.operations;
    register(manager);
    let a = Arc::new(Resources::default());
    let b = Arc::new(Resources::default());

    let first = runtime.operations.submit_with_resources_at("scoped", a.clone()).await.unwrap();
    let second = runtime.operations.submit_with_resources_at("scoped", b.clone()).await.unwrap();
    first.ready().await.unwrap();
    second.ready().await.unwrap();
    assert_ne!(manager.resident_key(first.id()), manager.resident_key(second.id()));
    assert_eq!(a.started.load(SeqCst), 1);
    assert_eq!(b.started.load(SeqCst), 1);

    assert!(matches!(manager.submit_resident("scoped").await, Err(Error::InvalidInput { .. })));
    assert!(matches!(
        foreign.operations.submit_with_resources_at("scoped", a.clone()).await,
        Err(Error::NotRegistered { domain: "resident", .. })
    ));

    {
        let stop = manager.retire_resources(&a);
        tokio::pin!(stop);
        tokio::select! {
            _ = &mut stop => panic!("cleanup is gated"),
            _ = a.stopping.notified() => {}
        }
    }

    assert!(matches!(
        manager.submit_with_resources_at("scoped", a.clone()).await,
        Err(Error::Closing { domain })
            if domain.ends_with("scope")
    ));
    assert_eq!(manager.resident(&manager.resident_key(first.id()).unwrap()).unwrap().leases, 0);

    a.cleanup.notify_one();
    manager.retire_resources(&a).await.unwrap();
    assert_eq!(a.finished.load(SeqCst), 1);
    assert!(manager.resident_key(first.id()).is_none());

    let revived = manager.submit_with_resources_at("scoped", a.clone()).await.unwrap();
    revived.ready().await.unwrap();
    assert_ne!(revived.id(), first.id());
    assert_eq!(a.started.load(SeqCst), 2);
    assert!(matches!(manager.resident(&manager.resident_key(second.id()).unwrap()).unwrap().health, Some(ServiceStatus::Running { .. })));

    first.release().await.unwrap();
    a.cleanup.notify_one();
    revived.release().await.unwrap();
    b.cleanup.notify_one();
    second.release().await.unwrap();

    runtime.stop().await.unwrap();
    foreign.stop().await.unwrap();
}

#[tokio::test]
async fn dropped_lease_cleanup() {
    let runtime = assembly().runtime().await.unwrap();
    let manager = &runtime.operations;
    register(manager);
    let resources = Arc::new(Resources::default());
    let first = manager.submit_with_resources_at("scoped", resources.clone()).await.unwrap();
    first.ready().await.unwrap();
    let generation = first.id();
    drop(first);
    resources.stopping.notified().await;
    let next = manager.submit_with_resources_at("scoped", resources.clone());
    tokio::pin!(next);
    assert!(futures_util::poll!(&mut next).is_pending());
    assert_eq!(resources.started.load(SeqCst), 1);
    resources.cleanup.notify_one();
    let second = next.await.unwrap();
    second.ready().await.unwrap();
    assert_ne!(second.id(), generation);
    assert_eq!(resources.finished.load(SeqCst), 1);
    assert_eq!(resources.started.load(SeqCst), 2);
    resources.cleanup.notify_one();
    second.release().await.unwrap();
    runtime.stop().await.unwrap();
}

#[tokio::test]
async fn unregister_reserves_name() {
    let runtime = assembly().runtime().await.unwrap();
    let manager = &runtime.operations;
    register(manager);
    let resources = Arc::new(Resources::default());
    let stale = manager.submit_with_resources_at("scoped", resources.clone()).await.unwrap();
    stale.ready().await.unwrap();
    {
        let remove = manager.unregister_resident("scoped");
        tokio::pin!(remove);
        tokio::select! {
            _ = &mut remove => panic!("cleanup is gated"),
            _ = resources.stopping.notified() => {}
        }
        assert!(matches!(
            manager.submit_with_resources_at("scoped", resources.clone()).await,
            Err(Error::Closing { domain })
                if domain.ends_with("definition")
        ));
        assert!(matches!(
            manager.register_service(
                ServiceDefinition { id: "scoped".into(), policy: harmos_runtime::services::POLICY },
                |_: Arc<Resources>| super::resident
            ),
            Err(Error::AlreadyRegistered { .. })
        ));
        resources.cleanup.notify_one();
        remove.await.unwrap();
    }
    register(manager);
    let replacement = manager.submit_with_resources_at("scoped", resources.clone()).await.unwrap();
    replacement.ready().await.unwrap();
    assert_ne!(replacement.id(), stale.id());
    stale.release().await.unwrap();
    assert_eq!(manager.resident(&manager.resident_key(replacement.id()).unwrap()).unwrap().leases, 1);
    assert!(matches!(
        manager.resident(&manager.resident_key(replacement.id()).unwrap()).unwrap().health,
        Some(ServiceStatus::Running { .. })
    ));
    resources.cleanup.notify_one();
    replacement.release().await.unwrap();
    runtime.stop().await.unwrap();
}

#[tokio::test]
async fn shutdown_preserve_typed() {
    #[derive(Debug, thiserror::Error)]
    #[error("initialization refused: {0}")]
    struct Refused(u32);

    fn typed(error: &(dyn std::error::Error + 'static)) -> bool {
        if let Some(Fault::Application(Refused(42))) = error.downcast_ref::<Fault<Refused>>() {
            true
        } else if let Some(Error::TaskFailed { source, .. }) = error.downcast_ref::<Error>() {
            typed(source.as_ref())
        } else {
            error.source().is_some_and(typed)
        }
    }

    let runtime = assembly().runtime().await.unwrap();
    runtime
        .operations
        .register_service(ServiceDefinition { id: "broken".into(), policy: harmos_runtime::services::POLICY }, |_: Arc<()>| {
            |_service: Service<Bank>| async { Err::<(), _>(Fault::Application(Refused(42))) }
        })
        .unwrap();
    let lease = runtime.operations.submit_resident("broken").await.unwrap();
    let error = lease.ready().await.unwrap_err();
    assert!(typed(&error), "{error:?}");
    assert!(typed(&lease.release().await.unwrap_err()));
    for _ in 0..2 {
        assert!(typed(&runtime.stop().await.unwrap_err()));
    }
}

#[tokio::test]
async fn shutdown_signals_scope() {
    let runtime = assembly().runtime().await.unwrap();
    register(&runtime.operations);
    let a = Arc::new(Resources::default());
    let b = Arc::new(Resources::default());
    let first = runtime.operations.submit_with_resources_at("scoped", a.clone()).await.unwrap();
    let second = runtime.operations.submit_with_resources_at("scoped", b.clone()).await.unwrap();
    first.ready().await.unwrap();
    second.ready().await.unwrap();
    let stop = runtime.stop();
    tokio::pin!(stop);
    tokio::select! {
        _ = &mut stop => panic!("both cleanups are gated"),
        _ = async { a.stopping.notified().await; b.stopping.notified().await; } => {}
    }
    a.cleanup.notify_one();
    b.cleanup.notify_one();
    stop.await.unwrap();
    assert_eq!(a.finished.load(SeqCst), 1);
    assert_eq!(b.finished.load(SeqCst), 1);
}

#[tokio::test]
async fn unregister_drops_factory() {
    struct Reentrant {
        manager: OperationManager,
        dropped: Arc<AtomicUsize>,
    }
    impl Drop for Reentrant {
        fn drop(&mut self) {
            assert!(self.manager.resident_definition("unused").is_none());
            self.dropped.fetch_add(1, SeqCst);
        }
    }
    let runtime = assembly().runtime().await.unwrap();
    let dropped = Arc::new(AtomicUsize::new(0));
    let resource = Arc::new(Reentrant { manager: runtime.operations.clone(), dropped: dropped.clone() });
    runtime
        .operations
        .register_service(ServiceDefinition { id: "unused".into(), policy: harmos_runtime::services::POLICY }, move |_: Arc<()>| {
            let _owner = resource.clone();
            super::resident
        })
        .unwrap();
    runtime.operations.unregister_resident("unused").await.unwrap();
    assert_eq!(dropped.load(SeqCst), 1);
    runtime.stop().await.unwrap();
}

#[tokio::test]
async fn resident_not_reinitialized() {
    let runtime = assembly().service("one-shot", |_service| async { Ok::<(), Fault<&str>>(()) }).runtime().await.unwrap();
    let key = harmos_runtime::operations::resident::Key::root("one-shot");
    runtime
        .operations
        .watch_residents()
        .wait_for(|values| values.get(&key).is_some_and(|instance| instance.health == Some(ServiceStatus::Stopped)))
        .await
        .unwrap();
    assert!(matches!(runtime.operations.submit_resident("one-shot").await, Err(Error::NotReady { domain: "retired assembly service" })));
    assert_eq!(runtime.operations.resident(&key).unwrap().health, Some(ServiceStatus::Stopped));
    runtime.stop().await.unwrap();
}

#[tokio::test]
async fn factory_panic_reported() {
    let runtime = assembly().runtime().await.unwrap();
    runtime
        .operations
        .register_service(ServiceDefinition { id: "factory".into(), policy: harmos_runtime::services::POLICY }, |fail: Arc<bool>| {
            assert!(!*fail, "factory initialization failed");
            super::resident
        })
        .unwrap();
    let lease = runtime.operations.submit_with_resources_at("factory", Arc::new(true)).await.unwrap();
    assert!(matches!(lease.ready().await, Err(Error::TaskFailed { .. })));
    assert!(matches!(
        runtime.operations.resident(&runtime.operations.resident_key(lease.id()).unwrap()).unwrap().health,
        Some(ServiceStatus::Inactive { cause: harmos_runtime::services::ServiceExit::Panicked, .. })
    ));
    assert!(matches!(runtime.stop().await, Err(Error::TaskFailed { .. })));
}

/* === Tests === */