reovim-kernel 0.14.3

Core kernel mechanisms for reovim (Linux kernel/ equivalent)
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
use {
    super::super::*,
    crate::ipc::{EventBus, EventScope},
    std::sync::{
        Arc,
        atomic::{AtomicBool, AtomicUsize, Ordering},
    },
};

// === Basic tests ===

#[test]
fn test_runtime_new() {
    let runtime = Runtime::new();
    assert_eq!(runtime.state(), RuntimeState::Booting);
    assert!(runtime.is_idle());
}

#[test]
fn test_runtime_with_config() {
    let config = RuntimeConfig {
        work_queue_capacity: 100,
        priority_queue_capacity: 50,
        batch_size: 8,
        max_timers: 64,
    };
    let runtime = Runtime::with_config(config);
    assert_eq!(runtime.state(), RuntimeState::Booting);
}

#[test]
fn test_runtime_boot() {
    let mut runtime = Runtime::new();
    assert_eq!(runtime.state(), RuntimeState::Booting);

    runtime.boot();
    assert_eq!(runtime.state(), RuntimeState::Running);
}

#[test]
#[should_panic(expected = "boot() called when not in Booting state")]
fn test_runtime_boot_twice_panics() {
    let mut runtime = Runtime::new();
    runtime.boot();
    runtime.boot(); // Should panic
}

// === Shutdown tests ===

#[test]
fn test_runtime_shutdown() {
    let mut runtime = Runtime::new();
    runtime.boot();
    assert_eq!(runtime.state(), RuntimeState::Running);

    runtime.shutdown();
    assert_eq!(runtime.state(), RuntimeState::Stopping);
}

#[test]
fn test_runtime_emergency_stop() {
    let mut runtime = Runtime::new();
    runtime.boot();

    runtime.emergency_stop();
    assert_eq!(runtime.state(), RuntimeState::Emergency);
}

#[test]
fn test_runtime_shutdown_via_command() {
    let mut runtime = Runtime::new();
    runtime.boot();

    let sender = runtime.command_sender();
    sender.send(RuntimeCommand::Shutdown).unwrap();

    runtime.tick();
    assert_eq!(runtime.state(), RuntimeState::Stopping);
}

// === Task scheduling tests ===

#[test]
fn test_runtime_schedule_work() {
    let mut runtime = Runtime::new();
    runtime.boot();

    let executed = Arc::new(AtomicBool::new(false));
    let executed_clone = Arc::clone(&executed);

    assert!(runtime.schedule_work(move || {
        executed_clone.store(true, Ordering::SeqCst);
    }));

    runtime.tick();
    assert!(executed.load(Ordering::SeqCst));
}

#[test]
fn test_runtime_schedule_work_not_running() {
    let runtime = Runtime::new(); // Still booting
    assert!(!runtime.schedule_work(|| {}));
}

#[test]
fn test_runtime_schedule_task() {
    let mut runtime = Runtime::new();
    runtime.boot();

    let executed = Arc::new(AtomicBool::new(false));
    let executed_clone = Arc::clone(&executed);

    let task = Task::new(move || {
        executed_clone.store(true, Ordering::SeqCst);
    });

    assert!(runtime.schedule_task(task));
    runtime.tick();
    assert!(executed.load(Ordering::SeqCst));
}

#[test]
fn test_runtime_schedule_work_with_priority() {
    let mut runtime = Runtime::new();
    runtime.boot();

    let order = Arc::new(std::sync::Mutex::new(Vec::new()));

    let order1 = Arc::clone(&order);
    runtime.schedule_work_with_priority(Priority::LOW, move || {
        order1.lock().unwrap().push("low");
    });

    let order2 = Arc::clone(&order);
    runtime.schedule_work_with_priority(Priority::HIGH, move || {
        order2.lock().unwrap().push("high");
    });

    // Note: WorkQueue is FIFO, not priority-ordered
    // Priority only matters for tasks in PriorityQueue
    runtime.tick();
    runtime.tick();

    let result = order.lock().unwrap().clone();
    // Tasks are executed in FIFO order from WorkQueue
    assert_eq!(result.len(), 2);
}

// === Render pending tests ===

#[test]
fn test_runtime_render_pending() {
    let mut runtime = Runtime::new();

    assert!(!runtime.is_render_pending());

    runtime.request_render();
    assert!(runtime.is_render_pending());

    assert!(runtime.take_render_pending());
    assert!(!runtime.is_render_pending());
}

