okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

Aly Raffauffeat(daemon): own startup defaults and readinessa6d8ebe

main
1.6 KiB49 linesraw
1/// Concurrent ownership of one home conflicts with the one-daemon E2E harness.
2import albedo/daemon/server
3import gleam/erlang/process
4import gleam/result
5import gleeunit/should
6
7@external(erlang, "albedo_skills_test_support", "fixture")
8fn fixture() -> #(String, String, String)
9
10@external(erlang, "albedo_skills_test_support", "cleanup")
11fn cleanup(root: String) -> Nil
12
13pub fn second_daemon_cannot_claim_a_held_home_test() -> Nil {
14  let #(root, _, home) = fixture()
15  let claimed = process.new_subject()
16  // A subject only delivers to the process that created it, so the owner makes
17  // its own release subject and hands it back.
18  process.spawn(fn() {
19    let release = process.new_subject()
20    process.send(claimed, #(server.claim_home(home), release))
21    process.receive_forever(release)
22  })
23  let assert Ok(#(first, release)) = process.receive(claimed, 5000)
24  first |> should.equal(Ok(Nil))
25
26  server.claim_home(home)
27  |> result.map_error(fn(_) { Nil })
28  |> should.equal(Error(Nil))
29
30  // The connection closes when the owner's resources are freed, which can land
31  // just after its exit is observable, so a fresh claimant retries briefly.
32  process.send(release, Nil)
33  claim_eventually(home, 50) |> should.equal(Ok(Nil))
34
35  cleanup(root)
36}
37
38fn claim_eventually(home: String, attempts: Int) -> Result(Nil, Nil) {
39  let reply = process.new_subject()
40  process.spawn(fn() { process.send(reply, server.claim_home(home)) })
41  case process.receive(reply, 5000) {
42    Ok(Ok(Nil)) -> Ok(Nil)
43    _ if attempts > 1 -> {
44      process.sleep(20)
45      claim_eventually(home, attempts - 1)
46    }
47    _ -> Error(Nil)
48  }
49}