semiont 0.7.2

Semiont's Rust SDK: the protocol's types, the transport contract and a client of the bus over it, and what a client needs to name its principals and hold its roles, as protocol/specs/ states it.
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! The state-unit axioms, executable: `assert_state_unit_axioms` runs each
//! against a unit and fails, naming the axiom, on the first one it breaks.
//! One call per state unit, from that unit's tests.
//!
//! - **A5** `dispose` is idempotent and total: any number of calls, none
//!   panics, and none disposes what the unit was given.
//! - **A5b** the unit is inert after `dispose`: its methods do nothing, and a
//!   surface taken afterwards has already ended.
//! - **A6** every reader of a surface taken before `dispose` sees it end.
//! - **A7-passed** disposing the unit does not dispose a dependency it was
//!   given: a `DisposeProbe` standing in for one, or the client itself.
//! - **A7-owned** disposing the unit ends the surfaces of the children it
//!   made.
//! - **X3** instances are isolated: driving one never moves another's
//!   surfaces.
//!
//! Each run happens on a runtime of its own with time paused, so a unit that
//! spawns tasks or keeps timers is given the chance to act on what the
//! harness did before the harness looks.

use crate::client::SemiontClient;
use crate::state_unit::StateUnit;
use futures_core::Stream;
use proptest::collection::vec;
use proptest::strategy::Strategy;
use proptest::test_runner::{Config, TestCaseError, TestRunner};
use std::fmt::Debug;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::task::{Context, Poll, Waker};
use std::time::Duration;
use tokio::sync::{broadcast, watch};

/// A surface of a state unit as the axioms watch it: one reader's view of a
/// state or an event stream.
pub trait Surface {
    /// Whether it has ended: nothing more will come.
    fn ended(&mut self) -> bool;
    /// Whether anything came since this was last asked.
    fn moved(&mut self) -> bool;
}

impl<T> Surface for watch::Receiver<T> {
    fn ended(&mut self) -> bool {
        self.has_changed().is_err()
    }

    fn moved(&mut self) -> bool {
        let moved = matches!(self.has_changed(), Ok(true));
        if moved {
            self.borrow_and_update();
        }
        moved
    }
}

impl<T: Clone> Surface for broadcast::Receiver<T> {
    fn ended(&mut self) -> bool {
        loop {
            match self.try_recv() {
                Err(broadcast::error::TryRecvError::Closed) => return true,
                Err(broadcast::error::TryRecvError::Empty) => return false,
                Ok(_) | Err(broadcast::error::TryRecvError::Lagged(_)) => {}
            }
        }
    }

    fn moved(&mut self) -> bool {
        let mut moved = false;
        while matches!(
            self.try_recv(),
            Ok(_) | Err(broadcast::error::TryRecvError::Lagged(_))
        ) {
            moved = true;
        }
        moved
    }
}

/// Any stream as a surface: it has moved when it has an item ready, and
/// ended when it has ended.
pub struct StreamSurface<S>(pub S);

impl<S: Stream + Unpin> StreamSurface<S> {
    fn poll(&mut self) -> Poll<Option<S::Item>> {
        Pin::new(&mut self.0).poll_next(&mut Context::from_waker(Waker::noop()))
    }
}

impl<S: Stream + Unpin> Surface for StreamSurface<S> {
    fn ended(&mut self) -> bool {
        loop {
            match self.poll() {
                Poll::Ready(None) => return true,
                Poll::Pending => return false,
                Poll::Ready(Some(_)) => {}
            }
        }
    }

    fn moved(&mut self) -> bool {
        let mut moved = false;
        while let Poll::Ready(Some(_)) = self.poll() {
            moved = true;
        }
        moved
    }
}

/// A dependency a unit was given, as the axioms watch it.
pub trait Given {
    /// Whether anything has disposed it.
    fn disposed(&self) -> bool;
}

/// A client is disposed when it is closed, and closing it ends its own bus.
impl Given for Arc<SemiontClient> {
    fn disposed(&self) -> bool {
        self.bus().destroyed()
    }
}

/// A stand-in for a dependency a unit is given. Pass one to the unit, list it
/// in `Fresh::passed_in`, and the axioms check the unit never disposed it.
#[derive(Clone, Default)]
pub struct DisposeProbe {
    disposals: Arc<AtomicUsize>,
}

impl DisposeProbe {
    pub fn new() -> DisposeProbe {
        DisposeProbe::default()
    }

