Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
2.9 kB 82 lines
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283//// The verdict on a kernel that stops answering decides whether it is ended//// at once or left to its deadline, from two CPU samples of its process. E2E//// shows each verdict once; the boundary between them, the samples the//// process reader cannot give, and ps's time formats are checked here.
import gleam/erlang/atom.{type Atom}import gleeunit/should
@external(erlang, "albedo_python", "classify")fn classify( remote: Bool, sample: #(Atom, Int), first: #(Int, Int), now: Int,) -> Atom
@external(erlang, "albedo_python", "classify")fn classify_unsampled(remote: Bool, sample: Atom, first: Atom, now: Int) -> Atom
@external(erlang, "albedo_python", "classify")fn classify_without_cpu( remote: Bool, sample: #(Atom, Atom), first: Atom, now: Int,) -> Atom
@external(erlang, "albedo_python", "cpu_ms")fn cpu_ms(time: String) -> Int
fn live() -> Atom { atom.create("live")}
pub fn a_process_using_no_cpu_across_the_window_is_asleep_test() -> Nil { // 10 s without an answer, 40 ms of CPU: a C call blocking with the GIL. classify(False, #(live(), 1040), #(0, 1000), 10_000) |> should.equal(atom.create("asleep")) // Exactly a twentieth of the window on the CPU is already spinning. classify(False, #(live(), 1500), #(0, 1000), 10_000) |> should.equal(atom.create("spinning")) classify(False, #(live(), 1499), #(0, 1000), 10_000) |> should.equal(atom.create("asleep"))}
pub fn a_process_computing_with_the_gil_held_is_spinning_test() -> Nil { classify(False, #(live(), 10_900), #(0, 1000), 10_000) |> should.equal(atom.create("spinning"))}
pub fn a_stopped_or_gone_process_is_said_so_whatever_its_cpu_test() -> Nil { classify(False, #(atom.create("stopped"), 10_900), #(0, 1000), 10_000) |> should.equal(atom.create("stopped")) classify_unsampled(False, atom.create("gone"), atom.create("none"), 10_000) |> should.equal(atom.create("gone"))}
pub fn a_remote_or_unreadable_process_is_only_unresponsive_test() -> Nil { // A remote kernel's pid is another machine's: no verdict ends it early. classify(True, #(live(), 1000), #(0, 1000), 10_000) |> should.equal(atom.create("unresponsive")) classify_unsampled(False, atom.create("unknown"), atom.create("none"), 10_000) |> should.equal(atom.create("unresponsive")) classify_without_cpu( False, #(live(), atom.create("unknown")), atom.create("none"), 10_000, ) |> should.equal(atom.create("unresponsive")) // No window yet: the first sample is this one. classify(False, #(live(), 1000), #(10_000, 1000), 10_000) |> should.equal(atom.create("unresponsive"))}
pub fn ps_cpu_times_are_read_in_every_format_it_prints_test() -> Nil { cpu_ms("0:00.05") |> should.equal(50) cpu_ms("0:01.23") |> should.equal(1230) cpu_ms("12:34.56") |> should.equal(754_560) cpu_ms("1:02:03.45") |> should.equal(3_723_450) cpu_ms("00:00:07") |> should.equal(7000)}