Skip to content

Commit d2019b5

Browse files
committed
Add unit tests for threadext
Signed-off-by: Steven Woods <[email protected]>
1 parent a922278 commit d2019b5

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

lib/xapi-stdext-threads/dune

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
(library
22
(public_name xapi-stdext-threads)
33
(name xapi_stdext_threads)
4+
(modules :standard \ threadext_test)
45
(libraries
56
polly
67
threads
78
unix
89
xapi-stdext-pervasives)
910
)
11+
(test
12+
(name threadext_test)
13+
(modules threadext_test)
14+
(libraries xapi_stdext_threads alcotest mtime.clock.os)
15+
)

lib/xapi-stdext-threads/threadext.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ module Delay : sig
2323
type t
2424

2525
val make : unit -> t
26+
27+
val wait : t -> float -> bool
2628
(** Blocks the calling thread for a given period of time with the option of
2729
returning early if someone calls 'signal'. Returns true if the full time
2830
period elapsed and false if signalled. Note that multple 'signals' are
2931
coalesced; 'signals' sent before 'wait' is called are not lost. *)
3032

31-
val wait : t -> float -> bool
32-
(** Sends a signal to a waiting thread. See 'wait' *)
33-
3433
val signal : t -> unit
34+
(** Sends a signal to a waiting thread. See 'wait' *)
3535
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
(*
2+
* Copyright (C) 2006-2009 Citrix Systems Inc.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published
6+
* by the Free Software Foundation; version 2.1 only. with the special
7+
* exception on linking described in file LICENSE.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*)
14+
15+
module Delay = Xapi_stdext_threads.Threadext.Delay
16+
17+
let test_wait () =
18+
let m = Delay.make () in
19+
let c = Mtime_clock.counter () in
20+
let expected = Mtime.Span.(5 * s) in
21+
let max_error = Mtime.Span.(10 * ms) in
22+
let _ = Delay.wait m 5.0 in
23+
let wait_time = Mtime_clock.count c in
24+
let diff = Mtime.Span.abs_diff expected wait_time in
25+
let cmp = Mtime.Span.compare diff max_error in
26+
Alcotest.(check bool) "diff is smaller than max error" true (cmp < 0)
27+
28+
29+
let () =
30+
Alcotest.run "Threadext" [("wait", [("wait", `Quick, test_wait)])]

xapi-stdext-threads.opam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ homepage: "https://github.com/xapi-project/stdext"
88
bug-reports: "https://github.com/xapi-project/stdext/issues"
99
depends: [
1010
"dune" {>= "2.7"}
11+
"polly"
1112
"ocaml"
1213
"base-threads"
1314
"base-unix"

xapi-stdext-unix.opam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ homepage: "https://github.com/xapi-project/stdext"
88
bug-reports: "https://github.com/xapi-project/stdext/issues"
99
depends: [
1010
"dune" {>= "2.7"}
11+
"polly"
1112
"ocaml"
1213
"base-unix"
1314
"fd-send-recv" {>= "2.0.0"}

0 commit comments

Comments
 (0)