nim-codex/tests/codex/utils/testtimer.nim
markspanbroek 7a0a48e4a5
Fix warnings (drops Nim 1.2) (#348)
* [build] disable XCannotRaiseY hint

There are too many {.raises:[Defect].} in the
libraries that we use, drowning out all other
warnings and hints

* [build] disable BareExcept warning

Not yet enabled in a released version of Nim,
so libraries that we depend on have not fixed
this yet, drowning out our own hints and warnings

* [build] disable DotLikeOps warning

dot-like ops were an experiment that is not going
land in Nim

* [build] compile log statements in tests

When running tests, all log statements are compiled.
They are filtered out at runtime during a test run.

* [build] do not build executable when running unit test

It's already built in the integration test

* [build] Fix warnings

- remove unused code
- remove unused imports
- stop using deprecated stuff

* [build] Put compiler flags behind nim version checks

* [CI] remove Nim 1.2 compatibility
2023-03-09 12:23:45 +01:00

84 lines
2.2 KiB
Nim

## Nim-Codex
## Copyright (c) 2023 Status Research & Development GmbH
## Licensed under either of
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
## at your option.
## This file may not be copied, modified, or distributed except according to
## those terms.
import pkg/chronos
import pkg/asynctest
import codex/utils/timer
import ../helpers/eventually
suite "Timer":
var timer1: Timer
var timer2: Timer
var output: string
var numbersState = 0
var lettersState = 'a'
proc numbersCallback(): Future[void] {.async.} =
output &= $numbersState
inc numbersState
proc lettersCallback(): Future[void] {.async.} =
output &= $lettersState
inc lettersState
proc exceptionCallback(): Future[void] {.async.} =
raise newException(CatchableError, "Test Exception")
proc startNumbersTimer() =
timer1.start(numbersCallback, 10.milliseconds)
proc startLettersTimer() =
timer2.start(lettersCallback, 10.milliseconds)
setup:
timer1 = Timer.new()
timer2 = Timer.new()
output = ""
numbersState = 0
lettersState = 'a'
teardown:
await timer1.stop()
await timer2.stop()
test "Start timer1 should execute callback":
startNumbersTimer()
check eventually output == "0"
test "Start timer1 should execute callback multiple times":
startNumbersTimer()
check eventually output == "012"
test "Starting timer1 multiple times has no impact":
startNumbersTimer()
startNumbersTimer()
startNumbersTimer()
check eventually output == "01234"
test "Stop timer1 should stop execution of the callback":
startNumbersTimer()
check eventually output == "012"
await timer1.stop()
await sleepAsync(30.milliseconds)
let stoppedOutput = output
await sleepAsync(30.milliseconds)
check output == stoppedOutput
test "Exceptions raised in timer callback are handled":
timer1.start(exceptionCallback, 10.milliseconds)
await sleepAsync(30.milliseconds)
await timer1.stop()
test "Starting both timers should execute callbacks sequentially":
startNumbersTimer()
startLettersTimer()
check eventually output == "0a1b2c3d4e"