simu-des 0.1.0

Discrete-event simulation for Rust, inspired by SimPy — single-threaded async executor, resources, and Monte Carlo parallelism.
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
// SPDX-FileCopyrightText: Copyright (c) Siemens 2026 contributed by Christoph Kuhmuench christoph.kuhmuench@gmail.com
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::cell::RefCell;
use std::rc::Rc;

use simu::SimEnv;
use simu::Container;

type Log = Rc<RefCell<Vec<String>>>;
fn new_log() -> Log { Rc::new(RefCell::new(Vec::new())) }

// ---------------------------------------------------------------------------
// Immediate resolution
// ---------------------------------------------------------------------------

#[test]
fn get_immediate_when_level_sufficient() {
    let mut env = SimEnv::with_seed(0);
    let h = env.handle();
    let c = Container::new(10.0, 5.0);
    let log = new_log();

    {
        let c = c.clone();
        let log = log.clone();
        env.spawn(async move {
            c.get(3.0).await;
            log.borrow_mut().push(format!("level:{}", c.level()));
        });
    }

    env.run();
    assert_eq!(*log.borrow(), vec!["level:2"]);
    // Time should be 0 — no suspension occurred.
    assert_eq!(h.now(), 0.0);
}

#[test]
fn put_immediate_when_space_available() {
    let mut env = SimEnv::with_seed(0);
    let h = env.handle();
    let c = Container::empty(10.0);
    let log = new_log();

    {
        let c = c.clone();
        let log = log.clone();
        env.spawn(async move {
            c.put(4.0).await;
            log.borrow_mut().push(format!("level:{}", c.level()));
        });
    }

    env.run();
    assert_eq!(*log.borrow(), vec!["level:4"]);
    assert_eq!(h.now(), 0.0);
}

// ---------------------------------------------------------------------------
// Blocking and waking
// ---------------------------------------------------------------------------

#[test]
fn get_blocks_then_wakes_on_put() {
    let mut env = SimEnv::with_seed(0);
    let c = Container::empty(10.0);
    let log = new_log();

    // Process A: wait 5 time units, then put 3.
    {
        let h = env.handle();
        let c = c.clone();
        env.spawn(async move {
            h.timeout(5.0).await;
            c.put(3.0).await;
        });
    }

    // Process B: immediately try to get 3 — blocks until A's put.
    {
        let h = env.handle();
        let c = c.clone();
        let log = log.clone();
        env.spawn(async move {
            c.get(3.0).await;
            log.borrow_mut().push(format!("B_got:{}", h.now()));
        });
    }

    env.run();
    assert_eq!(*log.borrow(), vec!["B_got:5"]);
}

#[test]
fn put_blocks_then_wakes_on_get() {
    let mut env = SimEnv::with_seed(0);
    let c = Container::new(5.0, 5.0); // starts full
    let log = new_log();

    // Process A: wait 3 time units, then get 2 (frees space).
    {
        let h = env.handle();
        let c = c.clone();
        env.spawn(async move {
            h.timeout(3.0).await;
            c.get(2.0).await;
        });
    }

    // Process B: immediately try to put 2 — blocks until A's get.
    {
        let h = env.handle();
        let c = c.clone();
        let log = log.clone();
        env.spawn(async move {
            c.put(2.0).await;
            log.borrow_mut().push(format!("B_put:{}", h.now()));
        });
    }

    env.run();
    assert_eq!(*log.borrow(), vec!["B_put:3"]);
}

// ---------------------------------------------------------------------------
// FIFO ordering
// ---------------------------------------------------------------------------

#[test]
fn fifo_ordering_for_get_waiters() {
    let mut env = SimEnv::with_seed(0);
    let c = Container::empty(10.0);
    let log = new_log();

    // Three consumers each wanting 4 units — all block immediately (level=0).
    for name in ["G1", "G2", "G3"] {
        let h = env.handle();
        let c = c.clone();
        let log = log.clone();
        env.spawn(async move {
            c.get(4.0).await;
            log.borrow_mut().push(format!("{}:{}", name, h.now()));
        });
    }

    // Producer: supply one batch per time unit.
    {
        let h = env.handle();
        let c = c.clone();
        env.spawn(async move {
            for _ in 0..3 {
                h.timeout(1.0).await;
                c.put(4.0).await;
            }
        });
    }

    env.run();
    assert_eq!(*log.borrow(), vec!["G1:1", "G2:2", "G3:3"]);
}

