Skip to content

Commit a85b4ab

Browse files
committed
Add test for removing periodic event in periodic scheduler
Potentially a periodic event can be cancelled while this is processed. Simulate this behavior removing the event inside the handler. This was fixed by previous commit. Signed-off-by: Frediano Ziglio <[email protected]>
1 parent eaeca75 commit a85b4ab

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ let send event data = Event.(send event data |> sync)
2424

2525
let receive event = Event.(receive event |> sync)
2626

27+
let elapsed_ms cnt =
28+
let elapsed_ns = Mtime_clock.count cnt |> Mtime.Span.to_uint64_ns in
29+
Int64.(div elapsed_ns 1000000L |> to_int)
30+
31+
let is_less = Alcotest.(testable (pp int)) Stdlib.( > )
32+
2733
let test_single () =
2834
let finished = Event.new_channel () in
2935
Scheduler.add_to_queue "one" Scheduler.OneShot 0.001 (fun () ->
@@ -32,6 +38,28 @@ let test_single () =
3238
start_schedule () ;
3339
Alcotest.(check bool) "result" true (receive finished)
3440

35-
let tests = [("test_single", `Quick, test_single)]
41+
let test_remove_self () =
42+
let which = Event.new_channel () in
43+
Scheduler.add_to_queue "self" (Scheduler.Periodic 0.001) 0.001 (fun () ->
44+
(* this should remove the periodic scheduling *)
45+
Scheduler.remove_from_queue "self" ;
46+
(* add an operation to stop the test *)
47+
Scheduler.add_to_queue "stop" Scheduler.OneShot 0.1 (fun () ->
48+
send which "stop"
49+
) ;
50+
send which "self"
51+
) ;
52+
start_schedule () ;
53+
let cnt = Mtime_clock.counter () in
54+
Alcotest.(check string) "same event name" "self" (receive which) ;
55+
Alcotest.(check string) "same event name" "stop" (receive which) ;
56+
let elapsed_ms = elapsed_ms cnt in
57+
Alcotest.check is_less "small time" 300 elapsed_ms
58+
59+
let tests =
60+
[
61+
("test_single", `Quick, test_single)
62+
; ("test_remove_self", `Quick, test_remove_self)
63+
]
3664

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

0 commit comments

Comments
 (0)