// === Scope tests ===

#[test]
fn test_runtime_scope() {
    let mut runtime = Runtime::new();
    assert!(runtime.current_scope().is_none());

    let scope = EventScope::new();
    runtime.set_scope(scope);
    assert!(runtime.current_scope().is_some());

    runtime.clear_scope();
    assert!(runtime.current_scope().is_none());
}

// === Idle tests ===

#[test]
fn test_runtime_is_idle() {
    let mut runtime = Runtime::new();
    runtime.boot();
    assert!(runtime.is_idle());

    runtime.schedule_work(|| {});
    assert!(!runtime.is_idle());

    runtime.tick();
    assert!(runtime.is_idle());
}

// === Stats tests ===

#[test]
fn test_runtime_stats() {
    let mut runtime = Runtime::new();
    runtime.boot();

    runtime.schedule_work(|| {});
    runtime.schedule_work(|| panic!("intentional panic"));

    let stats = runtime.stats();
    assert_eq!(stats.state, RuntimeState::Running);
    assert_eq!(stats.work_queue_len, 2);
    assert_eq!(stats.tasks_executed, 0);

    runtime.tick();

    let stats = runtime.stats();
    assert_eq!(stats.work_queue_len, 0);
    // One succeeded, one failed
    assert_eq!(stats.tasks_executed, 1);
    assert_eq!(stats.tasks_failed, 1);
}

// === Tick behavior tests ===

#[test]
fn test_runtime_tick_returns_false_on_stop() {
    let mut runtime = Runtime::new();
    runtime.boot();

    assert!(runtime.tick());

    runtime.shutdown();
    assert!(!runtime.tick());
}

#[test]
fn test_runtime_tick_drains_on_shutdown() {
    let mut runtime = Runtime::new();
    runtime.boot();

    let counter = Arc::new(AtomicUsize::new(0));

    for _ in 0..5 {
        let counter_clone = Arc::clone(&counter);
        runtime.schedule_work(move || {
            counter_clone.fetch_add(1, Ordering::SeqCst);
        });
    }

    runtime.shutdown();
    runtime.tick(); // Should drain all tasks

    assert_eq!(counter.load(Ordering::SeqCst), 5);
}

// === Run tests ===

#[test]
fn test_runtime_run() {
    let mut runtime = Runtime::new();
    runtime.boot();

    let counter = Arc::new(AtomicUsize::new(0));
    let counter_clone = Arc::clone(&counter);

    // Schedule work that will trigger shutdown
    runtime.schedule_work(move || {
        counter_clone.fetch_add(1, Ordering::SeqCst);
    });

    // Send shutdown command
    let sender = runtime.command_sender();
    sender.send(RuntimeCommand::Shutdown).unwrap();

    runtime.run();

    assert_eq!(counter.load(Ordering::SeqCst), 1);
    assert!(runtime.state().is_shutting_down());
}

// === Debug tests ===

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn test_runtime_debug() {
    let runtime = Runtime::new();
    let debug_str = format!("{runtime:?}");
    assert!(debug_str.contains("Runtime"));
    assert!(debug_str.contains("Booting"));
}

// === Event bus integration ===

#[test]
fn test_runtime_with_event_bus() {
    let bus = Arc::new(EventBus::new());
    let bus_clone = Arc::clone(&bus);

    let runtime = Runtime::new().with_event_bus(bus);

    // Both should point to the same EventBus
    assert!(Arc::ptr_eq(runtime.event_bus(), &bus_clone));
}

// === Command channel tests ===

#[test]
fn test_runtime_command_schedule_task() {
    let mut runtime = Runtime::new();
    runtime.boot();

    let executed = Arc::new(AtomicBool::new(false));
    let executed_clone = Arc::clone(&executed);

    let task = Task::new(move || {
        executed_clone.store(true, Ordering::SeqCst);
    });

    let sender = runtime.command_sender();
    sender.send(RuntimeCommand::ScheduleTask(task)).unwrap();

    runtime.tick();
    runtime.tick(); // Need second tick to execute the scheduled task

    assert!(executed.load(Ordering::SeqCst));
}

// === Send test ===

#[test]
fn test_runtime_send() {
    fn assert_send<T: Send>() {}
    assert_send::<Runtime>();
}

// === schedule_delayed ===

