lean4-htt/tests/elab/async_tcp_half.lean
Sofia Rodrigues 2e48cd293a
refactor: move Async and Http from Internal to Std (#13511)
This PR moves Async and Http from Internal to Std
2026-04-23 19:55:22 +00:00

54 lines
1.3 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Std.Async
import Std.Internal.UV
import Std.Net.Addr
open Std.Async
open Std.Net
-- Using this function to create IO Error. For some reason the assert! is not pausing the execution.
def assertBEq [BEq α] [ToString α] (actual expected : α) : IO Unit := do
unless actual == expected do
throw <| IO.userError <|
s!"expected '{expected}', got '{actual}'"
/-- Joe is another client. -/
def runJoe (addr: SocketAddress) : Async Unit := do
let client ← TCP.Socket.Client.mk
client.connect addr
client.send (String.toUTF8 "hello robert!")
client.shutdown
def listenClose : IO Unit := do
let addr := SocketAddressV4.mk (.ofParts 127 0 0 1) 8080
let server ← TCP.Socket.Server.mk
server.bind addr
server.listen 128
def acceptClose : IO Unit := do
let addr := SocketAddressV4.mk (.ofParts 127 0 0 1) 8081
let server ← TCP.Socket.Server.mk
server.bind addr
server.listen 128
let joeTask ← (runJoe addr).toIO
let task ← server.accept |>.toBaseIO
let client ← task.block
let mes ← client.recv? 1024 |>.toBaseIO
let msg ← mes.block
assertBEq (String.fromUTF8? =<< msg) ("hello robert!")
let mes ← client.recv? 1024 |>.toBaseIO
let msg ← mes.block
assertBEq (String.fromUTF8? =<< msg) none
-- Waiting to avoid errors from escaping.
joeTask.block
#eval listenClose