lean4-htt/tests/elab/async_base_functions.lean
Garmelon 08eb78a5b2
chore: switch to new test/bench suite (#12590)
This PR sets up the new integrated test/bench suite. It then migrates
all benchmarks and some related tests to the new suite. There's also
some documentation and some linting.

For now, a lot of the old tests are left alone so this PR doesn't become
even larger than it already is. Eventually, all tests should be migrated
to the new suite though so there isn't a confusing mix of two systems.
2026-02-25 13:51:53 +00:00

77 lines
2.1 KiB
Text

import Std.Internal.Async
import Std.Sync.Mutex
open Std
open Std.Internal.IO.Async
def wait (ms : UInt32) (ref : Std.Mutex Nat) (val : Nat) : Async Unit := do
ref.atomically (·.modify (· * val))
IO.sleep ms
ref.atomically (·.modify (· + val))
-- Tests
def sequential : Async Unit := do
let ref ← Std.Mutex.new 0
wait 200 ref 1
wait 400 ref 2
ref.atomically (·.modify (· * 10))
assert! (← ref.atomically (·.get)) == 40
#eval do (← sequential.toEIO).block
def conc : Async Unit := do
let ref ← Std.Mutex.new 0
discard <| Async.concurrently (wait 200 ref 1) (wait 1000 ref 2)
ref.atomically (·.modify (· * 10))
assert! (← ref.atomically (·.get)) == 30
#eval do (← conc.toEIO).block
def racer : Async Unit := do
let ref ← Std.Mutex.new 0
Async.race (wait 200 ref 1) (wait 1000 ref 2)
ref.atomically (·.modify (· * 10))
assert! (← ref.atomically (·.get)) == 10
#eval do (← racer.toEIO).block
def concAll : Async Unit := do
let ref ← Std.Mutex.new 0
discard <| Async.concurrentlyAll #[(wait 200 ref 1), (wait 1000 ref 2)]
ref.atomically (·.modify (· * 10))
assert! (← ref.atomically (·.get)) == 30
#eval do (← concAll.toEIO).block
def racerAll : Async Unit := do
let ref ← Std.Mutex.new 0
Async.raceAll #[(wait 200 ref 1), (wait 1000 ref 2)]
ref.atomically (·.modify (· * 10))
assert! (← ref.atomically (·.get)) == 10
#eval do (← racerAll.toEIO).block
def racerAllNotCancels : Async Unit := do
let ref ← Std.Mutex.new 0
Async.raceAll #[(wait 200 ref 1), (wait 700 ref 2)]
ref.atomically (·.modify (· * 10))
IO.sleep 1000
assert! (← ref.atomically (·.get)) == 12
#eval do (← racerAllNotCancels.toEIO).block
def racerAllError : Async Unit := do
let ref ← Std.Mutex.new 0
Async.raceAll #[(wait 400 ref 2), throw (IO.userError "error wins")]
/-- error: error wins -/
#guard_msgs in
#eval do (← racerAllError.toEIO).block
def racerAllErrorLost : Async Unit := do
let result ← Async.raceAll #[(do IO.sleep 1000; throw (IO.userError "error wins")) , (do IO.sleep 200; pure 10)]
assert! result = 10
#eval do (← racerAllErrorLost.toEIO).block