tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

162 lignes · 4352 octetsBrut · Historique
1// The event store: in memory everywhere, on PostgreSQL when
2// LOUISE_PG_URL is set (a container in development; skipped otherwise).
3
4import std/bytes
5import std/list
6import std/os
7import std/process.{type proc}
8import std/random.{type random}
9import std/result
10import std/test.{assert, assert_equal}
11
12import counter
13import event_store
14import postgres as pg
15import projection
16
17// ---- in memory (every backend) ----------------------------------------------
18
19@test
20fn the_memory_store_appends_and_loads ::
21{
22 event_store.with_memory
23 fn
24 {
25 let v = result.expect (counter.increment "counter-1" 2) "first increment"
26 assert_equal v 1
27 let v = result.expect (counter.increment "counter-1" 3) "second increment"
28 assert_equal v 2
29 match event_store.load "counter-1"
30 { Ok(events) ->
31 {
32 assert_equal (list.length events) 2
33 match events
34 { [first, _] ->
35 {
36 assert_equal first.version 1
37 assert_equal first.kind "incremented"
38 assert (first.data == bytes.from_string "2") "the payload"
39 }
40 , _ -> panic "two events"
41 }
42 }
43 , Error(e) -> panic "{e:?}"
44 }
45 }
46}
47
48@test
49fn a_stale_version_conflicts ::
50{
51 event_store.with_memory
52 fn
53 {
54 result.expect (counter.increment "counter-1" 1) "the first write"
55 // Appending at version 0 again is a conflict.
56 match event_store.append
57 "counter-1"
58 0
59 [event_store.NewEvent("incremented", bytes.from_string "1")]
60 { Error(event_store.Conflict) -> ()
61 , other -> panic "expected a conflict, got {other:?}"
62 }
63 }
64}
65
66@test
67fn the_aggregate_rules ::
68{
69 assert_equal
70 ( counter.evolve
71 2
72 event_store.Event("s", 1, "incremented", bytes.from_string "3")
73 )
74 5
75 assert
76 (counter.decide counter.Increment(0) 0 |> result.is_error)
77 "no zero increments"
78}
79
80// ---- on PostgreSQL (native, LOUISE_PG_URL) -----------------------------------
81
82@test
83fn the_postgres_store_and_the_projection :: -> () / <io, proc, random>
84{
85 match os.env "LOUISE_PG_URL"
86 { None -> ()
87 , Some(url) ->
88 {
89 let config = result.expect (pg.config url) "config"
90 let pool = result.expect (pg.start_pool config 2) "the pool"
91 result.expect (event_store.migrate pool) "migrate"
92 event_store.with_postgres
93 pool
94 fn
95 {
96 // A fresh stream, two increments, read back.
97 let stream = "orders-" <> random.token 8
98 let v = result.expect (counter.increment stream 2) "first increment"
99 assert_equal v 1
100 let v = result.expect (counter.increment stream 3) "second increment"
101 assert_equal v 2
102 match event_store.load stream
103 { Ok(events) -> assert_equal (list.length events) 2
104 , Error(e) -> panic "{e:?}"
105 }
106 // A stale writer conflicts.
107 match event_store.append
108 stream
109 1
110 [event_store.NewEvent("incremented", bytes.from_string "9")]
111 { Error(event_store.Conflict) -> ()
112 , other -> panic "expected a conflict, got {other:?}"
113 }
114 }
115 // The projection catches up through the outbox, woken by LISTEN/NOTIFY.
116 let listener =
117 result.expect (projection.deliver config pool) "the listener"
118 let stream = "orders-" <> random.token 8
119 event_store.with_postgres
120 pool
121 fn
122 {
123 result.expect (counter.increment stream 5) "the increment"
124 result.expect (counter.increment stream 5) "another"
125 ()
126 }
127 assert (wait_for_count pool stream 2 40) "the projection caught up"
128 pg.unlisten listener
129 }
130 }
131}
132
133/// Poll the projection until the stream's count is `want` (or give up).
134fn wait_for_count ::
135 pool: pg.Pool
136 stream: String
137 want: Int
138 tries: Int
139 -> Bool / <proc>
140{
141 let found = match pg.with_connection
142 pool
143 fn
144 {
145 pg.query
146 "select count from stream_counts where stream = $1"
147 [pg.text stream]
148 }
149 { Ok(Ok(res)) -> match res.rows
150 { [row] -> pg.row_int row "count" == Ok(Some(want)), _ -> False }
151 , _ -> False
152 }
153 if found || tries <= 0
154 {
155 found
156 }
157 else
158 {
159 process.sleep 50
160 wait_for_count pool stream want (tries - 1)
161 }
162}