syndicate-nim/tests/test_timers.nim

37 lines
1.1 KiB
Nim
Raw Normal View History

2023-07-13 14:07:04 +00:00
# SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/[asyncdispatch, os, times]
import preserves, syndicate, syndicate/actors/timers
proc now: float64 = getTime().toUnixFloat()
proc testTimers(turn: var Turn; ds: Ref) =
onPublish(turn, ds, ?LaterThan(seconds: now()+1.0)) do:
stderr.writeLine "slept one second once"
onPublish(turn, ds, ?LaterThan(seconds: now()+1.0)) do:
stderr.writeLine "slept one second twice"
onPublish(turn, ds, ?LaterThan(seconds: now()+1.0)) do:
stderr.writeLine "slept one second thrice"
quit()
spawnTimers(turn, ds)
type Args {.preservesDictionary.} = object
dataspace: Ref
proc asInferior: bool =
commandLineParams() == @["--inferior"]
if asInferior():
stderr.writeLine "connect stdio"
runActor("test_timers") do (root: Ref; turn: var Turn):
connectStdio(root, turn)
during(turn, root, ?Args) do (ds: Ref):
testTimers(turn, ds)
else:
stderr.writeLine "use local dataspace"
discard bootDataspace("test_timers") do (ds: Ref; turn: var Turn):
testTimers(turn, ds)
for i in 0..10: poll()