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 | // Transaction ownership over a scripted PostgreSQL wire server. Native only |
| 2 | // (the interpreter has no sockets); run with LOUISE_NET_TESTS=1. |
| 3 | |
| 4 | import postgres as pg |
| 5 | import std/bytes |
| 6 | import std/ffi.{type Resource} |
| 7 | import std/list |
| 8 | import std/net.{type TcpListener, type TcpStream} |
| 9 | import std/option |
| 10 | import std/os |
| 11 | import std/process.{type Subject, type proc} |
| 12 | import std/random.{type random} |
| 13 | import std/result |
| 14 | import std/string |
| 15 | import std/test.{assert, assert_equal} |
| 16 | |
| 17 | import binary |
| 18 | import config.{type Config, Config, Disable} |
| 19 | import protocol |
| 20 | |
| 21 | fn enabled :: -> Bool / <io> |
| 22 | { |
| 23 | option.is_some (os.env "LOUISE_NET_TESTS") |
| 24 | } |
| 25 | |
| 26 | @derive Eq |
| 27 | type Behavior |
| 28 | { CommitOk, CommitAmbiguous, CommitFailed, CommitRetry, BeginFailedOnce } |
| 29 | type Command { Begin, Commit, Rollback } |
| 30 | type Event { Connected(Int), Closed(Int), RolledBack(Int) } |
| 31 | |
| 32 | type ServerState |
| 33 | { ServerState( |
| 34 | behavior: Behavior |
| 35 | , statements: List<(String, Command)> |
| 36 | , in_tx: Bool |
| 37 | , begin_failed: Bool |
| 38 | ) |
| 39 | } |
| 40 | |
| 41 | @test |
| 42 | fn commit_errors_release_the_checkout_without_reusing_the_connection :: |
| 43 | -> () / <io, proc, random> |
| 44 | { |
| 45 | if enabled () |
| 46 | { |
| 47 | // COMMIT's success frame arrives, then EOF before Sync's ReadyForQuery: |
| 48 | // the transaction may have committed, but the connection is unusable. |
| 49 | commit_error_gets_a_fresh_connection CommitAmbiguous None |
| 50 | // An ErrorResponse without CommandComplete must not strand it either. |
| 51 | commit_error_gets_a_fresh_connection CommitFailed (Some("XX000")) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fn commit_error_gets_a_fresh_connection :: |
| 56 | behavior: Behavior |
| 57 | code: Option<String> |
| 58 | -> () / <io, proc, random> |
| 59 | { |
| 60 | let events = process.subject () |
| 61 | let attempts = process.subject () |
| 62 | let pool = |
| 63 | result.expect |
| 64 | (pg.start_pool (test_config (server behavior events)) 1) |
| 65 | "pool" |
| 66 | match pg.transaction_retry |
| 67 | pool |
| 68 | 1 |
| 69 | fn |
| 70 | { |
| 71 | process.send attempts () |
| 72 | Ok(1) |
| 73 | } |
| 74 | { Ok(_) -> panic "the scripted COMMIT must fail" |
| 75 | , Error(e) -> assert_equal (pg.sqlstate e) code |
| 76 | } |
| 77 | // Neither disconnects nor XX000 are transient: no retry is allowed. |
| 78 | assert_equal (drain_attempts attempts 0) 1 |
| 79 | // The old code times out here with the sole connection still checked out. |
| 80 | assert_equal |
| 81 | (result.expect (pg.transaction pool fn { Ok(42) }) "next transaction") |
| 82 | 42 |
| 83 | assert_first_connection_replaced events |
| 84 | } |
| 85 | |
| 86 | @test |
| 87 | fn transaction_retry_uses_a_fresh_connection_and_reruns_only_on_transient_errors :: |
| 88 | -> () / <io, proc, random> |
| 89 | { |
| 90 | if enabled () |
| 91 | { |
| 92 | let events = process.subject () |
| 93 | let attempts = process.subject () |
| 94 | let pool = |
| 95 | result.expect |
| 96 | (pg.start_pool (test_config (server CommitRetry events)) 1) |
| 97 | "pool" |
| 98 | assert_equal |
| 99 | ( result.expect |
| 100 | ( pg.transaction_retry |
| 101 | pool |
| 102 | 1 |
| 103 | fn |
| 104 | { |
| 105 | process.send attempts () |
| 106 | Ok(42) |
| 107 | } |
| 108 | ) |
| 109 | "serialized transaction" |
| 110 | ) |
| 111 | 42 |
| 112 | assert_equal (drain_attempts attempts 0) 2 |
| 113 | assert_first_connection_replaced events |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | fn assert_first_connection_replaced :: events: Subject<Event> -> () / <proc> |
| 118 | { |
| 119 | let observed = [next_event events, next_event events, next_event events] |
| 120 | assert |
| 121 | (has_connected observed 2) |
| 122 | "the next transaction uses a fresh connection" |
| 123 | assert (has_closed observed 1) "the failed-COMMIT connection was closed" |
| 124 | } |
| 125 | |
| 126 | fn drain_attempts :: attempts: Subject<()> count: Int -> Int / <proc> |
| 127 | { |
| 128 | match process.receive attempts 0 |
| 129 | { Some(()) -> drain_attempts attempts (count + 1), None -> count } |
| 130 | } |
| 131 | |
| 132 | @test |
| 133 | fn other_transaction_outcomes_keep_their_release_semantics :: |
| 134 | -> () / <io, proc, random> |
| 135 | { |
| 136 | if enabled () |
| 137 | { |
| 138 | let events = process.subject () |
| 139 | let pool = |
| 140 | result.expect |
| 141 | (pg.start_pool (test_config (server BeginFailedOnce events)) 1) |
| 142 | "pool" |
| 143 | match pg.transaction pool fn { Ok(()) } |
| 144 | { Ok(_) -> panic "the scripted BEGIN must fail" |
| 145 | , Error(e) -> assert_equal (pg.sqlstate e) Some("XX100") |
| 146 | } |
| 147 | // BEGIN failed before opening a transaction, so this connection is safe |
| 148 | // to reuse. A normal COMMIT still releases it too. |
| 149 | assert_equal (result.expect (pg.transaction pool fn { Ok(42) }) "commit") 42 |
| 150 | match pg.transaction pool fn { Error(pg.driver ("body failed")) } |
| 151 | { Ok(_) -> panic "the callback must fail" |
| 152 | , Error(e) -> assert_equal (pg.error_message e) "body failed" |
| 153 | } |
| 154 | // The callback error rolled back and released the same live connection. |
| 155 | assert_equal |
| 156 | (result.expect (pg.transaction pool fn { Ok(43) }) "after rollback") |
| 157 | 43 |
| 158 | match (next_event events, next_event events) |
| 159 | { (Connected(1), RolledBack(1)) -> () |
| 160 | , _ -> panic "BEGIN failure and COMMIT must reuse connection 1" |
| 161 | } |
| 162 | match process.receive events 100 |
| 163 | { None -> (), Some(_) -> panic "an unexpected connection event" } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | fn has_connected :: events: List<Event> id: Int -> Bool |
| 168 | { |
| 169 | list.any |
| 170 | events |
| 171 | fn event { match event { Connected(n) -> n == id, _ -> False } } |
| 172 | } |
| 173 | |
| 174 | fn has_closed :: events: List<Event> id: Int -> Bool |
| 175 | { |
| 176 | list.any events fn event { match event { Closed(n) -> n == id, _ -> False } } |
| 177 | } |
| 178 | |
| 179 | fn next_event :: events: Subject<Event> -> Event / <proc> |
| 180 | { |
| 181 | match process.receive events 2000 |
| 182 | { Some(event) -> event |
| 183 | , None -> panic "timed out waiting for the wire server" |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | fn test_config :: port: Int -> Config |
| 188 | { |
| 189 | Config(..pg.config_defaults (), host: "127.0.0.1", port: port, tls: Disable) |
| 190 | } |
| 191 | |
| 192 | // ---- the scripted wire server --------------------------------------------------- |
| 193 | |
| 194 | fn server :: first: Behavior events: Subject<Event> -> Int / <io, proc> |
| 195 | { |
| 196 | let listener = result.expect (net.listen "127.0.0.1" 0) "listen" |
| 197 | let port = net.port listener |
| 198 | let _ = process.spawn fn { accept_next listener first events 1 } |
| 199 | port |
| 200 | } |
| 201 | |
| 202 | fn accept_next :: |
| 203 | listener: Resource<TcpListener> |
| 204 | first: Behavior |
| 205 | events: Subject<Event> |
| 206 | id: Int |
| 207 | -> () / <io, proc> |
| 208 | { |
| 209 | match net.accept listener |
| 210 | { Error(_) -> () |
| 211 | , Ok(conn) -> |
| 212 | { |
| 213 | let behavior = if id == 1 { first } else { CommitOk } |
| 214 | process.send events Connected(id) |
| 215 | let _ = process.spawn fn { serve_socket conn id behavior events } |
| 216 | accept_next listener first events (id + 1) |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | fn serve_socket :: |
| 222 | conn: Resource<TcpStream> |
| 223 | id: Int |
| 224 | behavior: Behavior |
| 225 | events: Subject<Event> |
| 226 | -> () / <io, proc> |
| 227 | { |
| 228 | match startup_message conn (bytes.empty ()) |
| 229 | { None -> net.close conn |
| 230 | , Some(_) -> |
| 231 | { |
| 232 | result.expect |
| 233 | ( net.write |
| 234 | conn |
| 235 | (bytes.concat_all [message 82 (binary.be32 0), ready False]) |
| 236 | ) |
| 237 | "handshake" |
| 238 | serve conn id events ServerState(behavior, [], False, False) |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | fn serve :: |
| 244 | conn: Resource<TcpStream> |
| 245 | id: Int |
| 246 | events: Subject<Event> |
| 247 | state: ServerState |
| 248 | -> () / <io, proc> |
| 249 | { |
| 250 | match exchange_message conn (bytes.empty ()) |
| 251 | { None -> close_server_connection conn id events |
| 252 | , Some(data) -> if bytes.read_u8 data 0 == 88 // Terminate |
| 253 | { |
| 254 | close_server_connection conn id events |
| 255 | } |
| 256 | else |
| 257 | { |
| 258 | match bytes.read_u8 data 0 |
| 259 | { 80 -> // Parse + DescribeStatement + Sync |
| 260 | { |
| 261 | let (name, _) = string_at data 5 |
| 262 | let command = command_in data |
| 263 | result.expect |
| 264 | ( net.write |
| 265 | conn |
| 266 | ( bytes.concat_all |
| 267 | [ message 49 (bytes.empty ()) |
| 268 | , message 116 (binary.be16 0) |
| 269 | , message 110 (bytes.empty ()) |
| 270 | , ready state.in_tx |
| 271 | ] |
| 272 | ) |
| 273 | ) |
| 274 | "prepare" |
| 275 | serve |
| 276 | conn |
| 277 | id |
| 278 | events |
| 279 | ServerState( ..state |
| 280 | , statements: [(name, command), ..state.statements] |
| 281 | ) |
| 282 | } |
| 283 | , 66 -> // Bind + DescribePortal + Execute + Sync |
| 284 | { |
| 285 | let (_, at) = string_at data 5 |
| 286 | let (name, _) = string_at data at |
| 287 | let (state, response, close) = |
| 288 | execute id events state (statement state.statements name) |
| 289 | result.expect (net.write conn response) "execute" |
| 290 | if close |
| 291 | { |
| 292 | close_server_connection conn id events |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | serve conn id events state |
| 297 | } |
| 298 | } |
| 299 | , _ -> panic "unexpected frontend message" |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | fn execute :: |
| 306 | id: Int |
| 307 | events: Subject<Event> |
| 308 | state: ServerState |
| 309 | command: Command |
| 310 | -> (ServerState, Bytes, Bool) / <proc> |
| 311 | { |
| 312 | match command |
| 313 | { Begin -> if state.behavior == BeginFailedOnce && !state.begin_failed |
| 314 | { |
| 315 | ( ServerState(..state, begin_failed: True) |
| 316 | , bytes.concat_all [server_error "XX100" "begin failed", ready False] |
| 317 | , False |
| 318 | ) |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | ( ServerState(..state, in_tx: True) |
| 323 | , bytes.concat_all [command_complete "BEGIN", ready True] |
| 324 | , False |
| 325 | ) |
| 326 | } |
| 327 | , Rollback -> |
| 328 | { |
| 329 | process.send events RolledBack(id) |
| 330 | ( ServerState(..state, in_tx: False) |
| 331 | , bytes.concat_all [command_complete "ROLLBACK", ready False] |
| 332 | , False |
| 333 | ) |
| 334 | } |
| 335 | , Commit -> match state.behavior |
| 336 | { CommitAmbiguous -> ( ServerState(..state, in_tx: False) |
| 337 | , command_complete "COMMIT" |
| 338 | , True // Close before ReadyForQuery: a genuinely ambiguous commit. |
| 339 | ) |
| 340 | , CommitFailed -> ( ServerState(..state, in_tx: False) |
| 341 | , bytes.concat_all [server_error "XX000" "commit failed", ready False] |
| 342 | , False |
| 343 | ) |
| 344 | , CommitRetry -> ( ServerState(..state, in_tx: False) |
| 345 | , bytes.concat_all |
| 346 | [server_error "40001" "serialization failure", ready False] |
| 347 | , False |
| 348 | ) |
| 349 | , _ -> ( ServerState(..state, in_tx: False) |
| 350 | , bytes.concat_all [command_complete "COMMIT", ready False] |
| 351 | , False |
| 352 | ) |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | fn close_server_connection :: |
| 358 | conn: Resource<TcpStream> |
| 359 | id: Int |
| 360 | events: Subject<Event> |
| 361 | -> () / <io, proc> |
| 362 | { |
| 363 | process.send events Closed(id) |
| 364 | net.close conn |
| 365 | } |
| 366 | |
| 367 | fn statement :: statements: List<(String, Command)> name: String -> Command |
| 368 | { |
| 369 | match statements |
| 370 | { [] -> panic "Execute for an unknown prepared statement" |
| 371 | , [(prepared, command), ..] if prepared == name -> command |
| 372 | , [_, ..rest] -> statement rest name |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | fn string_at :: data: Bytes at: Int -> (String, Int) |
| 377 | { |
| 378 | let end = string_end data at |
| 379 | let text = |
| 380 | result.expect (bytes.to_string (bytes.slice data at end)) "statement name" |
| 381 | (text, end + 1) |
| 382 | } |
| 383 | |
| 384 | fn string_end :: data: Bytes at: Int -> Int |
| 385 | { |
| 386 | if bytes.read_u8 data at == 0 |
| 387 | { |
| 388 | at |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | string_end data (at + 1) |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | fn command_in :: data: Bytes -> Command |
| 397 | { |
| 398 | let text = bytes.to_string_lossy data |
| 399 | if string.contains text "begin" |
| 400 | { |
| 401 | Begin |
| 402 | } |
| 403 | else if string.contains text "commit" |
| 404 | { |
| 405 | Commit |
| 406 | } |
| 407 | else if string.contains text "rollback" |
| 408 | { |
| 409 | Rollback |
| 410 | } |
| 411 | else |
| 412 | { |
| 413 | panic "unexpected SQL in the transaction test" |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | fn startup_message :: |
| 418 | conn: Resource<TcpStream> |
| 419 | have: Bytes |
| 420 | -> Option<Bytes> / <io> |
| 421 | { |
| 422 | if bytes.length have >= 4 && bytes.length have >= bytes.read_u32be have 0 |
| 423 | { |
| 424 | Some(have) |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | match net.read conn 65536 |
| 429 | { Ok(more) if bytes.length more > 0 -> |
| 430 | startup_message conn (bytes.concat have more) |
| 431 | , _ -> None |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | fn exchange_message :: |
| 437 | conn: Resource<TcpStream> |
| 438 | have: Bytes |
| 439 | -> Option<Bytes> / <io> |
| 440 | { |
| 441 | let n = bytes.length have |
| 442 | let sync = protocol.encode protocol.Sync |
| 443 | if n >= 5 && bytes.slice have (n - 5) n == sync |
| 444 | || n == 5 && bytes.length have > 0 && bytes.read_u8 have 0 == 88 |
| 445 | { |
| 446 | Some(have) |
| 447 | } |
| 448 | else |
| 449 | { |
| 450 | match net.read conn 65536 |
| 451 | { Ok(more) if bytes.length more > 0 -> |
| 452 | exchange_message conn (bytes.concat have more) |
| 453 | , _ -> None |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | fn command_complete :: tag: String -> Bytes |
| 459 | { |
| 460 | message 67 (c_string tag) |
| 461 | } |
| 462 | |
| 463 | fn server_error :: code: String text: String -> Bytes |
| 464 | { |
| 465 | message |
| 466 | 69 |
| 467 | ( bytes.concat_all |
| 468 | [ bytes.concat (bytes.from_list [83]) (c_string "ERROR") |
| 469 | , bytes.concat (bytes.from_list [67]) (c_string code) |
| 470 | , bytes.concat (bytes.from_list [77]) (c_string text) |
| 471 | , bytes.from_list [0] |
| 472 | ] |
| 473 | ) |
| 474 | } |
| 475 | |
| 476 | fn ready :: in_tx: Bool -> Bytes |
| 477 | { |
| 478 | message 90 (bytes.from_list [if in_tx { 84 } else { 73 }]) |
| 479 | } |
| 480 | |
| 481 | fn c_string :: text: String -> Bytes |
| 482 | { |
| 483 | bytes.concat (bytes.from_string text) (bytes.from_list [0]) |
| 484 | } |
| 485 | |
| 486 | fn message :: tag: Int payload: Bytes -> Bytes |
| 487 | { |
| 488 | bytes.concat_all |
| 489 | [bytes.from_list [tag], binary.be32 (bytes.length payload + 4), payload] |
| 490 | } |