okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
e4803b3
main
1//// A shared value must cost nothing to capture or send, and a released 2//// handle must fail loudly rather than answer a stale value; neither shows 3//// through the daemon's behaviour until memory or a wrong catalog does. 4 5import albedo/harness/protect 6import albedo/shared 7import gleam/int 8import gleam/list 9import gleam/string 10import gleeunit/should 11 12@external ( erlang , "albedo_copy_test_support" , "captured_words" ) 13fn captured_words ( term : a) -> Int 14 15pub fn read_answers_the_published_value_test () -> Nil { 16let handle = shared . publish (#( "catalog" , [ 1 , 2 , 3 ])) 17shared . read ( handle ) |> should . equal (#( "catalog" , [ 1 , 2 , 3 ])) 18shared . release ( handle ) 19} 20 21pub fn captured_shared_value_copies_nothing_test () -> Nil { 22let baseline = captured_words ( Nil ) 23// Built at runtime: a string literal would already be one. 24let value = 25list . repeat ( Nil , 20_000 ) |> list . index_map ( fn ( _ , n ) { int . to_string ( n ) }) 26should . be_true ( captured_words ( value ) > baseline + 60_000 ) 27let handle = shared . publish ( value ) 28let read = shared . read ( handle ) 29captured_words ( read ) |> should . equal ( baseline ) 30// A selection over a shared list copies its cells, never its elements. 31should . be_true ( captured_words ( list . take ( read , 10_000 )) < baseline + 30_000 ) 32shared . release ( handle ) 33} 34 35pub fn reading_a_released_handle_panics_test () -> Nil { 36let handle = shared . publish ( "gone" ) 37shared . release ( handle ) 38let assert Error ( reason ) = protect . attempt ( fn () { shared . read ( handle ) }) 39should . be_true ( string . contains ( reason , "shared value was released" )) 40}