#[test]
fn test_runtime_schedule_delayed() {
    let mut runtime = Runtime::new();
    runtime.boot();

    let counter = Arc::new(AtomicUsize::new(0));
    let counter_clone = Arc::clone(&counter);

    let handle = runtime.schedule_delayed(std::time::Duration::ZERO, move || {
        counter_clone.fetch_add(1, Ordering::SeqCst);
    });
    assert!(!handle.is_failed());

    // Tick to fire timer and schedule work, then execute
    runtime.tick();
    runtime.tick();

    assert_eq!(counter.load(Ordering::SeqCst), 1);
}

// === schedule_periodic ===

#[test]
fn test_runtime_schedule_periodic() {
    let mut runtime = Runtime::new();
    runtime.boot();

    let counter = Arc::new(AtomicUsize::new(0));
    let counter_clone = Arc::clone(&counter);

    let handle = runtime.schedule_periodic(std::time::Duration::ZERO, move || {
        counter_clone.fetch_add(1, Ordering::SeqCst);
    });
    let _ = handle.detach();

    // Tick multiple times
    for _ in 0..4 {
        runtime.tick();
    }

    assert!(counter.load(Ordering::SeqCst) >= 1);
}

// === cancel_timer ===

#[test]
fn test_runtime_cancel_timer() {
    let mut runtime = Runtime::new();
    runtime.boot();

    let handle = runtime.schedule_delayed(std::time::Duration::from_mins(1), || {});
    let id = handle.detach();
    assert!(runtime.cancel_timer(id));
}

// === timer_wheel() ===

#[test]
fn test_runtime_timer_wheel() {
    let runtime = Runtime::new();
    let wheel = runtime.timer_wheel();
    assert_eq!(wheel.pending_count(), 0);
}

// === queue_event ===

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn test_runtime_queue_event() {
    use crate::ipc::{DynEvent, Event};

    #[derive(Debug)]
    struct TestEvent;
    impl Event for TestEvent {}

    let mut runtime = Runtime::new();
    runtime.boot();

    let event = DynEvent::new(TestEvent);
    assert!(runtime.queue_event(event));
}

// === Emergency command ===

#[test]
fn test_runtime_emergency_via_command() {
    let mut runtime = Runtime::new();
    runtime.boot();

    let sender = runtime.command_sender();
    sender.send(RuntimeCommand::Emergency).unwrap();

    runtime.tick();
    assert_eq!(runtime.state(), RuntimeState::Emergency);
}

// === schedule when not accepting work ===

#[test]
fn test_runtime_schedule_work_when_stopping() {
    let mut runtime = Runtime::new();
    runtime.boot();
    runtime.shutdown();

    // Stopping state should not accept new work
    assert!(!runtime.schedule_work(|| {}));
}

#[test]
fn test_runtime_schedule_task_not_running() {
    let runtime = Runtime::new(); // Booting state
    let task = Task::new(|| {});
    assert!(!runtime.schedule_task(task));
}

// === Default ===

#[test]
fn test_runtime_default() {
    let runtime = Runtime::default();
    assert_eq!(runtime.state(), RuntimeState::Booting);
}

// === work_queue accessor ===

#[test]
fn test_runtime_work_queue() {
    let runtime = Runtime::new();
    assert!(runtime.work_queue().is_empty());
}

// === schedule_work_with_priority when not running ===

#[test]
fn test_runtime_schedule_work_with_priority_not_running() {
    let runtime = Runtime::new(); // Booting state
    assert!(!runtime.schedule_work_with_priority(Priority::HIGH, || {}));
}

// === Coverage: ScheduleTask via command when not accepting work ===

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn test_runtime_command_schedule_task_when_stopping() {
    let mut runtime = Runtime::new();
    runtime.boot();

    let executed = Arc::new(AtomicBool::new(false));
    let executed_clone = Arc::clone(&executed);

    let sender = runtime.command_sender();
    sender.send(RuntimeCommand::Shutdown).unwrap();
    sender
        .send(RuntimeCommand::ScheduleTask(Task::new(move || {
            executed_clone.store(true, Ordering::SeqCst);
        })))
        .unwrap();

    runtime.tick();
    assert_eq!(runtime.state(), RuntimeState::Stopping);
}

// === Coverage: shutdown when not running (from Booting) ===

