How to set a timeout for tests with OUnit?

685 views Asked by At

I have some tests on infinite lazy structures that might run indefinitely if the tested function is not correctly implemented, but I can’t find in the OUnit docs how to set a timeout on tests.

2

There are 2 answers

1
rgrinberg On BEST ANSWER

I don't think that oUnit provides this functionality. I remember having to do this a while back and this is the quick hack I've come up with:

let race seconds ~f =
  let ch = Event.new_channel () in
  let timeout = Thread.create (fun () ->
      Thread.delay seconds;
      `Time_out |> Event.send ch |> Event.sync
    ) () in
  let tf = Thread.create (fun () ->
      `Result (f ()) |> Event.send ch |> Event.sync) () in
  let res = ch |> Event.receive |> Event.sync in
  try
    Thread.kill timeout;
    Thread.kill tf;
    res
  with _ -> res

let () =
  let big_sum () =
    let arr = Array.init 1_000_000 (fun x -> x) in
    Array.fold_left (+) 0 arr in
  match race 0.0001 ~f:big_sum with
  | `Time_out -> print_endline "time to upgrade";
  | `Result x -> Printf.printf "sum is: %d\n" x

This worked well enough for my use case but I'd definitely would not recommend using this if only because race will not work as you'd expect if ~f does no allocations or calls Thread.yield manually.

0
Stefan Schmiedl On

If you're using OUnit2, the following should work:

let tests = 
    "suite" >::: [OUnitTest.TestCase ( 
                    OUnitTest.Short,
                    (fun _ -> assert_equal 2 (1+1))
                  );
                  OUnitTest.TestCase (
                    OUnitTest.Long,
                    (fun _ -> assert_equal 4 (2+2))
                  )]

The type test_length is defined as:

type test_length =
|   Immediate
|   Short
|   Long
|   Huge
|   Custom_length of float