    pub fn dispose_count(&self) -> usize {
        self.disposals.load(Ordering::SeqCst)
    }
}

impl StateUnit for DisposeProbe {
    fn dispose(&self) {
        self.disposals.fetch_add(1, Ordering::SeqCst);
    }
}

impl Given for DisposeProbe {
    fn disposed(&self) -> bool {
        self.dispose_count() > 0
    }
}

/// A freshly built unit, and what it was built with.
pub struct Fresh<U> {
    pub unit: U,
    /// What it was given as dependencies.
    pub passed_in: Vec<Box<dyn Given>>,
}

impl<U> Fresh<U> {
    pub fn of(unit: U) -> Fresh<U> {
        Fresh {
            unit,
            passed_in: Vec::new(),
        }
    }

    /// The same, with one more dependency the unit was given.
    pub fn given(mut self, dependency: impl Given + 'static) -> Fresh<U> {
        self.passed_in.push(Box::new(dependency));
        self
    }
}

/// How the axioms reach one kind of state unit. Only `setup` is required: an
/// accessor left at its default skips the axioms that need it.
pub trait AxiomSubject {
    type Unit: StateUnit;

    /// Build a fresh unit, independent of every other one.
    fn setup(&self) -> Fresh<Self::Unit>;

    /// A new reader of each surface the unit owns.
    fn surfaces(&self, _unit: &Self::Unit) -> Vec<Box<dyn Surface>> {
        Vec::new()
    }

    /// Each of the unit's methods, as a call with no arguments.
    fn invocations<'a>(&self, _unit: &'a Self::Unit) -> Vec<Box<dyn Fn() + 'a>> {
        Vec::new()
    }

    /// A new reader of each surface of the children the unit made itself.
    fn owned_child_surfaces(&self, _unit: &Self::Unit) -> Vec<Box<dyn Surface>> {
        Vec::new()
    }
}

const CASES: u32 = 30;

/// Let whatever the unit spawned act on what just happened.
async fn settle() {
    tokio::time::sleep(Duration::from_millis(1)).await;
}

fn property<T: Debug>(
    axiom: &str,
    values: impl Strategy<Value = T>,
    check: impl Fn(T) -> Result<(), String>,
) -> Result<(), String> {
    let mut runner = TestRunner::new(Config {
        cases: CASES,
        failure_persistence: None,
        ..Config::default()
    });
    runner
        .run(&values, |value| {
            match catch_unwind(AssertUnwindSafe(|| check(value))) {
                Ok(Ok(())) => Ok(()),
                Ok(Err(violation)) => Err(TestCaseError::fail(violation)),
                Err(_) => Err(TestCaseError::fail("the unit panicked")),
            }
        })
        .map_err(|failure| format!("{axiom}: {failure}"))
}

