Skip to content

Commit

Permalink
test: use more deterministic test for timer
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Nov 12, 2019
1 parent 7cefde4 commit 7bdf6f6
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/threads/CCTimer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7bdf6f6

Please sign in to comment.