tobogganSe connecter

packages

The Louise packages, each released on its own

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

541 lignes · 16566 octetsBrut · Historique
1// Against a real PostgreSQL (native only): runs when LOUISE_PG_URL is
2// set (a `postgres://` URL), skipped otherwise. The codec is tested
3// without a database; this file is the end-to-end check.
4
5import postgres as pg
6import postgres.{type sql, sql}
7import std/bytes
8import std/dec.{type Dec}
9import std/list
10import std/option
11import std/os
12import std/process.{type proc}
13import std/random.{type random}
14import std/result
15import std/test.{assert, assert_equal}
16import std/time.{type clock}
17import uuid
18
19import config.{Config}
20import pool.{PoolSettings}
21
22@test
23fn a_long_query_can_be_cancelled :: -> () / <io, proc, random>
24{
25 match os.env "LOUISE_PG_URL"
26 { None -> ()
27 , Some(url) ->
28 {
29 let cfg = result.expect (pg.config url) "config"
30 let conn = result.expect (pg.connect cfg) "connect"
31 // A 30s query in another actor; cancel it from here.
32 let done = process.subject ()
33 let _ =
34 process.spawn
35 fn
36 {
37 let answer = pg.query_on conn "select pg_sleep(30)" []
38 process.send done answer
39 }
40 process.sleep 300 // let the query reach the server
41 result.expect (pg.cancel conn) "the cancel"
42 match process.receive done 10000
43 { Some(answer) -> match answer
44 { Ok(_) -> panic "pg_sleep should have been cancelled"
45 , Error(e) -> assert_equal (pg.sqlstate e) Some("57014")
46 }
47 , None -> panic "no answer from the sleeper"
48 }
49 pg.close conn
50 }
51 }
52}
53
54@test
55fn listen_and_notify :: -> () / <io, proc, random>
56{
57 match os.env "LOUISE_PG_URL"
58 { None -> ()
59 , Some(url) ->
60 {
61 let cfg = result.expect (pg.config url) "config"
62 let events = process.subject ()
63 let listener =
64 result.expect (pg.listen cfg "louise_events" events) "listen"
65 // NOTIFY from a pooled connection (the listener's LISTEN has
66 // already landed: `pg.listen` returns after it).
67 result.expect
68 ( result.expect
69 ( pg.with_connection
70 (result.expect (pg.start_pool cfg 1) "the pool")
71 fn { pg.execute "notify louise_events, 'hello'" [] }
72 )
73 "notify"
74 )
75 "the notify"
76 match process.receive events 10000
77 { Some(n) ->
78 {
79 assert_equal n.channel "louise_events"
80 assert_equal n.payload "hello"
81 }
82 , None -> panic "no notification within the timeout"
83 }
84 pg.unlisten listener
85 }
86 }
87}
88
89@test
90fn connects_queries_and_closes :: -> () / <io, proc, random>
91{
92 match os.env "LOUISE_PG_URL"
93 { None -> () // skipped: no database
94 , Some(url) ->
95 {
96 let cfg = result.expect (pg.config url) "parse the URL"
97 let conn = result.expect (pg.connect cfg) "connect"
98 // A query without parameters, every v0 type in it.
99 let res =
100 result.expect
101 ( pg.query_on
102 conn
103 "select 42 as answer, 'hello' as greeting, true as flag, null::text as nothing, '\\xdeadbeef'::bytea as blob"
104 []
105 )
106 "the types query"
107 assert_equal (list.length res.rows) 1
108 match res.rows
109 { [row] ->
110 {
111 assert_equal (pg.row_int row "answer") Ok(Some(42))
112 assert_equal (pg.row_text row "greeting") Ok(Some("hello"))
113 assert_equal (pg.row_bool row "flag") Ok(Some(True))
114 assert_equal (pg.row_text row "nothing") Ok(None)
115 assert
116 ( pg.row_bytea row "blob"
117 == Ok(Some(bytes.from_list [222, 173, 190, 239]))
118 )
119 "the blob round-trips"
120 }
121 , _ -> panic "one row"
122 }
123 // A parameterized one: the value never enters the SQL text.
124 let counted =
125 result.expect
126 (pg.query_on conn "select $1::int + 1 as n" [pg.int 41])
127 "parameterized"
128 match counted.rows
129 { [row] -> assert_equal (pg.row_int row "n") Ok(Some(42))
130 , _ -> panic "one row"
131 }
132 // The same, written as a `sql` template.
133 let via_template =
134 result.expect
135 (pg.query_statement_on conn sql`select {41}::int + 1 as n`)
136 "the template"
137 match via_template.rows
138 { [row] -> assert_equal (pg.row_int row "n") Ok(Some(42))
139 , _ -> panic "one row"
140 }
141 // uuid and timestamptz values decode.
142 let typed =
143 result.expect
144 ( pg.query_on
145 conn
146 "select 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::uuid as id, '2026-01-02 03:04:05.5+00'::timestamptz as at"
147 []
148 )
149 "the typed query"
150 match typed.rows
151 { [row] ->
152 {
153 match pg.row_uuid row "id"
154 { Ok(Some(id)) ->
155 assert_equal
156 (uuid.to_rfc id)
157 "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"
158 , _ -> panic "the uuid"
159 }
160 match pg.row_timestamptz row "at"
161 { Ok(Some(at)) ->
162 assert_equal (time.format at) "2026-01-02T03:04:05.500Z"
163 , _ -> panic "the timestamp"
164 }
165 }
166 , _ -> panic "one row"
167 }
168 pg.close conn
169 }
170 }
171}
172
173@test
174fn executes_writes_and_errors_have_sqlstates :: -> () / <io, proc, random>
175{
176 match os.env "LOUISE_PG_URL"
177 { None -> ()
178 , Some(url) ->
179 {
180 let cfg = result.expect (pg.config url) "parse the URL"
181 let conn = result.expect (pg.connect cfg) "connect"
182 result.expect
183 ( pg.execute_on
184 conn
185 "create table if not exists louise_live (id int primary key, note text)"
186 []
187 )
188 "create table"
189 result.expect (pg.execute_on conn "delete from louise_live" []) "delete"
190 let changed =
191 result.expect
192 ( pg.execute_on
193 conn
194 "insert into louise_live (id, note) values ($1, $2), ($3, $4)"
195 [pg.int 1, pg.text "first", pg.int 2, pg.null ()]
196 )
197 "insert"
198 assert_equal changed 2
199 // A duplicate key: SQLSTATE 23505.
200 match pg.execute_on
201 conn
202 "insert into louise_live (id, note) values ($1, $2)"
203 [pg.int 1, pg.text "again"]
204 { Ok(_) -> panic "a duplicate key must fail"
205 , Error(e) -> assert_equal (pg.sqlstate e) Some("23505")
206 }
207 // NULL came back as NULL.
208 let res =
209 result.expect
210 ( pg.query_on
211 conn
212 "select note from louise_live where id = $1"
213 [pg.int 2]
214 )
215 "select"
216 match res.rows
217 { [row] -> assert_equal (pg.row_text row "note") Ok(None)
218 , _ -> panic "one row"
219 }
220 pg.close conn
221 }
222 }
223}
224
225// ---- large bytea (issue gabla108/louise#311) ---------------------------------------------------
226
227@test
228fn a_large_bytea_round_trips_in_binary_and_in_a_template ::
229 -> () / <io, proc, random, clock>
230{
231 match os.env "LOUISE_PG_URL"
232 { None -> ()
233 , Some(url) ->
234 {
235 let cfg = result.expect (pg.config url) "config"
236 let conn = result.expect (pg.connect cfg) "connect"
237 result.expect
238 ( pg.execute_on
239 conn
240 "create table if not exists louise_bytea (name text primary key, data bytea not null)"
241 []
242 )
243 "create table"
244 // 10 MB of deterministic bytes. Before the fix, a 37 MB value took
245 // 20 s and 2.86 GB of RSS: the text form was one list cell and one
246 // 2-character string per byte.
247 let block =
248 bytes.from_list (list.map (list.range 1 1000) fn i { i % 251 })
249 let data =
250 bytes.concat_all (list.map (list.range 1 10_000) fn _ { block })
251 assert_equal (bytes.length data) 10_000_000
252 // The binary path: a `pg.bytea` parameter is declared oid 17 in the
253 // Parse and binds as the raw bytes.
254 let start = time.now ()
255 result.expect
256 ( pg.execute_on
257 conn
258 "insert into louise_bytea (name, data) values ($1, $2) on conflict (name) do update set data = excluded.data"
259 [pg.text "binary", pg.bytea data]
260 )
261 "the binary insert"
262 let binary_ms = time.in_milliseconds (time.since (time.now ()) start)
263 assert
264 (binary_ms < 2000)
265 "a 10 MB bytea binds in binary: {binary_ms} ms (was ~5500)"
266 // The template path: the hole is the `\x…` text form, now built in
267 // one pass.
268 let start = time.now ()
269 result.expect
270 ( pg.execute_statement_on
271 conn
272 sql`insert into louise_bytea (name, data) values ({"template"}, {pg.bytea data}) on conflict (name) do update set data = excluded.data`
273 )
274 "the template insert"
275 let template_ms = time.in_milliseconds (time.since (time.now ()) start)
276 assert
277 (template_ms < 5000)
278 "a 10 MB bytea through a template: {template_ms} ms (was ~5500)"
279 // Both come back byte for byte (a bytea column reads in binary).
280 list.each
281 ["binary", "template"]
282 fn name
283 {
284 let res =
285 result.expect
286 ( pg.query_on
287 conn
288 "select data from louise_bytea where name = $1"
289 [pg.text name]
290 )
291 "the read back"
292 match res.rows
293 { [row] ->
294 assert
295 (pg.row_bytea row "data" == Ok(Some(data)))
296 "{name}: the 10 MB bytea round-trips"
297 , _ -> panic "one row"
298 }
299 }
300 pg.close conn
301 }
302 }
303}
304
305// ---- numeric (Dec, ADR 0070) -------------------------------------------------------
306
307@test
308fn numeric_round_trips_without_loss :: -> () / <io, proc, random>
309{
310 match os.env "LOUISE_PG_URL"
311 { None -> ()
312 , Some(url) ->
313 {
314 let cfg = result.expect (pg.config url) "config"
315 let conn = result.expect (pg.connect cfg) "connect"
316 let price = result.expect (dec.parse "12345.6789") "a price"
317 // A binary parameter (declared OID 1700), and binary results
318 // (1700 is a known binary OID): exact both ways.
319 let res =
320 result.expect
321 ( pg.query_on
322 conn
323 "select $1::numeric as price, 0.1::numeric as tenth, 1.20::numeric as padded"
324 [pg.dec price]
325 )
326 "the numeric query"
327 match res.rows
328 { [row] ->
329 {
330 assert_equal (pg.row_dec row "price") (Ok(Some(price)))
331 assert_equal
332 (pg.row_dec row "tenth")
333 (Ok(Some(result.expect (dec.parse "0.1") "0.1")))
334 // 1.20 and 1.2 are one Dec (the fixed scale is canonical).
335 assert_equal
336 (pg.row_dec row "padded")
337 (Ok(Some(result.expect (dec.parse "1.2") "1.2")))
338 }
339 , _ -> panic "one row"
340 }
341 // Past Dec's range or precision: an Error, never a silent
342 // truncation. 1e24 overflows; 1/3 rounds half-even at 18 places.
343 let bounds =
344 result.expect
345 ( pg.query_on
346 conn
347 "select 1e24::numeric as huge, (1::numeric / 3) as third, 'NaN'::numeric as nan"
348 []
349 )
350 "the bounds query"
351 match bounds.rows
352 { [row] ->
353 {
354 match pg.row_dec row "huge"
355 { Error(_) -> ()
356 , Ok(v) -> panic "1e24 should not fit a Dec, got {v:?}"
357 }
358 assert_equal
359 (pg.row_dec row "third")
360 (Ok(Some(result.expect (dec.parse "0.333333333333333333") "1/3")))
361 match pg.row_dec row "nan"
362 { Error(_) -> (), Ok(v) -> panic "NaN is not a Dec, got {v:?}" }
363 }
364 , _ -> panic "one row"
365 }
366 pg.close conn
367 }
368 }
369}
370
371// ---- the pool's knobs (issue gabla108/louise#145 §3) ------------------------------------------
372
373/// The connection's own backend pid.
374fn backend_pid :: -> Int / <sql>
375{
376 let res =
377 result.expect (pg.query "select pg_backend_pid() as pid" []) "backend_pid"
378 match res.rows
379 { [row] -> option.unwrap_or (result.expect (pg.row_int row "pid") "the pid") 0
380 , _ -> panic "one row"
381 }
382}
383
384/// The backends `app` has, counted from an outside connection (its own
385/// application name differs, so it doesn't count itself).
386fn backends :: observer: pg.Connection app: String -> Int / <proc>
387{
388 let res =
389 result.expect
390 ( pg.query_on
391 observer
392 "select count(*) as n from pg_stat_activity where application_name = $1"
393 [pg.text app]
394 )
395 "count the backends"
396 match res.rows
397 { [row] -> option.unwrap_or (result.expect (pg.row_int row "n") "the count") 0
398 , _ -> panic "one row"
399 }
400}
401
402/// `pg_stat_activity` can lag a close by a beat: poll, about 5 s at most.
403fn wait_backends ::
404 observer: pg.Connection
405 app: String
406 want: Int
407 tries: Int
408 -> () / <proc>
409{
410 if backends observer app == want
411 {
412 ()
413 }
414 else if tries <= 0
415 {
416 assert_equal (backends observer app) want
417 }
418 else
419 {
420 process.sleep 200
421 wait_backends observer app want (tries - 1)
422 }
423}
424
425@test
426fn the_pool_reaps_idle_connections_to_min_idle :: -> () / <io, proc, random>
427{
428 match os.env "LOUISE_PG_URL"
429 { None -> ()
430 , Some(url) ->
431 {
432 let cfg = result.expect (pg.config url) "config"
433 let observer = result.expect (pg.connect cfg) "the observer"
434 let app = "louise-pool-idle"
435 let settings =
436 PoolSettings( ..pg.pool_settings 2
437 , min_idle: 1
438 , idle_timeout: 1000
439 , tick: 250
440 )
441 let pl =
442 result.expect
443 (pg.start_pool_with Config(..cfg, application: app) settings)
444 "the pool"
445 wait_backends observer app 2 10 // eager start
446 wait_backends observer app 1 25 // the idle timeout reaps to the floor
447 // the floor stays warm
448 process.sleep 1000
449 assert_equal (backends observer app) 1
450 // and the pool still serves
451 result.expect
452 (pg.with_connection pl fn { pg.execute "select 1" [] })
453 "a query after the reap"
454 pg.close observer
455 }
456 }
457}
458
459@test
460fn a_connection_past_its_lifetime_is_recycled_on_return ::
461 -> () / <io, proc, random>
462{
463 match os.env "LOUISE_PG_URL"
464 { None -> ()
465 , Some(url) ->
466 {
467 let cfg = result.expect (pg.config url) "config"
468 let observer = result.expect (pg.connect cfg) "the observer"
469 let app = "louise-pool-life"
470 let settings =
471 PoolSettings(..pg.pool_settings 1, max_lifetime: 800, tick: 250)
472 let pl =
473 result.expect
474 (pg.start_pool_with Config(..cfg, application: app) settings)
475 "the pool"
476 // Held past its lifetime: the return closes it (the tick only
477 // reaps idle connections, never one in use).
478 let first =
479 result.expect
480 ( pg.with_connection
481 pl
482 fn
483 {
484 let pid = backend_pid ()
485 process.sleep 1200
486 pid
487 }
488 )
489 "the first hold"
490 let second =
491 result.expect (pg.with_connection pl fn { backend_pid () }) "the second"
492 assert
493 (first != 0 && second != 0 && first != second)
494 "the connection was recycled on return"
495 pg.close observer
496 }
497 }
498}
499
500@test
501fn a_stale_connection_fails_its_ping_and_is_replaced ::
502 -> () / <io, proc, random>
503{
504 match os.env "LOUISE_PG_URL"
505 { None -> ()
506 , Some(url) ->
507 {
508 let cfg = result.expect (pg.config url) "config"
509 let observer = result.expect (pg.connect cfg) "the observer"
510 let app = "louise-pool-stale"
511 let settings =
512 PoolSettings(..pg.pool_settings 1, check_after: 500, tick: 250)
513 let pl =
514 result.expect
515 (pg.start_pool_with Config(..cfg, application: app) settings)
516 "the pool"
517 let first =
518 result.expect (pg.with_connection pl fn { backend_pid () }) "the first"
519 // The server kills it; the pool doesn't know yet. That's the
520 // point of the check: a connection idle past `check_after` is
521 // pinged before it is handed out.
522 result.expect
523 ( pg.query_on
524 observer
525 "select pg_terminate_backend($1) as killed"
526 [pg.int first]
527 )
528 "terminate the backend"
529 wait_backends observer app 0 25
530 process.sleep 1000 // let it sit past check_after
531 let second =
532 result.expect
533 (pg.with_connection pl fn { backend_pid () })
534 "the replacement"
535 assert
536 (second != 0 && second != first)
537 "a fresh connection replaced the stale one"
538 pg.close observer
539 }
540 }
541}