| @@ -10,10 +10,14 @@ |
| 10 | 10 | |
| 11 | 11 | import fetch. |
| 12 | 12 | { type Options |
| 13 | +, type PendingRequest |
| 14 | +, type RequestOutcome |
| 13 | 15 | , type Response |
| 14 | 16 | , AnyAddress |
| 15 | 17 | , BadHeader |
| 16 | 18 | , BadUrl |
| 19 | +, Cancelled |
| 20 | +, Completed |
| 17 | 21 | , Connect |
| 18 | 22 | , Malformed |
| 19 | 23 | , NoAddress |
| @@ -31,7 +35,7 @@ import std/list |
| 31 | 35 | import std/net.{type TcpListener, type TcpStream} |
| 32 | 36 | import std/option |
| 33 | 37 | import std/os |
| 34 | | −import std/process.{type proc} |
| 38 | +import std/process.{type Subject, type proc} |
| 35 | 39 | import std/result |
| 36 | 40 | import std/string |
| 37 | 41 | import std/test.{assert, assert_equal} |
| @@ -217,6 +221,99 @@ fn stalling_server :: -> Int / <io, proc> |
| 217 | 221 | port |
| 218 | 222 | } |
| 219 | 223 | |
| 224 | +type CancellationPoint |
| 225 | +{ BeforeResponse, DuringBody, TlsHandshake, LateResponse } |
| 226 | + |
| 227 | +/// One server connection at a known stall. The server, not the waiting |
| 228 | +/// caller, owns the cancellation-handoff subject. It cancels twice and then |
| 229 | +/// reports whether it observed the client close. |
| 230 | +fn cancelling_server :: |
| 231 | + point: CancellationPoint |
| 232 | + -> (Int, Subject<PendingRequest>, Subject<Bool>) / <io, proc> |
| 233 | +{ |
| 234 | + let listener = result.expect (net.listen "127.0.0.1" 0) "listen" |
| 235 | + let port = net.port listener |
| 236 | + let handoff = process.subject () |
| 237 | + let closed = process.subject () |
| 238 | + let _ = |
| 239 | + process.spawn |
| 240 | + fn |
| 241 | + { |
| 242 | + let requests = process.subject () |
| 243 | + process.send handoff requests |
| 244 | + match net.accept listener |
| 245 | + { Ok(conn) -> |
| 246 | + { |
| 247 | + match point |
| 248 | + { TlsHandshake -> |
| 249 | + { |
| 250 | + let _ = net.read conn 4096 |
| 251 | + () |
| 252 | + } |
| 253 | + , _ -> |
| 254 | + { |
| 255 | + let _ = head_from conn (bytes.empty ()) |
| 256 | + () |
| 257 | + } |
| 258 | + } |
| 259 | + match point |
| 260 | + { DuringBody -> |
| 261 | + { |
| 262 | + let _ = |
| 263 | + net.write |
| 264 | + conn |
| 265 | + ( bytes.from_string |
| 266 | + "HTTP/1.1 200 OK\r\nContent-Length: 100\r\n\r\npartial" |
| 267 | + ) |
| 268 | + // Let the exchange consume the head and enter its body read. |
| 269 | + // The assertion still waits on the observed close, not this pause. |
| 270 | + process.sleep 50 |
| 271 | + } |
| 272 | + , _ -> () |
| 273 | + } |
| 274 | + match process.receive requests 5000 |
| 275 | + { Some(pending) -> |
| 276 | + { |
| 277 | + fetch.cancel_request pending |
| 278 | + fetch.cancel_request pending |
| 279 | + } |
| 280 | + , None -> () |
| 281 | + } |
| 282 | + match point |
| 283 | + { LateResponse -> |
| 284 | + { |
| 285 | + let _ = |
| 286 | + net.write |
| 287 | + conn |
| 288 | + ( bytes.from_string |
| 289 | + "HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nlate" |
| 290 | + ) |
| 291 | + () |
| 292 | + } |
| 293 | + , _ -> () |
| 294 | + } |
| 295 | + let did_close = until_closed conn |
| 296 | + net.close conn |
| 297 | + process.send closed did_close |
| 298 | + } |
| 299 | + , Error(_) -> process.send closed False |
| 300 | + } |
| 301 | + } |
| 302 | + match process.receive handoff 5000 |
| 303 | + { Some(requests) -> (port, requests, closed) |
| 304 | + , None -> panic "the server never sent its request handoff" |
| 305 | + } |
| 306 | +} |
| 307 | + |
| 308 | +fn until_closed :: conn: Resource<TcpStream> -> Bool / <io> |
| 309 | +{ |
| 310 | + match net.read conn 4096 |
| 311 | + { Ok(more) if bytes.length more > 0 -> until_closed conn |
| 312 | + , Ok(_) -> True |
| 313 | + , Error(_) -> True |
| 314 | + } |
| 315 | +} |
| 316 | + |
| 220 | 317 | /// A TLS server for one connection with `cert`; it answers `text`, then |
| 221 | 318 | /// ends the TLS stream properly or (`clean` false) just closes the socket. |
| 222 | 319 | fn tls_server :: cert: String text: String clean: Bool -> Int / <io, proc> |
| @@ -272,6 +369,50 @@ fn body_text :: |
| 272 | 369 | |> result.map fn r { (r.status, bytes.to_string_lossy r.body, r.truncated) } |
| 273 | 370 | } |
| 274 | 371 | |
| 372 | +fn pending_get :: |
| 373 | + scheme: String |
| 374 | + port: Int |
| 375 | + opts: Options |
| 376 | + -> PendingRequest / <proc, clock> |
| 377 | +{ |
| 378 | + result.expect |
| 379 | + ( fetch.start_request |
| 380 | + "GET" |
| 381 | + (at "{scheme}://127.0.0.1:{port}/") |
| 382 | + [] |
| 383 | + (bytes.empty ()) |
| 384 | + opts |
| 385 | + ) |
| 386 | + "start the request" |
| 387 | +} |
| 388 | + |
| 389 | +fn completed_body :: |
| 390 | + outcome: RequestOutcome |
| 391 | + -> Result<(Int, String, Bool), fetch.Error> |
| 392 | +{ |
| 393 | + match outcome |
| 394 | + { Completed(answer) -> body_text answer |
| 395 | + , Cancelled -> Error(Malformed("the test request was cancelled")) |
| 396 | + } |
| 397 | +} |
| 398 | + |
| 399 | +fn expect_cancelled :: |
| 400 | + point: CancellationPoint |
| 401 | + scheme: String |
| 402 | + -> () / <io, proc, clock> |
| 403 | +{ |
| 404 | + let (port, requests, closed) = cancelling_server point |
| 405 | + let pending = pending_get scheme port (local ()) |
| 406 | + process.send requests pending |
| 407 | + match fetch.await_request pending |
| 408 | + { Cancelled -> () |
| 409 | + , Completed(answer) -> panic "a cancelled request completed: {show answer}" |
| 410 | + } |
| 411 | + fetch.cancel_request pending |
| 412 | + fetch.cancel_request pending |
| 413 | + assert_equal (process.receive closed 3000) Some(True) |
| 414 | +} |
| 415 | + |
| 275 | 416 | // ---- bodies -------------------------------------------------------------------- |
| 276 | 417 | |
| 277 | 418 | @test |
| @@ -658,6 +799,91 @@ fn a_deadline_during_the_lookup :: -> () / <io, proc, clock> |
| 658 | 799 | } |
| 659 | 800 | } |
| 660 | 801 | |
| 802 | +// ---- cancellable collected requests ---------------------------------------------- |
| 803 | + |
| 804 | +@test |
| 805 | +fn pending_success_and_late_cancellation_do_not_touch_a_new_request :: |
| 806 | + -> () / <io, proc, clock> |
| 807 | +{ |
| 808 | + if enabled () |
| 809 | + { |
| 810 | + let answer = "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok" |
| 811 | + let first = pending_get "http" (answering answer) (local ()) |
| 812 | + assert_equal |
| 813 | + (completed_body (fetch.await_request first)) |
| 814 | + Ok((200, "ok", False)) |
| 815 | + |
| 816 | + // Start the next exchange before repeatedly stopping the retired handle. |
| 817 | + // A confused or reused identity would kill this request too. |
| 818 | + let second = pending_get "http" (answering answer) (local ()) |
| 819 | + fetch.cancel_request first |
| 820 | + fetch.cancel_request first |
| 821 | + assert_equal |
| 822 | + (completed_body (fetch.await_request second)) |
| 823 | + Ok((200, "ok", False)) |
| 824 | + } |
| 825 | +} |
| 826 | + |
| 827 | +@test |
| 828 | +fn cancellation_before_the_response_releases_every_exchange :: |
| 829 | + -> () / <io, proc, clock> |
| 830 | +{ |
| 831 | + if enabled () |
| 832 | + { |
| 833 | + list.each (list.range 1 20) fn _ { expect_cancelled BeforeResponse "http" } |
| 834 | + } |
| 835 | +} |
| 836 | + |
| 837 | +@test |
| 838 | +fn cancellation_during_body_collection_closes_the_connection :: |
| 839 | + -> () / <io, proc, clock> |
| 840 | +{ |
| 841 | + if enabled () |
| 842 | + { |
| 843 | + expect_cancelled DuringBody "http" |
| 844 | + } |
| 845 | +} |
| 846 | + |
| 847 | +@test |
| 848 | +fn cancellation_during_a_tls_handshake_closes_its_socket :: |
| 849 | + -> () / <io, proc, clock> |
| 850 | +{ |
| 851 | + if enabled () |
| 852 | + { |
| 853 | + expect_cancelled TlsHandshake "https" |
| 854 | + } |
| 855 | +} |
| 856 | + |
| 857 | +@test |
| 858 | +fn a_response_written_after_cancellation_is_not_a_success :: |
| 859 | + -> () / <io, proc, clock> |
| 860 | +{ |
| 861 | + if enabled () |
| 862 | + { |
| 863 | + list.each (list.range 1 20) fn _ { expect_cancelled LateResponse "http" } |
| 864 | + } |
| 865 | +} |
| 866 | + |
| 867 | +@test |
| 868 | +fn a_pending_request_keeps_the_deadline_behavior :: -> () / <io, proc, clock> |
| 869 | +{ |
| 870 | + if enabled () |
| 871 | + { |
| 872 | + let opts = Options(..local (), deadline: fetch.deadline_in 200) |
| 873 | + let pending = pending_get "http" (stalling_server ()) opts |
| 874 | + assert_equal (fetch.await_request pending) Completed(Error(TimedOut)) |
| 875 | + fetch.cancel_request pending |
| 876 | + |
| 877 | + // Time spent between start and await must not turn an elapsed deadline |
| 878 | + // into the negative timeout that means "wait forever" in std/process. |
| 879 | + let opts = Options(..local (), deadline: fetch.deadline_in 50) |
| 880 | + let pending = pending_get "http" (stalling_server ()) opts |
| 881 | + process.sleep 100 |
| 882 | + assert_equal (fetch.await_request pending) Completed(Error(TimedOut)) |
| 883 | + fetch.cancel_request pending |
| 884 | + } |
| 885 | +} |
| 886 | + |
| 661 | 887 | // ---- TLS ----------------------------------------------------------------------- |
| 662 | 888 | |
| 663 | 889 | @test |