/// Run every axiom the subject gives the means to check. `Err` names the
/// first axiom broken and how.
pub fn assert_state_unit_axioms<S: AxiomSubject>(subject: &S) -> Result<(), String> {
    let runtime = tokio::runtime::Builder::new_current_thread()
        .enable_time()
        .start_paused(true)
        .build()
        .map_err(|e| format!("cannot build a runtime for the axioms: {e}"))?;
    let undisposed = |fresh: &Fresh<S::Unit>, when: &str| -> Result<(), String> {
        match fresh.passed_in.iter().position(|given| given.disposed()) {
            Some(i) => Err(format!(
                "the unit disposed dependency #{i} it was given {when}"
            )),
            None => Ok(()),
        }
    };

    // A7-passed
    runtime
        .block_on(async {
            let fresh = subject.setup();
            fresh.unit.dispose();
            settle().await;
            undisposed(&fresh, "(it does not own it)")
        })
        .map_err(|violation| format!("A7-passed: {violation}"))?;

    // A5
    property("A5", 1usize..=20, |n| {
        runtime.block_on(async {
            let fresh = subject.setup();
            for _ in 0..n {
                fresh.unit.dispose();
            }
            settle().await;
            undisposed(&fresh, &format!("under {n} dispose() calls"))
        })
    })?;

    // A6
    if !runtime.block_on(async { subject.surfaces(&subject.setup().unit).is_empty() }) {
        property("A6", 1usize..=10, |k| {
            runtime.block_on(async {
                let fresh = subject.setup();
                let mut readers: Vec<Box<dyn Surface>> =
                    (0..k).flat_map(|_| subject.surfaces(&fresh.unit)).collect();
                fresh.unit.dispose();
                settle().await;
                match readers.iter_mut().position(|reader| !reader.ended()) {
                    Some(i) => Err(format!(
                        "reader #{i} of a surface did not see it end on dispose (k={k})"
                    )),
                    None => Ok(()),
                }
            })
        })?;
    }

    // A5b
    property("A5b", vec(0usize..1000, 0..=12), |calls| {
        runtime.block_on(async {
            let fresh = subject.setup();
            fresh.unit.dispose();
            {
                let invocations = subject.invocations(&fresh.unit);
                for call in &calls {
                    if invocations.is_empty() {
                        break;
                    }
                    invocations[call % invocations.len()]();
                }
            }
            settle().await;
            for (i, surface) in subject.surfaces(&fresh.unit).iter_mut().enumerate() {
                if surface.moved() {
                    return Err(format!("surface #{i} gave a value after dispose"));
                }
                if !surface.ended() {
                    return Err(format!("surface #{i}, taken after dispose, has not ended"));
                }
            }
            Ok(())
        })
    })?;

    // X3
    property("X3", vec(0usize..1000, 1..=8), |calls| {
        runtime.block_on(async {
            let a = subject.setup();
            let b = subject.setup();
            let mut watched = subject.surfaces(&b.unit);
            settle().await;
            for surface in watched.iter_mut() {
                surface.moved();
            }
            {
                let invocations = subject.invocations(&a.unit);
                for call in &calls {
                    if invocations.is_empty() {
                        break;
                    }
                    invocations[call % invocations.len()]();
                }
            }
            settle().await;
            let perturbed = watched.iter_mut().position(|surface| surface.moved());
            a.unit.dispose();
            b.unit.dispose();
            settle().await;
            match perturbed {
                Some(i) => Err(format!(
                    "driving one instance moved surface #{i} of another"
                )),
                None => Ok(()),
            }
        })
    })?;

    // A7-owned
    runtime
        .block_on(async {
            let fresh = subject.setup();
            let mut children = subject.owned_child_surfaces(&fresh.unit);
            fresh.unit.dispose();
            settle().await;
            match children.iter_mut().position(|child| !child.ended()) {
                Some(i) => Err(format!(
                    "the surface #{i} of a child the unit made did not end when the unit was disposed"
                )),
                None => Ok(()),
            }
        })
        .map_err(|violation| format!("A7-owned: {violation}"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testing::{TestClientOptions, create_test_client};
    use std::sync::Mutex;

    type Value = Arc<Mutex<Option<watch::Sender<u32>>>>;

    /// The ways a counter can break the axioms, one switch each.
    #[derive(Clone, Copy, Default)]
    struct Faults {
        dispose_panics_twice: bool,
        keeps_its_sender: bool,
        revives_after_dispose: bool,
        disposes_what_it_was_given: bool,
        increments_every_instance: bool,
        keeps_its_child: bool,
    }

    /// A counter: one state, one method, one dependency it was given and one
    /// child it made.
    struct Counter {
        faults: Faults,
        value: Value,
        reader: Mutex<watch::Receiver<u32>>,
        /// Every counter its subject built, itself among them.
        instances: Arc<Mutex<Vec<Value>>>,
        given: DisposeProbe,
        child: Mutex<Option<watch::Sender<u32>>>,
        child_reader: watch::Receiver<u32>,
        disposals: AtomicUsize,
    }

    impl Counter {
        fn new(faults: Faults, given: DisposeProbe, instances: Arc<Mutex<Vec<Value>>>) -> Counter {
            let (value, reader) = watch::channel(0);
            let value: Value = Arc::new(Mutex::new(Some(value)));
            instances.lock().unwrap().push(value.clone());
            let (child, child_reader) = watch::channel(0);
            Counter {
                faults,
                value,
                reader: Mutex::new(reader),
                instances,
                given,
                child: Mutex::new(Some(child)),
                child_reader,
                disposals: AtomicUsize::new(0),
            }
        }

        fn increment(&self) {
            let bump = |value: &Value| {
                if let Some(value) = value.lock().unwrap().as_ref() {
                    value.send_modify(|n| *n += 1);
                }
            };
            if self.faults.increments_every_instance {
                self.instances.lock().unwrap().iter().for_each(bump);
            } else {
                bump(&self.value);
            }
        }
    }

    impl StateUnit for Counter {
        fn dispose(&self) {
            if self.disposals.fetch_add(1, Ordering::SeqCst) == 1
                && self.faults.dispose_panics_twice
            {
                panic!("disposed twice");
            }
            if self.faults.disposes_what_it_was_given {
                self.given.dispose();
            }
            if self.faults.revives_after_dispose {
                let (value, reader) = watch::channel(0);
                *self.value.lock().unwrap() = Some(value);
                *self.reader.lock().unwrap() = reader;
            } else if !self.faults.keeps_its_sender {
                *self.value.lock().unwrap() = None;
            }
            if !self.faults.keeps_its_child {
                *self.child.lock().unwrap() = None;
            }
        }
    }

    struct Counters {
        faults: Faults,
        instances: Arc<Mutex<Vec<Value>>>,
    }

    impl AxiomSubject for Counters {
        type Unit = Counter;

        fn setup(&self) -> Fresh<Counter> {
            let given = DisposeProbe::new();
            Fresh::of(Counter::new(
                self.faults,
                given.clone(),
                self.instances.clone(),
            ))
            .given(given)
        }

        fn surfaces(&self, unit: &Counter) -> Vec<Box<dyn Surface>> {
            vec![Box::new(unit.reader.lock().unwrap().clone())]
        }

        fn invocations<'a>(&self, unit: &'a Counter) -> Vec<Box<dyn Fn() + 'a>> {
            vec![Box::new(|| unit.increment())]
        }

        fn owned_child_surfaces(&self, unit: &Counter) -> Vec<Box<dyn Surface>> {
            vec![Box::new(unit.child_reader.clone())]
        }
    }

    fn check(faults: Faults) -> Result<(), String> {
        assert_state_unit_axioms(&Counters {
            faults,
            instances: Arc::new(Mutex::new(Vec::new())),
        })
    }

    fn broken(faults: Faults) -> String {
        check(faults).expect_err("the fault must be caught")
    }

    #[test]
    fn a_unit_that_keeps_the_axioms_passes() {
        assert_eq!(check(Faults::default()), Ok(()));
    }

    #[test]
    fn a_dispose_that_is_not_idempotent_breaks_a5() {
        let violation = broken(Faults {
            dispose_panics_twice: true,
            ..Faults::default()
        });
        assert!(violation.starts_with("A5:"), "{violation}");
    }

    #[test]
    fn a_sender_left_open_breaks_a6() {
        let violation = broken(Faults {
            keeps_its_sender: true,
            ..Faults::default()
        });
        assert!(violation.starts_with("A6:"), "{violation}");
    }

    #[test]
    fn a_unit_that_acts_after_dispose_breaks_a5b() {
        let violation = broken(Faults {
            revives_after_dispose: true,
            ..Faults::default()
        });
        assert!(violation.starts_with("A5b:"), "{violation}");
    }

    #[test]
    fn a_unit_that_disposes_what_it_was_given_breaks_a7_passed() {
        let violation = broken(Faults {
            disposes_what_it_was_given: true,
            ..Faults::default()
        });
        assert!(violation.starts_with("A7-passed:"), "{violation}");
    }

    /// A unit over a client, which it closes when it is disposed.
    struct ClosesItsClient(Arc<SemiontClient>);

    impl StateUnit for ClosesItsClient {
        fn dispose(&self) {
            self.0.bus().destroy();
        }
    }

    struct ClientClosers;

    impl AxiomSubject for ClientClosers {
        type Unit = ClosesItsClient;

        fn setup(&self) -> Fresh<ClosesItsClient> {
            let client = create_test_client(TestClientOptions::default()).client;
            Fresh::of(ClosesItsClient(client.clone())).given(client)
        }
    }

    #[test]
    fn a_unit_that_closes_the_client_it_was_given_breaks_a7_passed() {
        let violation =
            assert_state_unit_axioms(&ClientClosers).expect_err("the fault must be caught");
        assert!(violation.starts_with("A7-passed:"), "{violation}");
    }

    #[test]
    fn a_child_left_alive_breaks_a7_owned() {
        let violation = broken(Faults {
            keeps_its_child: true,
            ..Faults::default()
        });
        assert!(violation.starts_with("A7-owned:"), "{violation}");
    }

    #[test]
    fn state_shared_between_instances_breaks_x3() {
        let violation = broken(Faults {
            increments_every_instance: true,
            ..Faults::default()
        });
        assert!(violation.starts_with("X3:"), "{violation}");
    }
}