lean4-htt/tests/lean/promise.lean
Sebastian Ullrich 7c79f05cd4
feat: API to avoid deadlocks from dropped promises (#6958)
This PR improves the `Promise` API by considering how dropped promises
can lead to never-finished tasks.
2025-02-07 15:33:10 +00:00

29 lines
590 B
Text

import Std.Sync.Channel
open IO
-- not in the run/ directory because then it would be run with -j0
#eval do
let promise ← Promise.new
promise.resolve 42
assert! promise.result?.get = some 42
/- Dropping a promise resolves `result?` to `none`. -/
#eval do
let promise : Promise Nat ← Promise.new
assert! promise.result?.get = none
#eval do
let ch ← Std.Channel.new
let out ← IO.mkRef #[]
ch.send 0
let drainFinished ← ch.forAsync fun x => out.modify (·.push x)
ch.send 1
ch.close
ch.send 2
IO.wait drainFinished
assert! (← out.get) = #[0, 1]