Test timers cosmetic changes

This commit is contained in:
Raycho Mukelov 2023-08-25 12:33:31 +03:00
parent 93fa2d632c
commit a34d9c4288
2 changed files with 32 additions and 19 deletions

View File

@ -10,6 +10,7 @@
import std/asyncdispatch import std/asyncdispatch
import std/locks import std/locks
import ../raft/types import ../raft/types
import uuids
export asyncdispatch export asyncdispatch
@ -48,13 +49,15 @@ proc RaftTimerCancelCustomImpl*(timer: RaftTimer): bool {.nimcall, gcsafe, disca
proc RaftTimerPollThread() {.thread, nimcall, gcsafe.} = proc RaftTimerPollThread() {.thread, nimcall, gcsafe.} =
while running: while running:
try: try:
poll() poll()
debugEcho activeDescriptors()
except ValueError as e: except ValueError as e:
# debugEcho e.msg debugEcho e.msg
# Add a 'dummy' timer if no other handles are present to prevent more # Add a 'dummy' timer if no other handles are present to prevent more
# ValueError exceptions this is a workaround for a asyncdyspatch bug # ValueError exceptions this is a workaround for a asyncdyspatch bug
# see - https://github.com/nim-lang/Nim/issues/14564 # see - https://github.com/nim-lang/Nim/issues/14564
addTimer(1, false, proc (fd: AsyncFD): bool {.closure, gcsafe.} = false) addTimer(10000, false, proc (fd: AsyncFD): bool {.closure, gcsafe.} = false)
proc RaftTimerJoinPollThread*() {.nimcall, gcsafe.} = proc RaftTimerJoinPollThread*() {.nimcall, gcsafe.} =
joinThread(pollThr) joinThread(pollThr)

View File

@ -16,11 +16,11 @@ import random
const const
MAX_TIMERS = 50 MAX_TIMERS = 50
SLOW_TIMERS_MIN = 300 SLOW_TIMERS_MIN = 300
SLOW_TIMERS_MAX = 350 SLOW_TIMERS_MAX = 400
FAST_TIMERS_MIN = 20 FAST_TIMERS_MIN = 10
FAST_TIMERS_MAX = 150 FAST_TIMERS_MAX = 100
WAIT_FOR_SLOW_TIMERS = 225 WAIT_FOR_SLOW_TIMERS = 200
FINAL_WAIT = 125 FINAL_WAIT = 300
proc timersRunner() = proc timersRunner() =
var var
@ -32,31 +32,41 @@ proc timersRunner() =
discard discard
suite "Create and test basic timers": suite "Create and test basic timers":
test "Start timers": test "Start timers":
RaftTimerStartCustomImpl(false) RaftTimerStartCustomImpl(false)
check true
test "Create 'slow' timers": test "Create 'slow' and 'fast' timers":
for i in 0..MAX_TIMERS: for i in 0..MAX_TIMERS:
slowTimers[i] = RaftTimerCreateCustomImpl(max(SLOW_TIMERS_MIN, rand(SLOW_TIMERS_MAX)), true, RaftDummyTimerCallback) slowTimers[i] = RaftTimerCreateCustomImpl(max(SLOW_TIMERS_MIN, rand(SLOW_TIMERS_MAX)), true, RaftDummyTimerCallback)
check true
test "Create 'fast' timers":
for i in 0..MAX_TIMERS: for i in 0..MAX_TIMERS:
fastTimers[i] = RaftTimerCreateCustomImpl(max(FAST_TIMERS_MIN, rand(FAST_TIMERS_MAX)), true, RaftDummyTimerCallback) fastTimers[i] = RaftTimerCreateCustomImpl(max(FAST_TIMERS_MIN, rand(FAST_TIMERS_MAX)), true, RaftDummyTimerCallback)
check true
test "Wait for and cancel 'slow' timers": test "Wait for and cancel 'slow' timers":
waitFor sleepAsync(WAIT_FOR_SLOW_TIMERS) var fut = sleepAsync(WAIT_FOR_SLOW_TIMERS)
while not fut.finished:
discard
for i in 0..MAX_TIMERS: for i in 0..MAX_TIMERS:
RaftTimerCancelCustomImpl(slowTimers[i]) RaftTimerCancelCustomImpl(slowTimers[i])
check true
test "Wait and stop timers": test "Wait and stop timers":
waitFor sleepAsync(FINAL_WAIT) waitFor sleepAsync(FINAL_WAIT)
RaftTimerStopCustomImpl(true) RaftTimerStopCustomImpl()
check true
test "Check timers consistency": test "Check timers consistency":
var
pass = true
for i in 0..MAX_TIMERS: for i in 0..MAX_TIMERS:
if not fastTimers[i].expired or not slowTimers[i].canceled: if not fastTimers[i].expired:
check false pass = false
check true break
if not slowTimers[i].canceled:
pass = false
break
check pass
if isMainModule: if isMainModule:
timersRunner() timersRunner()