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.
Dernier runRéussi#28 · main · 3 min 34 s
0Étoiles
0Forks
3Issues
| 1 | import postgres as pg |
| 2 | import resilience/rate_limit.{Admitted, Wait} |
| 3 | import shared |
| 4 | import std/os |
| 5 | import std/process.{type proc} |
| 6 | import std/random.{type random} |
| 7 | import std/result |
| 8 | import std/test.{assert, assert_equal} |
| 9 | |
| 10 | /// Against a real database when `LOUISE_PG_URL` names one; skipped |
| 11 | /// otherwise. |
| 12 | fn against_postgres :: |
| 13 | check: fn(pg.Pool) -> () / <io, proc, random> |
| 14 | -> () / <io, proc, random> |
| 15 | { |
| 16 | match os.env "LOUISE_PG_URL" |
| 17 | { None -> () |
| 18 | , Some(url) -> |
| 19 | { |
| 20 | let config = result.expect (pg.config url) "a database url" |
| 21 | let pool = result.expect (pg.start_pool config 2) "a pool" |
| 22 | check pool |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | fn reset :: pool: pg.Pool store: shared.Store table: String -> () / <proc> |
| 28 | { |
| 29 | let run = fn text |
| 30 | { |
| 31 | pg.with_connection pool fn { pg.query_statement (pg.Statement(text, [])) } |
| 32 | } |
| 33 | let _ = run "drop table if exists {table}" |
| 34 | let _ = run (shared.create_table store) |
| 35 | () |
| 36 | } |
| 37 | |
| 38 | @test |
| 39 | fn a_shared_bucket_admits_its_rate_then_says_how_long_to_wait :: |
| 40 | -> () / <io, proc, random> |
| 41 | { |
| 42 | against_postgres |
| 43 | fn pool |
| 44 | { |
| 45 | let store = shared.rates () |
| 46 | reset pool store "rates" |
| 47 | let try = fn key { shared.admission pool store 2 key } |
| 48 | assert_equal (try "a") Ok(Admitted) |
| 49 | assert_equal (try "a") Ok(Admitted) |
| 50 | match try "a" |
| 51 | { Ok(Wait(seconds)) -> assert (seconds >= 1 && seconds <= 30) "a wait" |
| 52 | , other -> panic "expected a wait, got {other}" |
| 53 | } |
| 54 | // another key has its own bucket; 0 a minute admits everything |
| 55 | assert_equal (try "b") Ok(Admitted) |
| 56 | assert_equal (shared.admission pool store 0 "a") Ok(Admitted) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// An app's own table and key column (starfleet's `api_rates(agent)`). |
| 61 | @test |
| 62 | fn a_store_of_its_own_names_its_table_and_column :: -> () / <io, proc, random> |
| 63 | { |
| 64 | against_postgres |
| 65 | fn pool |
| 66 | { |
| 67 | let store = |
| 68 | result.expect (shared.store "throttle_test_rates" "agent") "a store" |
| 69 | reset pool store "throttle_test_rates" |
| 70 | assert_equal (shared.admission pool store 1 "x") Ok(Admitted) |
| 71 | match shared.admission pool store 1 "x" |
| 72 | { Ok(Wait(_)) -> (), other -> panic "expected a wait, got {other}" } |
| 73 | } |
| 74 | } |