#[test]
fn fifo_ordering_for_put_waiters() {
    let mut env = SimEnv::with_seed(0);
    let c = Container::new(4.0, 4.0); // starts full
    let log = new_log();

    // Three producers each wanting to put 4 units — all block (no space).
    for name in ["P1", "P2", "P3"] {
        let h = env.handle();
        let c = c.clone();
        let log = log.clone();
        env.spawn(async move {
            c.put(4.0).await;
            log.borrow_mut().push(format!("{}:{}", name, h.now()));
        });
    }

    // Consumer: drain one batch per time unit.
    {
        let h = env.handle();
        let c = c.clone();
        env.spawn(async move {
            for _ in 0..3 {
                h.timeout(1.0).await;
                c.get(4.0).await;
            }
        });
    }

    env.run();
    assert_eq!(*log.borrow(), vec!["P1:1", "P2:2", "P3:3"]);
}

// Regression: head-of-line FIFO. A freshly-arriving small `get` must NOT take
// level ahead of an already-blocked larger `get`, even when the current level
// would cover the small one. This matches SimPy's Container and the documented
// "served FIFO" contract. (Found by the compare/ SimPy harness: simu used to
// let the small draw bypass the queue.)
//
// Setup: empty cap-10 container.
//   - G_big: get(8) at t=0 -> level 0 < 8 -> blocks (get-waiter #1).
//   - producer puts 5 at t=1: level 0 -> 5. G_big still blocked (5 < 8).
//   - G_small: get(3) arrives at t=2, level is 5 >= 3 but G_big is queued
//     ahead -> G_small must queue behind it, not jump in.
//   - producer puts 5 at t=3: level 5 -> 10. Cascade serves G_big (10 -> 2),
//     then G_small (2 < 3) stays blocked.
//   - producer puts 1 at t=4: level 2 -> 3. Cascade serves G_small.
#[test]
fn fresh_small_get_queues_behind_blocked_large_get() {
    let mut env = SimEnv::with_seed(0);
    let c = Container::empty(10.0);
    let log = new_log();

    // G_big blocks immediately at t=0.
    {
        let h = env.handle();
        let c = c.clone();
        let log = log.clone();
        env.spawn(async move {
            c.get(8.0).await;
            log.borrow_mut().push(format!("G_big:{}", h.now()));
        });
    }
    // G_small arrives later (t=2), when level (5) already covers its request.
    {
        let h = env.handle();
        let c = c.clone();
        let log = log.clone();
        env.spawn(async move {
            h.timeout(2.0).await;
            c.get(3.0).await;
            log.borrow_mut().push(format!("G_small:{}", h.now()));
        });
    }
    // Producer drips material: +5 @ t=1, +5 @ t=3, +1 @ t=4.
    {
        let h = env.handle();
        let c = c.clone();
        env.spawn(async move {
            h.timeout(1.0).await;
            c.put(5.0).await;
            h.timeout(2.0).await; // t=3
            c.put(5.0).await;
            h.timeout(1.0).await; // t=4
            c.put(1.0).await;
        });
    }

    env.run();

    // Strict FIFO: G_big served first (at t=3), G_small only after (t=4) —
    // even though at t=2 the level (5) already covered G_small's 3.
    assert_eq!(*log.borrow(), vec!["G_big:3", "G_small:4"]);
}

// Symmetric head-of-line FIFO for the put queue: a fresh small `put` must not
// take free space ahead of an earlier blocked larger `put`.
//
// Setup: cap-10 container starting full (level 10).
//   - P_big: put(8) at t=0 -> 10 + 8 > 10 -> blocks (put-waiter #1).
//   - consumer gets 5 at t=1: level 10 -> 5. P_big still blocked (5 + 8 > 10).
//   - P_small: put(3) arrives at t=2, space is 5 (5 + 3 <= 10) but P_big is
//     queued ahead -> P_small must queue behind it.
//   - consumer gets 5 at t=3: level 5 -> 0. Cascade serves P_big (0 -> 8),
//     then P_small (8 + 3 > 10) stays blocked.
//   - consumer gets 1 at t=4: level 8 -> 7. Cascade serves P_small (7+3=10).
#[test]
fn fresh_small_put_queues_behind_blocked_large_put() {
    let mut env = SimEnv::with_seed(0);
    let c = Container::new(10.0, 10.0); // starts full
    let log = new_log();

    {
        let h = env.handle();
        let c = c.clone();
        let log = log.clone();
        env.spawn(async move {
            c.put(8.0).await;
            log.borrow_mut().push(format!("P_big:{}", h.now()));
        });
    }
    {
        let h = env.handle();
        let c = c.clone();
        let log = log.clone();
        env.spawn(async move {
            h.timeout(2.0).await;
            c.put(3.0).await;
            log.borrow_mut().push(format!("P_small:{}", h.now()));
        });
    }
    {
        let h = env.handle();
        let c = c.clone();
        env.spawn(async move {
            h.timeout(1.0).await;
            c.get(5.0).await;
            h.timeout(2.0).await; // t=3
            c.get(5.0).await;
            h.timeout(1.0).await; // t=4
            c.get(1.0).await;
        });
    }

    env.run();

    assert_eq!(*log.borrow(), vec!["P_big:3", "P_small:4"]);
}

