add test for simple non-blocking future

This commit is contained in:
gmega 2023-11-27 09:49:44 -03:00
parent 81711f8b11
commit ce1e03aa4e
No known key found for this signature in database
GPG Key ID: FFD8DAF00660270F
2 changed files with 40 additions and 7 deletions

View File

@ -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")

View File

@ -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