2019-02-06 14:49:11 +00:00
|
|
|
# Chronos Test Suite
|
|
|
|
# (c) Copyright 2018-Present
|
2018-05-22 22:29:07 +00:00
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
import std/os
|
|
|
|
import unittest2
|
2019-02-06 14:49:11 +00:00
|
|
|
import ../chronos, ../chronos/timer
|
2018-05-22 22:29:07 +00:00
|
|
|
|
2023-03-31 05:35:04 +00:00
|
|
|
{.used.}
|
2019-10-24 13:01:57 +00:00
|
|
|
|
2022-06-28 13:47:59 +00:00
|
|
|
static:
|
|
|
|
doAssert Moment.high - Moment.low == Duration.high
|
|
|
|
doAssert Moment.low.epochSeconds == 0
|
|
|
|
doAssert Moment.low.epochNanoSeconds == 0
|
|
|
|
|
2020-11-13 12:22:58 +00:00
|
|
|
suite "Asynchronous timers & steps test suite":
|
2019-03-30 22:31:10 +00:00
|
|
|
const TimersCount = 10
|
2018-05-22 22:29:07 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc timeWorker(time: Duration): Future[Duration] {.async.} =
|
|
|
|
var st = Moment.now()
|
|
|
|
await sleepAsync(time)
|
|
|
|
var et = Moment.now()
|
|
|
|
result = et - st
|
2018-05-22 22:29:07 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc waitAll[T](futs: seq[Future[T]]): Future[void] =
|
|
|
|
var counter = len(futs)
|
|
|
|
var retFuture = newFuture[void]("waitAll")
|
|
|
|
proc cb(udata: pointer) =
|
|
|
|
dec(counter)
|
|
|
|
if counter == 0:
|
|
|
|
retFuture.complete()
|
|
|
|
for fut in futs:
|
|
|
|
fut.addCallback(cb)
|
|
|
|
return retFuture
|
2018-05-22 22:29:07 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc test(timeout: Duration): Future[Duration] {.async.} =
|
|
|
|
var workers = newSeq[Future[Duration]](TimersCount)
|
|
|
|
for i in 0..<TimersCount:
|
|
|
|
workers[i] = timeWorker(timeout)
|
|
|
|
await waitAll(workers)
|
|
|
|
var sum: Duration
|
|
|
|
for i in 0..<TimersCount:
|
|
|
|
var time = workers[i].read()
|
|
|
|
sum = sum + time
|
|
|
|
result = sum div 10'i64
|
2018-05-22 22:29:07 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
proc testTimer(): bool =
|
|
|
|
let a = Moment.now()
|
|
|
|
waitFor(sleepAsync(1000.milliseconds))
|
|
|
|
let b = Moment.now()
|
|
|
|
let d = b - a
|
2019-06-27 10:12:52 +00:00
|
|
|
result = (d >= 1000.milliseconds) and (d <= 3000.milliseconds)
|
|
|
|
if not result:
|
|
|
|
echo d
|
2019-03-24 16:57:36 +00:00
|
|
|
|
2019-03-30 22:31:10 +00:00
|
|
|
test "Timer reliability test [" & asyncTimer & "]":
|
|
|
|
check testTimer() == true
|
|
|
|
test $TimersCount & " timers with 10ms timeout":
|
|
|
|
var res = waitFor(test(10.milliseconds))
|
|
|
|
check (res >= 10.milliseconds) and (res <= 100.milliseconds)
|
|
|
|
test $TimersCount & " timers with 100ms timeout":
|
|
|
|
var res = waitFor(test(100.milliseconds))
|
|
|
|
check (res >= 100.milliseconds) and (res <= 1000.milliseconds)
|
|
|
|
test $TimersCount & " timers with 1000ms timeout":
|
|
|
|
var res = waitFor(test(1000.milliseconds))
|
|
|
|
check (res >= 1000.milliseconds) and (res <= 5000.milliseconds)
|
2022-11-02 07:09:15 +00:00
|
|
|
test "Timer stringification test":
|
|
|
|
check:
|
|
|
|
$weeks(1) == "1w"
|
|
|
|
$days(1) == "1d"
|
|
|
|
$hours(1) == "1h"
|
|
|
|
$minutes(1) == "1m"
|
|
|
|
$seconds(1) == "1s"
|
|
|
|
$milliseconds(1) == "1ms"
|
|
|
|
$microseconds(1) == "1us"
|
|
|
|
$nanoseconds(1) == "1ns"
|
|
|
|
$(weeks(1) + days(1)) == "1w1d"
|
|
|
|
$(days(1) + hours(1)) == "1d1h"
|
|
|
|
$(hours(1) + minutes(1)) == "1h1m"
|
|
|
|
$(minutes(1) + seconds(1)) == "1m1s"
|
|
|
|
$(seconds(1) + milliseconds(1)) == "1s1ms"
|
|
|
|
$(milliseconds(1) + microseconds(1)) == "1ms1us"
|
|
|
|
$nanoseconds(1_000_000_000) == "1s"
|
|
|
|
$nanoseconds(1_900_000_000) == "1s900ms"
|
|
|
|
$nanoseconds(1_000_900_000) == "1s900us"
|
|
|
|
$nanoseconds(1_000_000_900) == "1s900ns"
|
|
|
|
$nanoseconds(1_800_700_000) == "1s800ms700us"
|
|
|
|
$nanoseconds(1_800_000_600) == "1s800ms600ns"
|
|
|
|
|
2020-11-13 12:22:58 +00:00
|
|
|
test "Asynchronous steps test":
|
|
|
|
var fut1 = stepsAsync(1)
|
|
|
|
var fut2 = stepsAsync(2)
|
|
|
|
var fut3 = stepsAsync(3)
|
2023-09-15 16:38:39 +00:00
|
|
|
|
2020-11-13 12:22:58 +00:00
|
|
|
check:
|
|
|
|
fut1.completed() == false
|
|
|
|
fut2.completed() == false
|
|
|
|
fut3.completed() == false
|
2023-09-15 16:38:39 +00:00
|
|
|
|
|
|
|
# We need `fut` because `stepsAsync` do not power `poll()` anymore.
|
|
|
|
block:
|
|
|
|
var fut {.used.} = sleepAsync(50.milliseconds)
|
|
|
|
poll()
|
|
|
|
|
2020-11-13 12:22:58 +00:00
|
|
|
check:
|
|
|
|
fut1.completed() == true
|
|
|
|
fut2.completed() == false
|
|
|
|
fut3.completed() == false
|
2023-09-15 16:38:39 +00:00
|
|
|
|
|
|
|
block:
|
|
|
|
var fut {.used.} = sleepAsync(50.milliseconds)
|
|
|
|
poll()
|
|
|
|
|
2020-11-13 12:22:58 +00:00
|
|
|
check:
|
|
|
|
fut2.completed() == true
|
|
|
|
fut3.completed() == false
|
2023-09-15 16:38:39 +00:00
|
|
|
|
|
|
|
block:
|
|
|
|
var fut {.used.} = sleepAsync(50.milliseconds)
|
|
|
|
poll()
|
|
|
|
|
2020-11-13 12:22:58 +00:00
|
|
|
check:
|
|
|
|
fut3.completed() == true
|