A deterministic hierarchical timing wheel and virtual-time scheduler for MoonBit.
moon add brother-666/moonwheelkit///|
test "schedule with deterministic virtual time" {
let wheel = @moonwheelkit.Wheel::new()
ignore(wheel.schedule(@moonwheelkit.TimerSpec::once(3, "expire")))
match wheel.advance_to(3) {
@moonwheelkit.AdvanceResult::Advanced(report) => {
assert_eq(report.fired.length(), 1)
assert_eq(report.fired[0].payload, "expire")
}
@moonwheelkit.AdvanceResult::Rejected(_) =>
fail("forward advance should succeed")
}
}match wheel.advance_late_budgeted(1000, 256) {
@moonwheelkit.DrainResult::Drained(report) => {
println(report.to_json())
if report.exhausted {
// Continue draining at tick 1000 in a later event-loop turn.
}
}
@moonwheelkit.DrainResult::Rejected(_) => ()
}pub(all) struct TimerInfo {
id : Int
deadline : Int
period : Int
repeat : RepeatMode
payload : String
state : TimerState
sequence : Int
generation : Int
occurrence : Int
} derive(Eq, Debug)pub(all) struct TimerSnapshot {
now : Int
next_id : Int
next_sequence : Int
fired_total : Int
scheduled_total : Int
cascades_total : Int
config : WheelConfig
timers : Array[TimerInfo]
} derive(Eq, Debug)pub(all) struct TimerSpec {
delay : Int
period : Int
repeat : RepeatMode
payload : String
} derive(Eq, Debug)pub struct Wheel {
config : WheelConfig
current_tick : Int
next_id : Int
next_sequence : Int
timers : Map[Int, InternalTimer]
buckets : Array[Array[BucketEntry]]
fired_total : Int
scheduled_total : Int
cascades_total : Int
}A deterministic hierarchical timing wheel and virtual-time scheduler for MoonBit.