Skip to content

Commit

Permalink
test: the surface level API
Browse files Browse the repository at this point in the history
  • Loading branch information
hargoniX committed Jan 5, 2025
1 parent 77d8e67 commit 7109127
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions tests/lean/run/async_surface_sleep.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import Std.Internal.Async.Timer

/-
these tests are just some preliminary ones as `async_sleep.lean` already contains extensive tests
for the entire timer state machine and `Async.Timer` is merely a light wrapper around it.
-/

open Std.Internal.IO.Async

def BASE_DURATION : Std.Time.Millisecond.Offset := 10

namespace SleepTest

def oneSleep : IO Unit := do
let task ← go
assert! (← task.block) == 37
where
go : IO (AsyncTask Nat) := do
let sleep ← Sleep.mk BASE_DURATION
(← sleep.wait).mapIO fun _ =>
return 37

def doubleSleep : IO Unit := do
let task ← go
assert! (← task.block) == 37
where
go : IO (AsyncTask Nat) := do
let sleep ← Sleep.mk BASE_DURATION
(← sleep.wait).bindIO fun _ => do
(← sleep.wait).mapIO fun _ =>
return 37

def resetSleep : IO Unit := do
let task ← go
assert! (← task.block) == 37
where
go : IO (AsyncTask Nat) := do
let sleep ← Sleep.mk BASE_DURATION
let waiter ← sleep.wait
sleep.reset
waiter.mapIO fun _ =>
return 37

def simpleSleep : IO Unit := do
let task ← go
assert! (← task.block) == 37
where
go : IO (AsyncTask Nat) := do
(← sleep BASE_DURATION).mapIO fun _ =>
return 37

#eval oneSleep
#eval doubleSleep
#eval resetSleep
#eval simpleSleep

end SleepTest

namespace IntervalTest

def oneSleep : IO Unit := do
let task ← go
assert! (← task.block) == 37
where
go : IO (AsyncTask Nat) := do
let interval ← Interval.mk BASE_DURATION
(← interval.tick).mapIO fun _ => do
interval.stop
return 37

def doubleSleep : IO Unit := do
let task ← go
assert! (← task.block) == 37
where
go : IO (AsyncTask Nat) := do
let interval ← Interval.mk BASE_DURATION
(← interval.tick).bindIO fun _ => do
(← interval.tick).mapIO fun _ => do
interval.stop
return 37

def resetSleep : IO Unit := do
let task ← go
assert! (← task.block) == 37
where
go : IO (AsyncTask Nat) := do
let interval ← Interval.mk BASE_DURATION
(← interval.tick).bindIO fun _ => do
let waiter ← interval.tick
interval.reset
waiter.mapIO fun _ => do
interval.stop
return 37

#eval oneSleep
#eval doubleSleep
#eval resetSleep

end IntervalTest

0 comments on commit 7109127

Please sign in to comment.