From 7bdf6f6cef44a9ece1322657497d7b843dec026b Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Tue, 12 Nov 2019 10:17:50 -0600 Subject: [PATCH] test: use more deterministic test for timer --- src/threads/CCTimer.ml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/threads/CCTimer.ml b/src/threads/CCTimer.ml index 2315c0280..9bd3226c9 100644 --- a/src/threads/CCTimer.ml +++ b/src/threads/CCTimer.ml @@ -157,16 +157,19 @@ let every ?delay timer d ~f = let start = Unix.gettimeofday() in let timer = create() in let res = CCLock.create 0 in + let sem = CCSemaphore.create 1 in + CCSemaphore.acquire 1 sem; let stop = ref 0. in every timer 0.1 ~f:(fun () -> if CCLock.incr_then_get res > 5 then ( stop := Unix.gettimeofday(); + CCSemaphore.release 1 sem; raise ExitEvery )); - Thread.delay 0.7; + CCSemaphore.acquire 1 sem; (* wait *) OUnit.assert_equal ~printer:CCInt.to_string 6 (CCLock.get res); - OUnit.assert_bool "estimate delay" (abs_float (!stop -. start -. 0.5) < 0.1); + OUnit.assert_bool "estimate delay" (abs_float (!stop -. start -. 0.5) < 0.2); *) let active timer = not timer.stop