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.
3.3 kB 117 lines
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118//// How many calls run at once, the order their answers come back in, and//// what a nested map does are scheduling properties no HTTP test can pin.
import albedo/harness/concurrentimport gleam/erlang/process.{type Subject}import gleam/intimport gleam/listimport gleam/resultimport gleam/stringimport gleeunit/should
type Count { Enter Leave Inside(Subject(Int)) Peak(Subject(Int))}
/// A process counting the workers inside at once, and the most there were.fn counter() -> Subject(Count) { let ready = process.new_subject() process.spawn_unlinked(fn() { let inbox = process.new_subject() process.send(ready, inbox) count(inbox, 0, 0) }) let assert Ok(inbox) = process.receive(ready, 1000) inbox}
fn count(inbox: Subject(Count), inside: Int, peak: Int) -> Nil { case process.receive_forever(inbox) { Enter -> count(inbox, inside + 1, int.max(peak, inside + 1)) Leave -> count(inbox, inside - 1, peak) Inside(reply) -> { process.send(reply, inside) count(inbox, inside, peak) } // The last question a test asks; the counter ends with it. Peak(reply) -> process.send(reply, peak) }}
fn ask(counter: Subject(Count), question: fn(Subject(Int)) -> Count) -> Int { let reply = process.new_subject() process.send(counter, question(reply)) let assert Ok(answer) = process.receive(reply, 1000) answer}
fn timed(counter: Subject(Count), value: a, ms: Int) -> Result(a, String) { process.send(counter, Enter) process.sleep(ms) process.send(counter, Leave) Ok(value)}
/// Waits until `others` workers are inside with this one, which only/// happens if they run side by side.fn together( counter: Subject(Count), others: Int, tries: Int,) -> Result(Nil, String) { case ask(counter, Inside), tries { inside, _ if inside >= others -> Ok(Nil) _, 0 -> Error("the items never ran at once") _, _ -> { process.sleep(10) together(counter, others, tries - 1) } }}
pub fn items_up_to_the_limit_run_at_once_test() -> Nil { let counter = counter() concurrent.map([1, 2, 3], 3, fn(item) { process.send(counter, Enter) use _ <- result.try(together(counter, 3, 200)) Ok(item) }) |> should.equal([Ok(1), Ok(2), Ok(3)]) let _ = ask(counter, Peak) Nil}
pub fn no_more_than_the_limit_run_and_answers_keep_their_order_test() -> Nil { let counter = counter() // Later items finish first. let items = [1, 2, 3, 4, 5, 6, 7, 8] items |> concurrent.map(3, fn(item) { timed(counter, item, 5 * { 9 - item }) }) |> should.equal(list.map(items, Ok)) should.be_true(ask(counter, Peak) <= 3)}
pub fn a_crash_is_its_own_items_error_test() -> Nil { let assert [Ok(1), Error(crash), Ok(3)] = concurrent.map([1, 2, 3], 3, fn(item) { case item { 2 -> panic as "the second item fails" _ -> Ok(item) } }) should.be_true(string.contains(crash, "the second item fails"))}
pub fn a_map_inside_a_worker_runs_one_item_at_a_time_test() -> Nil { let counter = counter() concurrent.map([1, 2], 2, fn(outer) { concurrent.map([1, 2, 3], 3, fn(inner) { timed(counter, inner, 20) }) |> list.length |> fn(length) { Ok(outer * length) } }) |> should.equal([Ok(3), Ok(6)]) should.be_true(ask(counter, Peak) <= 2)}