Compare commits

..

5 Commits
0.5.1 ... main

Author SHA1 Message Date
Mark Spanbroek
572c897a4e version 0.5.4 2025-04-08 10:38:46 +02:00
gmega
73c08f77af feat: add tunable polling interval with conservative defaults to async predicate checks 2025-03-21 14:52:32 -03:00
Ben
5154c0d79d
bumps ci to nim 1.6.18 2024-08-14 10:44:49 +02:00
Ben
32df0f19d6
version 0.5.2 2024-08-14 10:11:51 +02:00
gmega
12c356672d add exception handling for Chronos V4 2024-01-15 19:28:09 -03:00
9 changed files with 45 additions and 20 deletions

View File

@ -7,7 +7,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
nim: [stable, 1.6.14]
nim: [stable, 1.6.18]
os: [ubuntu-latest, macOS-latest, windows-latest]
steps:
- uses: actions/checkout@v2

View File

@ -11,7 +11,7 @@ Use the [Nimble][2] package manager to add asynctest to an existing project.
Add the following to its .nimble file:
```nim
requires "asynctest >= 0.5.1 & < 0.6.0"
requires "asynctest >= 0.5.4 & < 0.6.0"
```
Usage

View File

@ -1,4 +1,4 @@
version = "0.5.1"
version = "0.5.4"
author = "asynctest Authors"
description = "Test asynchronous code"
license = "MIT"

View File

@ -1,14 +1,14 @@
import std/asyncdispatch
import std/times
template eventually*(expression: untyped, timeout=5000): bool =
template eventually*(expression: untyped, timeout=5000, pollInterval=1000): bool =
proc eventually: Future[bool] {.async.} =
let endTime = getTime() + initDuration(milliseconds=timeout)
while not expression:
if endTime < getTime():
return false
await sleepAsync(10)
await sleepAsync(pollInterval)
return true
await eventually()

View File

@ -1,14 +1,24 @@
import pkg/chronos
import pkg/chronos/config
template eventually*(expression: untyped, timeout=5000): bool =
template eventuallyProcSignature(body: untyped): untyped =
when compiles(config.chronosHandleException):
proc eventually: Future[bool] {.async: (handleException: true,
raises: [AsyncExceptionError, CancelledError]).} =
body
else:
proc eventually: Future[bool] {.async.} =
body
template eventually*(expression: untyped, timeout=5000, pollInterval=1000): bool =
bind Moment, now, milliseconds
proc eventually: Future[bool] {.async.} =
eventuallyProcSignature:
let endTime = Moment.now() + timeout.milliseconds
while not expression:
if endTime < Moment.now():
return false
await sleepAsync(10.milliseconds)
await sleepAsync(pollInterval.milliseconds)
return true
await eventually()

View File

@ -1,4 +0,0 @@
# begin Nimble config (version 1)
when fileExists("nimble.paths"):
include "nimble.paths"
# end Nimble config

View File

@ -1,4 +0,0 @@
{
"version": 1,
"packages": {}
}

View File

@ -9,3 +9,10 @@ suite "eventually and std/times":
test "compiles when both chronos and std/times are imported":
check eventually true
test "compiles when the expression handed to eventually can raise Exception":
proc aProc(): bool {.raises: [Exception].} = raise newException(
Exception, "an exception")
check:
compiles:
discard eventually aProc()

View File

@ -42,13 +42,13 @@ suite "eventually":
inc tries
tries == 3
check eventually becomesTrue()
check eventually(becomesTrue(), pollInterval=10)
test "becomes false after timeout":
proc remainsFalse: bool = false
check not eventually(remainsFalse(), timeout=100)
check not eventually(remainsFalse(), timeout=100, pollInterval=10)
test "becomes true during timeout":
@ -56,7 +56,7 @@ suite "eventually":
sleep(100)
true
check eventually(slowTrue(), timeout=50)
check eventually(slowTrue(), timeout=50, pollInterval=10)
test "works with async procedures":
@ -70,5 +70,21 @@ suite "eventually":
x = 42
let future = slowProcedure()
check eventually x == 42
check eventually(x == 42, pollInterval=10)
await future
test "respects poll interval":
var evaluations: int = 0
# If we try to access this from the closure, it will crash later with
# a segfault, so pass as var.
proc expensivePredicate(counter: var int): bool =
inc counter
return false
check not eventually(evaluations.expensivePredicate(), pollInterval=100, timeout=500)
check 1 <= evaluations and evaluations <= 6
evaluations = 0
check not eventually(evaluations.expensivePredicate(), pollInterval=10, timeout=500)
check 20 <= evaluations and evaluations <= 51