// ---------------------------------------------------------------------------
// Cascade
// ---------------------------------------------------------------------------

/// `trigger_cascade` must loop: satisfying a blocked `get` frees space and
/// may unblock a subsequent `put`, whose completion may then unblock another
/// `get`, etc. This test sets up a 4-step chain (`get→put→get→put`) that is
/// resolved by a single `get` at t=1, and verifies every waiter resolves in
/// the same cascade pass (no simulated-time advance).
#[test]
fn cascade_chain_get_put_get_put() {
    let mut env = SimEnv::with_seed(0);
    // Capacity 4, level starts at 4 (full).
    let c = Container::new(4.0, 4.0);
    let log = new_log();

    // Two producers — each blocks immediately (container is full).
    for label in ["P1", "P2"] {
        let h = env.handle();
        let c = c.clone();
        let log = log.clone();
        let label = label.to_string();
        env.spawn(async move {
            c.put(4.0).await;
            log.borrow_mut().push(format!("{}:{}", label, h.now()));
        });
    }

    // Two consumers (beyond the initial 4 units) — G1 blocks at level=0
    // after the initial full level is drained; G2 blocks too.
    // First spawn a consumer that drains the initial level so P1 can fill it.
    {
        let h = env.handle();
        let c = c.clone();
        let log = log.clone();
        env.spawn(async move {
            c.get(4.0).await; // drains initial full level (resolves immediately)
            log.borrow_mut().push(format!("G_init:{}", h.now()));
        });
    }
    for label in ["G1", "G2"] {
        let h = env.handle();
        let c = c.clone();
        let log = log.clone();
        let label = label.to_string();
        env.spawn(async move {
            c.get(4.0).await;
            log.borrow_mut().push(format!("{}:{}", label, h.now()));
        });
    }

    env.run();

    // Expected cascade at t=0:
    //  1. G_init takes level 4→0 (initial drain)           → wakes P1
    //  2. P1 puts 4, level 0→4                              → wakes G1
    //  3. G1 takes 4, level 4→0                             → wakes P2
    //  4. P2 puts 4, level 0→4                              → wakes G2
    //  5. G2 takes 4, level 4→0                             → end of cascade
    // All five events resolve at t=0 in a single pass of `trigger_cascade`.
    let entries = log.borrow().clone();
    assert_eq!(entries.len(), 5);
    for entry in &entries {
        assert!(
            entry.ends_with(":0"),
            "expected every cascade step at t=0, got: {:?}",
            entries,
        );
    }
    // FIFO order preserved within the get and put queues.
    assert!(entries.contains(&"G_init:0".to_string()));
    assert!(entries.contains(&"P1:0".to_string()));
    assert!(entries.contains(&"P2:0".to_string()));
    assert!(entries.contains(&"G1:0".to_string()));
    assert!(entries.contains(&"G2:0".to_string()));
}

#[test]
fn cascade_satisfies_multiple_gets() {
    let mut env = SimEnv::with_seed(0);
    let c = Container::empty(100.0);
    let log = new_log();

    // Four consumers each wanting 5 units — all block (level=0).
    for i in 1..=4u32 {
        let h = env.handle();
        let c = c.clone();
        let log = log.clone();
        env.spawn(async move {
            c.get(5.0).await;
            log.borrow_mut().push(format!("G{}:{}", i, h.now()));
        });
    }

    // One large put at t=1 supplies 20 units — should satisfy all four.
    {
        let h = env.handle();
        let c = c.clone();
        env.spawn(async move {
            h.timeout(1.0).await;
            c.put(20.0).await;
        });
    }

    env.run();

    // All four gets resolve at t=1 (same cascade pass).
    assert_eq!(*log.borrow(), vec!["G1:1", "G2:1", "G3:1", "G4:1"]);
}

