'terminate TestTree using Timeout in Haskell Test.Tasty

In Test.Tasty there is a function called mkTimeout. I want to use it on a value of type TestTree. How can I do that? My goal is that I know that there's a possibility of the occurrence of a test case which does not terminate due to faulty implementation.

main = defaultMain $ testGroup "exercises" [
    exercise1,
    exercise2,
    exercise3,
    exercise4
    ]

Each exercise%d is a TestTree. One of them may include endless computation, and I want to know how to terminate that after let's say 5 seconds.

I know how to terminate test cases (properties) (using within in the module Test.Tasty.QuickCheck), which is also doable. But what about terminating TestTrees?

What I'll do as a compensation is asserting using Testable, and then using the timer accompanying the function within:

import Test.QuickCheck

main :: IO ()
main = do
  putStrLn "1:"
  exercise1
  putStrLn "2:"
  exercise2

------------

val1 = True
val2 = True

exercise1 :: IO ()
exercise1 = verboseCheck $ within 2000000 (val1 == val2)

------------

foo :: Bool
foo = foo

exercise2 :: IO ()
exercise2 = verboseCheck $ within 2000000 foo

And by executing the main function, I get the result I wish to get. exercise1 terminates while exercise2 does not terminate because foo is an infinite recursion, but the timer of 2 seconds will terminate the assertion of this property.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source