mirror of
https://github.com/logos-storage/nim-chronos.git
synced 2026-01-03 14:03:06 +00:00
add test for simple non-blocking future
This commit is contained in:
parent
81711f8b11
commit
ce1e03aa4e
@ -14,21 +14,37 @@ suite "profiler metrics test suite":
|
||||
clearRecording()
|
||||
revertCallbacks()
|
||||
resetTime()
|
||||
|
||||
proc recordedMetrics(): ProfilerMetrics =
|
||||
result.processAllEvents(rawRecording)
|
||||
|
||||
test "should compute correct times for a simple future":
|
||||
test "should compute correct times for a simple blocking future":
|
||||
proc simple() {.async.} =
|
||||
advanceTime(50.milliseconds)
|
||||
|
||||
waitFor simple()
|
||||
|
||||
var metrics = ProfilerMetrics()
|
||||
metrics.processAllEvents(rawRecording)
|
||||
var metrics = recordedMetrics()
|
||||
let simpleMetrics = metrics.forProc("simple")
|
||||
|
||||
check simpleMetrics.execTime == 50.milliseconds
|
||||
check simpleMetrics.wallClockTime == 50.milliseconds
|
||||
|
||||
test "should compute correct times for blocking children":
|
||||
test "should compute correct times for a simple non-blocking future":
|
||||
proc simple {.async.} =
|
||||
advanceTime(10.milliseconds)
|
||||
await advanceTimeAsync(50.milliseconds)
|
||||
advanceTime(10.milliseconds)
|
||||
|
||||
waitFor simple()
|
||||
|
||||
var metrics = recordedMetrics()
|
||||
let simpleMetrics = metrics.forProc("simple")
|
||||
|
||||
check simpleMetrics.execTime == 20.milliseconds
|
||||
check simpleMetrics.wallClockTime == 70.milliseconds
|
||||
|
||||
test "should compute correct times whent there is a single blocking child":
|
||||
proc child() {.async.} =
|
||||
advanceTime(10.milliseconds)
|
||||
|
||||
@ -39,8 +55,7 @@ suite "profiler metrics test suite":
|
||||
|
||||
waitFor parent()
|
||||
|
||||
var metrics = ProfilerMetrics()
|
||||
metrics.processAllEvents(rawRecording)
|
||||
var metrics = recordedMetrics()
|
||||
let parentMetrics = metrics.forProc("parent")
|
||||
let childMetrics = metrics.forProc("child")
|
||||
|
||||
|
||||
@ -63,4 +63,22 @@ proc resetTime*() =
|
||||
fakeTime = Moment.now()
|
||||
|
||||
proc advanceTime*(duration: Duration) =
|
||||
fakeTime += duration
|
||||
fakeTime += duration
|
||||
|
||||
proc advanceTimeAsync*(duration: Duration): Future[void] =
|
||||
# Simulates a non-blocking operation that takes the provided duration to
|
||||
# complete.
|
||||
var retFuture = newFuture[void]("advanceTimeAsync")
|
||||
var timer: TimerCallback
|
||||
|
||||
proc completion(data: pointer) {.gcsafe.} =
|
||||
if not(retFuture.finished()):
|
||||
advanceTime(duration)
|
||||
retFuture.complete()
|
||||
|
||||
# The actual value for the timer is irrelevant, the important thing is that
|
||||
# this causes the parent to pause before we advance time.
|
||||
timer = setTimer(Moment.fromNow(10.milliseconds),
|
||||
completion, cast[pointer](retFuture))
|
||||
|
||||
return retFuture
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user