// Note (strict-FIFO change, 2026-06-28): two earlier regressions
// (`immediate_put_wakes_blocked_put_after_get_drains` and
// `cascade_terminates_on_net_zero_level_delta`, review 2026-06-08 Findings 1 & 2)
// were removed here. Both relied on a fresh *fitting* op of one type slipping
// past an already-blocked *larger* op of the same type to kick a single
// bidirectional cascade pass. That bypass was the FIFO violation fixed in this
// change: a fresh put can no longer take space ahead of an earlier blocked put,
// so those exact setups now deadlock (matching SimPy). The cascade's cross-queue
// waking and work-driven loop are still exercised by `cascade_chain_get_put_get_put`,
// `cascade_satisfies_multiple_gets`, and the put-side mirror below.

// `trigger_cascade` must service *multiple* head-of-queue put-waiters in a single
// pass when one immediate op frees enough space — the put-side mirror of
// `cascade_satisfies_multiple_gets`, and the legal (FIFO-respecting) way to kick a
// cross-queue cascade: an immediate `get` frees space, waking blocked `put`s.
//
// Setup: cap 10, level 10 (full).
//   - P1: put(3) -> 10+3 > 10 -> blocks (put-waiter #1).
//   - P2: put(3) -> blocks (put-waiter #2, FIFO behind P1).
//   - trigger: immediate get(6) at t=1 -> level 10 -> 4 (no get-waiters ahead, so
//     it is legal as an immediate get). The freed space wakes P1 (4+3=7) and then
//     P2 (7+3=10) in one cascade pass.
#[test]
fn immediate_get_cascade_wakes_multiple_blocked_puts() {
    let mut env = SimEnv::with_seed(0);
    let c = Container::new(10.0, 10.0); // starts full
    let log = new_log();

    for name in ["P1", "P2"] {
        let h = env.handle();
        let c = c.clone();
        let log = log.clone();
        env.spawn(async move {
            c.put(3.0).await;
            log.borrow_mut().push(format!("{}:{}", name, h.now()));
        });
    }
    {
        let h = env.handle();
        let c = c.clone();
        env.spawn(async move {
            h.timeout(1.0).await;
            c.get(6.0).await;
        });
    }

    env.run();

    // Both puts serviced at t=1 in the same cascade pass; FIFO order preserved.
    assert_eq!(*log.borrow(), vec!["P1:1", "P2:1"]);
    // 10 (initial) -6 (get) +3 (P1) +3 (P2) = 10.
    assert_eq!(c.level(), 10.0);
}

// ---------------------------------------------------------------------------
// Accessors
// ---------------------------------------------------------------------------

#[test]
fn level_and_capacity_accessors() {
    let mut env = SimEnv::with_seed(0);
    let c = Container::new(10.0, 3.0);

    assert_eq!(c.capacity(), 10.0);
    assert_eq!(c.level(), 3.0);

    {
        let c = c.clone();
        env.spawn(async move {
            c.put(4.0).await; // level → 7
            c.get(2.0).await; // level → 5
        });
    }

    env.run();
    assert_eq!(c.level(), 5.0);
}

// ---------------------------------------------------------------------------
// Panic tests
// ---------------------------------------------------------------------------

#[test]
#[should_panic(expected = "capacity must be positive")]
fn zero_capacity_panics() {
    let _ = Container::new(0.0, 0.0);
}

#[test]
#[should_panic(expected = "capacity must be positive")]
fn negative_capacity_panics() {
    let _ = Container::new(-1.0, 0.0);
}

#[test]
#[should_panic(expected = "initial_level must not exceed capacity")]
fn initial_level_exceeds_capacity_panics() {
    let _ = Container::new(5.0, 6.0);
}

// --- F5: over-capacity requests are programming errors (would block forever) ---

#[test]
#[should_panic(expected = "exceeds capacity")]
fn put_exceeding_capacity_panics() {
    let c = Container::new(10.0, 0.0);
    let _req = c.put(11.0); // panics in put() before the future is built
}

#[test]
#[should_panic(expected = "exceeds capacity")]
fn get_exceeding_capacity_panics() {
    let c = Container::new(10.0, 0.0);
    let _req = c.get(11.0);
}

#[test]
fn amount_equal_to_capacity_is_allowed() {
    // Boundary: amount == capacity is satisfiable, must not panic.
    let c = Container::new(10.0, 10.0);
    let _req = c.get(10.0);
    let c2 = Container::empty(10.0);
    let _req2 = c2.put(10.0);
}