From 84785bbb59480eb7829c71efa1cfce1d96e66c3c Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 1 May 2020 11:58:47 +0300 Subject: [PATCH] tests: fix msvc transient failures when sleep_ms takes longer --- vlib/time/stopwatch_test.v | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/vlib/time/stopwatch_test.v b/vlib/time/stopwatch_test.v index 77943d2a43..41098fd8bc 100644 --- a/vlib/time/stopwatch_test.v +++ b/vlib/time/stopwatch_test.v @@ -1,6 +1,9 @@ import time -fn test_stopwatch_works_as_intended(){ +// NB: on CI jobs, especially msvc ones, sleep_ms may sleep for much more +// time than you have specified. To avoid false positives from CI test +// failures, some of the asserts will be run only if you pass `-d stopwatch` +fn test_stopwatch_works_as_intended() { sw := time.new_stopwatch() // sample code that you want to measure: println('Hello world') @@ -10,19 +13,23 @@ fn test_stopwatch_works_as_intended(){ assert sw.elapsed().nanoseconds() > 0 } -fn test_stopwatch_time_between_pause_and_start_should_be_skipped_in_elapsed(){ +fn test_stopwatch_time_between_pause_and_start_should_be_skipped_in_elapsed() { sw := time.new_stopwatch() time.sleep_ms(10) // A - //eprintln('${sw.elapsed().milliseconds()}ms') + // eprintln('${sw.elapsed().milliseconds()}ms') assert sw.elapsed().milliseconds() >= 10 sw.pause() time.sleep_ms(10) - //eprintln('${sw.elapsed().milliseconds()}ms') + // eprintln('${sw.elapsed().milliseconds()}ms') assert sw.elapsed().milliseconds() >= 10 - assert sw.elapsed().milliseconds() < 20 + $if stopwatch { + assert sw.elapsed().milliseconds() < 20 + } sw.start() time.sleep_ms(10) // B - // Here, sw.elapsed() should be ~10ms (from A) + 10ms (from B) = 20ms + // Here, sw.elapsed() should be ~10ms (from A) + ~10ms (from B) >= 20ms assert sw.elapsed().milliseconds() >= 20 - assert sw.elapsed().milliseconds() < 30 + $if stopwatch { + assert sw.elapsed().milliseconds() < 30 + } }