Skip to content

Commit 746ef70

Browse files
committed
Add test for handling event if queue was empty in periodic scheduler
Previously if the queue was empty and the loop thread was active the scheduler took quite some time to pick up the new event. Check that this is done in a timely fashion to avoid regressions in code. Signed-off-by: Frediano Ziglio <[email protected]>
1 parent a85b4ab commit 746ef70

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

ocaml/libs/xapi-stdext/lib/xapi-stdext-threads/scheduler_test.ml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,28 @@ let test_remove_self () =
5656
let elapsed_ms = elapsed_ms cnt in
5757
Alcotest.check is_less "small time" 300 elapsed_ms
5858

59+
let test_empty () =
60+
let finished = Event.new_channel () in
61+
Scheduler.add_to_queue "one" Scheduler.OneShot 0.001 (fun () ->
62+
send finished true
63+
) ;
64+
start_schedule () ;
65+
Alcotest.(check bool) "finished" true (receive finished) ;
66+
(* wait loop to go to wait with no work to do *)
67+
Thread.delay 0.1 ;
68+
Scheduler.add_to_queue "two" Scheduler.OneShot 0.001 (fun () ->
69+
send finished true
70+
) ;
71+
let cnt = Mtime_clock.counter () in
72+
Alcotest.(check bool) "finished" true (receive finished) ;
73+
let elapsed_ms = elapsed_ms cnt in
74+
Alcotest.check is_less "small time" 100 elapsed_ms
75+
5976
let tests =
6077
[
6178
("test_single", `Quick, test_single)
6279
; ("test_remove_self", `Quick, test_remove_self)
80+
; ("test_empty", `Quick, test_empty)
6381
]
6482

6583
let () = Alcotest.run "Scheduler" [("generic", tests)]

0 commit comments

Comments
 (0)