// The event store: in memory everywhere, on PostgreSQL when // LOUISE_PG_URL is set (a container in development; skipped otherwise). import std/bytes import std/list import std/os import std/process.{type proc} import std/random.{type random} import std/result import std/test.{assert, assert_equal} import counter import event_store import postgres as pg import projection // ---- in memory (every backend) ---------------------------------------------- @test fn the_memory_store_appends_and_loads :: { event_store.with_memory fn { let v = result.expect (counter.increment "counter-1" 2) "first increment" assert_equal v 1 let v = result.expect (counter.increment "counter-1" 3) "second increment" assert_equal v 2 match event_store.load "counter-1" { Ok(events) -> { assert_equal (list.length events) 2 match events { [first, _] -> { assert_equal first.version 1 assert_equal first.kind "incremented" assert (first.data == bytes.from_string "2") "the payload" } , _ -> panic "two events" } } , Error(e) -> panic "{e:?}" } } } @test fn a_stale_version_conflicts :: { event_store.with_memory fn { result.expect (counter.increment "counter-1" 1) "the first write" // Appending at version 0 again is a conflict. match event_store.append "counter-1" 0 [event_store.NewEvent("incremented", bytes.from_string "1")] { Error(event_store.Conflict) -> () , other -> panic "expected a conflict, got {other:?}" } } } @test fn the_aggregate_rules :: { assert_equal ( counter.evolve 2 event_store.Event("s", 1, "incremented", bytes.from_string "3") ) 5 assert (counter.decide counter.Increment(0) 0 |> result.is_error) "no zero increments" } // ---- on PostgreSQL (native, LOUISE_PG_URL) ----------------------------------- @test fn the_postgres_store_and_the_projection :: -> () / { match os.env "LOUISE_PG_URL" { None -> () , Some(url) -> { let config = result.expect (pg.config url) "config" let pool = result.expect (pg.start_pool config 2) "the pool" result.expect (event_store.migrate pool) "migrate" event_store.with_postgres pool fn { // A fresh stream, two increments, read back. let stream = "orders-" <> random.token 8 let v = result.expect (counter.increment stream 2) "first increment" assert_equal v 1 let v = result.expect (counter.increment stream 3) "second increment" assert_equal v 2 match event_store.load stream { Ok(events) -> assert_equal (list.length events) 2 , Error(e) -> panic "{e:?}" } // A stale writer conflicts. match event_store.append stream 1 [event_store.NewEvent("incremented", bytes.from_string "9")] { Error(event_store.Conflict) -> () , other -> panic "expected a conflict, got {other:?}" } } // The projection catches up through the outbox, woken by LISTEN/NOTIFY. let listener = result.expect (projection.deliver config pool) "the listener" let stream = "orders-" <> random.token 8 event_store.with_postgres pool fn { result.expect (counter.increment stream 5) "the increment" result.expect (counter.increment stream 5) "another" () } assert (wait_for_count pool stream 2 40) "the projection caught up" pg.unlisten listener } } } /// Poll the projection until the stream's count is `want` (or give up). fn wait_for_count :: pool: pg.Pool stream: String want: Int tries: Int -> Bool / { let found = match pg.with_connection pool fn { pg.query "select count from stream_counts where stream = $1" [pg.text stream] } { Ok(Ok(res)) -> match res.rows { [row] -> pg.row_int row "count" == Ok(Some(want)), _ -> False } , _ -> False } if found || tries <= 0 { found } else { process.sleep 50 wait_for_count pool stream want (tries - 1) } }