syndicate-nim/tests/test_timers.nim

42 lines
1021 B
Nim
Raw Normal View History

2023-07-13 14:07:04 +00:00
# SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
2024-02-28 17:17:46 +00:00
import std/[os, times]
2024-02-15 10:52:12 +00:00
import pkg/cps
2024-02-28 17:17:46 +00:00
import pkg/sys/ioqueue
2024-02-19 22:40:40 +00:00
import sam/syndicate
import sam/actors/timers
2023-07-13 14:07:04 +00:00
2024-02-28 17:17:46 +00:00
let actor = bootActor("timer-test") do (facet: Facet):
let ds = facet.newDataspace()
let h = publish(ds, toRecord(Symbol"greet", "hello world!"))
2023-07-13 14:07:04 +00:00
2024-02-28 17:17:46 +00:00
retract(ds, h)
2024-02-16 12:58:17 +00:00
2024-02-28 17:17:46 +00:00
facet.onStop do ():
echo "anonymous stop handler was invoked"
2024-02-16 12:58:17 +00:00
2024-02-28 17:17:46 +00:00
let
oneSec = initDuration(seconds = 1)
halfSec = initDuration(milliseconds = 500)
timers = facet.newDataspace()
spawnTimerActor(timers)
2024-02-15 14:34:13 +00:00
2024-02-28 17:17:46 +00:00
timers.after(initDuration(seconds = 1)) do ():
echo "slept one second"
2024-02-18 00:56:59 +00:00
2024-02-28 17:17:46 +00:00
timers.after(initDuration(seconds = 2)) do ():
echo "slept 3 seconds"
timers.after(initDuration(seconds = 3)) do ():
echo "slept six seconds"
stopActor(facet)
timers.after(initDuration(seconds = 20)) do ():
echo "slept twenty seconds"
2024-02-15 14:34:13 +00:00
2024-02-28 17:17:46 +00:00
var progress = true
while not actor.stopped:
if not run(actor):
ioqueue.run()