#[test]
fn test_runtime_shutdown_from_booting() {
    let mut runtime = Runtime::new();
    runtime.shutdown();
    assert_eq!(runtime.state(), RuntimeState::Booting);
}

// === Coverage: emergency_stop from any state ===

#[test]
fn test_runtime_emergency_stop_from_booting() {
    let mut runtime = Runtime::new();
    runtime.emergency_stop();
    assert_eq!(runtime.state(), RuntimeState::Emergency);
}

// === Coverage: dispatch_events with scope ===

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn test_runtime_dispatch_events_with_scope() {
    use crate::ipc::{DynEvent, Event, EventScope};

    #[derive(Debug)]
    struct ScopeTestEvent;
    impl Event for ScopeTestEvent {}

    let mut runtime = Runtime::new();
    runtime.boot();

    let scope = EventScope::new();
    scope.increment();
    scope.increment();
    runtime.set_scope(scope.clone());

    let event = DynEvent::new(ScopeTestEvent);
    runtime.queue_event(event);

    runtime.tick();
    assert!(scope.in_flight() < 2);
}

// === Coverage: RuntimeConfig Debug/Clone ===

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn test_runtime_config_debug_clone() {
    let config = RuntimeConfig::default();
    let cloned = config;
    assert_eq!(cloned.batch_size, DEFAULT_BATCH_SIZE);
    let debug = format!("{config:?}");
    assert!(debug.contains("RuntimeConfig"));
}

// === Coverage: RuntimeCommand Debug ===

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn test_runtime_command_debug() {
    let cmd = RuntimeCommand::Shutdown;
    let debug = format!("{cmd:?}");
    assert!(debug.contains("Shutdown"));
}

// === Coverage: RuntimeStats Default ===

#[test]
fn test_runtime_stats_default() {
    let stats = RuntimeStats::default();
    assert_eq!(stats.state, RuntimeState::Booting);
    assert_eq!(stats.tasks_executed, 0);
}

// === Coverage: tick with Emergency state ===

#[test]
fn test_runtime_tick_emergency_returns_false() {
    let mut runtime = Runtime::new();
    runtime.boot();
    runtime.emergency_stop();
    assert!(!runtime.tick());
}

// === MC/DC: schedule_work() when state is Emergency (not accepting work) ===

#[test]
fn test_runtime_schedule_work_when_emergency() {
    // Emergency state: can_accept_work() == false -> return false.
    // This exercises the `!self.state.can_accept_work()` true branch of
    // schedule_work() from a different non-accepting state than Booting.
    let mut runtime = Runtime::new();
    runtime.boot();
    runtime.emergency_stop();
    assert_eq!(runtime.state(), RuntimeState::Emergency);
    assert!(!runtime.schedule_work(|| {}));
}

// === MC/DC: schedule_work_with_priority() when state is Emergency ===

#[test]
fn test_runtime_schedule_work_with_priority_when_emergency() {
    // Emergency state: can_accept_work() == false -> return false.
    // Covers the rejection branch of schedule_work_with_priority() from
    // a state distinct from Booting (already tested) to satisfy MC/DC.
    let mut runtime = Runtime::new();
    runtime.boot();
    runtime.emergency_stop();
    assert!(!runtime.schedule_work_with_priority(Priority::HIGH, || {}));
}

// === MC/DC: schedule_task() when state is Emergency ===

#[test]
fn test_runtime_schedule_task_when_emergency() {
    // Emergency state: can_accept_work() == false -> return false.
    // Covers the rejection branch of schedule_task() from Emergency state.
    let mut runtime = Runtime::new();
    runtime.boot();
    runtime.emergency_stop();
    let task = Task::new(|| {});
    assert!(!runtime.schedule_task(task));
}

// === MC/DC: shutdown() when state is already Stopping (no-op) ===

#[test]
fn test_runtime_shutdown_when_already_stopping() {
    // state == Stopping: the `if self.state == RuntimeState::Running` condition
    // is false -> no-op. State remains Stopping.
    // This provides a distinct false-branch case from test_runtime_shutdown_from_booting.
    let mut runtime = Runtime::new();
    runtime.boot();
    runtime.shutdown(); // Running -> Stopping
    assert_eq!(runtime.state(), RuntimeState::Stopping);

    // Second shutdown call: state is Stopping, not Running -> no state change
    runtime.shutdown();
    assert_eq!(runtime.state(), RuntimeState::Stopping);
}