File: TestUtils.hs

package info (click to toggle)
haskell-concurrent-extra 0.7.0.12-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 184 kB
  • sloc: haskell: 1,040; makefile: 6
file content (57 lines) | stat: -rw-r--r-- 1,734 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{-# LANGUAGE CPP
           , NoImplicitPrelude
           , ScopedTypeVariables
  #-}

module TestUtils where


-------------------------------------------------------------------------------
-- Imports
-------------------------------------------------------------------------------

-- from base:
import Control.Applicative ( (<$>) )
import Control.Concurrent  ( threadDelay )
import Control.Exception   ( try, SomeException )
import Control.Monad       ( return )
import Data.Bool           ( Bool, not )
import Data.Either         ( Either(Left, Right) )
import Data.Int            ( Int )
import Data.Maybe          ( isJust )
import Prelude             ( String )
import System.IO           ( IO )
import System.Timeout      ( timeout )

#if __GLASGOW_HASKELL__ < 700
import Prelude             ( fromInteger )
import Control.Monad       ( (>>=), fail )
#endif

-- from HUnit:
import Test.HUnit          ( Assertion, assertFailure )


-------------------------------------------------------------------------------
-- Utilities for testing
-------------------------------------------------------------------------------

-- Exactly 1 moment. Currently equal to 0.005 seconds.
a_moment :: Int
a_moment = 5000

wait_a_moment :: IO ()
wait_a_moment = threadDelay a_moment

-- True if the action 'a' evaluates within 't' μs.
within :: Int -> IO a -> IO Bool
within t a = isJust <$> timeout t a

notWithin :: Int -> IO a -> IO Bool
notWithin t a = not <$> within t a

assertException :: String -> IO a -> Assertion
assertException errMsg a = do e <- try a
                              case e of
                                Left (_ :: SomeException ) -> return ()
                                Right _ -> assertFailure errMsg