2016-04-29 01:00:33 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
2015-10-08 14:05:59 +03:00
|
|
|
{-# LANGUAGE RankNTypes #-}
|
2016-04-29 01:00:33 +03:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE TypeSynonymInstances #-}
|
2015-10-08 14:05:59 +03:00
|
|
|
|
2016-05-26 15:54:13 +03:00
|
|
|
#if __GLASGOW_HASKELL__ >= 800
|
|
|
|
-- Impredicative polymorphism checks got stronger in GHC 8, breaking
|
|
|
|
-- the use of 'unsafeCoerce' below.
|
|
|
|
{-# LANGUAGE ImpredicativeTypes #-}
|
|
|
|
#endif
|
|
|
|
|
2015-10-08 14:05:59 +03:00
|
|
|
-- | This module allows using Deja Fu predicates with HUnit to test
|
|
|
|
-- the behaviour of concurrent systems.
|
|
|
|
module Test.HUnit.DejaFu
|
2016-04-29 01:00:33 +03:00
|
|
|
( -- * Unit testing
|
|
|
|
|
|
|
|
-- | This is supported by the 'Assertable' and 'Testable'
|
|
|
|
-- instances for 'ConcST' and 'ConcIO'. These instances try all
|
|
|
|
-- executions, reporting as failures the cases which throw an
|
|
|
|
-- 'HUnitFailure' exception.
|
|
|
|
--
|
|
|
|
-- @instance Testable (ConcST t ())@
|
|
|
|
-- @instance Assertable (ConcST t ())@
|
|
|
|
-- @instance Testable (ConcIO ())@
|
|
|
|
-- @instance Assertable (ConcIO ())@
|
|
|
|
--
|
|
|
|
-- These instances use the default memory model and schedule bounds.
|
|
|
|
|
|
|
|
-- * Property testing
|
2015-10-08 14:05:59 +03:00
|
|
|
testAuto
|
|
|
|
, testDejafu
|
|
|
|
, testDejafus
|
2016-04-29 01:00:33 +03:00
|
|
|
|
|
|
|
, testAuto'
|
|
|
|
, testDejafu'
|
|
|
|
, testDejafus'
|
|
|
|
|
|
|
|
-- ** @IO@
|
2015-10-08 14:05:59 +03:00
|
|
|
, testAutoIO
|
|
|
|
, testDejafuIO
|
|
|
|
, testDejafusIO
|
|
|
|
|
|
|
|
, testAutoIO'
|
2015-10-08 17:45:05 +03:00
|
|
|
, testDejafuIO'
|
2015-10-08 14:05:59 +03:00
|
|
|
, testDejafusIO'
|
2016-04-29 01:00:33 +03:00
|
|
|
|
|
|
|
-- * Re-exports
|
|
|
|
, Bounds(..)
|
|
|
|
, MemType(..)
|
2015-10-08 14:05:59 +03:00
|
|
|
) where
|
|
|
|
|
2016-04-29 01:00:33 +03:00
|
|
|
import Control.Monad.Catch (try)
|
2016-05-12 20:21:01 +03:00
|
|
|
import Control.Monad.ST (runST)
|
2016-02-09 21:37:50 +03:00
|
|
|
import Data.List (intercalate, intersperse)
|
2015-10-08 14:05:59 +03:00
|
|
|
import Test.DejaFu
|
2016-04-29 01:00:33 +03:00
|
|
|
import Test.DejaFu.Deterministic (ConcST, ConcIO, Trace, ThreadId, ThreadAction, Lookahead, showFail, showTrace)
|
2016-05-12 20:21:01 +03:00
|
|
|
import qualified Test.DejaFu.SCT as SCT
|
2016-04-29 01:00:33 +03:00
|
|
|
import Test.HUnit (Assertable(..), Test(..), Testable(..), assertString)
|
|
|
|
import Test.HUnit.Lang (HUnitFailure(..))
|
|
|
|
|
|
|
|
-- Can't put the necessary forall in the @Assertable ConcST t@
|
|
|
|
-- instance :(
|
|
|
|
import Unsafe.Coerce (unsafeCoerce)
|
|
|
|
|
|
|
|
#if MIN_VERSION_dejafu(0,3,0)
|
|
|
|
type Trc = Trace ThreadId ThreadAction Lookahead
|
|
|
|
#else
|
|
|
|
type Trc = Trace
|
|
|
|
#endif
|
2015-10-08 14:05:59 +03:00
|
|
|
|
2016-05-12 20:21:01 +03:00
|
|
|
sctBoundST :: MemType -> Bounds -> (forall t. ConcST t a) -> [(Either Failure a, Trc)]
|
|
|
|
sctBoundIO :: MemType -> Bounds -> ConcIO a -> IO [(Either Failure a, Trc)]
|
|
|
|
|
|
|
|
#if MIN_VERSION_dejafu(0,4,0)
|
|
|
|
sctBoundST memtype cb conc = runST (SCT.sctBound memtype cb conc)
|
|
|
|
sctBoundIO = SCT.sctBound
|
|
|
|
#else
|
|
|
|
sctBoundST = SCT.sctBound
|
|
|
|
sctBoundIO = SCT.sctBoundIO
|
|
|
|
#endif
|
|
|
|
|
2015-10-08 14:05:59 +03:00
|
|
|
--------------------------------------------------------------------------------
|
2016-04-29 01:00:33 +03:00
|
|
|
-- Unit testing
|
|
|
|
|
|
|
|
instance Testable (ConcST t ()) where
|
|
|
|
test conc = TestCase (assert conc)
|
|
|
|
|
|
|
|
instance Testable (ConcIO ()) where
|
|
|
|
test conc = TestCase (assert conc)
|
|
|
|
|
|
|
|
instance Assertable (ConcST t ()) where
|
|
|
|
assert conc = do
|
|
|
|
let traces = sctBound' conc'
|
|
|
|
assertString . showErr $ assertableP traces
|
|
|
|
|
|
|
|
where
|
|
|
|
conc' :: ConcST t (Either HUnitFailure ())
|
|
|
|
conc' = try conc
|
|
|
|
|
|
|
|
sctBound' :: ConcST t (Either HUnitFailure ()) -> [(Either Failure (Either HUnitFailure ()), Trc)]
|
2016-05-12 20:21:01 +03:00
|
|
|
sctBound' = unsafeCoerce $ sctBoundST defaultMemType defaultBounds
|
2016-04-29 01:00:33 +03:00
|
|
|
|
|
|
|
instance Assertable (ConcIO ()) where
|
|
|
|
assert conc = do
|
|
|
|
traces <- sctBoundIO defaultMemType defaultBounds (try conc)
|
|
|
|
assertString . showErr $ assertableP traces
|
|
|
|
|
|
|
|
assertableP :: Predicate (Either HUnitFailure ())
|
|
|
|
assertableP = alwaysTrue $ \r -> case r of
|
2016-05-26 14:41:10 +03:00
|
|
|
Right (Left (HUnitFailure {})) -> False
|
2016-04-29 01:00:33 +03:00
|
|
|
_ -> True
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Property testing
|
2015-10-08 14:05:59 +03:00
|
|
|
|
|
|
|
-- | Automatically test a computation. In particular, look for
|
|
|
|
-- deadlocks, uncaught exceptions, and multiple return values.
|
2016-06-20 23:45:34 +03:00
|
|
|
--
|
2015-10-08 14:05:59 +03:00
|
|
|
-- This uses the 'Conc' monad for testing, which is an instance of
|
|
|
|
-- 'MonadConc'. If you need to test something which also uses
|
|
|
|
-- 'MonadIO', use 'testAutoIO'.
|
|
|
|
testAuto :: (Eq a, Show a)
|
2015-11-07 21:07:10 +03:00
|
|
|
=> (forall t. ConcST t a)
|
2015-10-08 14:05:59 +03:00
|
|
|
-- ^ The computation to test
|
2015-10-08 23:09:48 +03:00
|
|
|
-> Test
|
2015-11-17 22:29:35 +03:00
|
|
|
testAuto = testAuto' defaultMemType
|
2015-10-08 14:05:59 +03:00
|
|
|
|
|
|
|
-- | Variant of 'testAuto' which tests a computation under a given
|
|
|
|
-- memory model.
|
|
|
|
testAuto' :: (Eq a, Show a)
|
|
|
|
=> MemType
|
|
|
|
-- ^ The memory model to use for non-synchronised @CRef@ operations.
|
2015-11-07 21:07:10 +03:00
|
|
|
-> (forall t. ConcST t a)
|
2015-10-08 14:05:59 +03:00
|
|
|
-- ^ The computation to test
|
2015-10-08 23:09:48 +03:00
|
|
|
-> Test
|
2015-11-19 16:57:14 +03:00
|
|
|
testAuto' memtype conc = testDejafus' memtype defaultBounds conc autocheckCases
|
2015-10-08 14:05:59 +03:00
|
|
|
|
|
|
|
-- | Variant of 'testAuto' for computations which do 'IO'.
|
2015-11-07 20:19:40 +03:00
|
|
|
testAutoIO :: (Eq a, Show a) => ConcIO a -> Test
|
2015-11-17 22:29:35 +03:00
|
|
|
testAutoIO = testAutoIO' defaultMemType
|
2015-10-08 14:05:59 +03:00
|
|
|
|
|
|
|
-- | Variant of 'testAuto'' for computations which do 'IO'.
|
2015-11-07 20:19:40 +03:00
|
|
|
testAutoIO' :: (Eq a, Show a) => MemType -> ConcIO a -> Test
|
2015-11-19 16:57:14 +03:00
|
|
|
testAutoIO' memtype concio = testDejafusIO' memtype defaultBounds concio autocheckCases
|
2015-10-08 14:05:59 +03:00
|
|
|
|
|
|
|
-- | Predicates for the various autocheck functions.
|
2015-10-08 17:15:13 +03:00
|
|
|
autocheckCases :: Eq a => [(String, Predicate a)]
|
|
|
|
autocheckCases =
|
2015-11-12 17:55:54 +03:00
|
|
|
[("Never Deadlocks", representative deadlocksNever)
|
|
|
|
, ("No Exceptions", representative exceptionsNever)
|
2015-10-08 17:15:13 +03:00
|
|
|
, ("Consistent Result", alwaysSame)
|
|
|
|
]
|
2015-10-08 14:05:59 +03:00
|
|
|
|
|
|
|
-- | Check that a predicate holds.
|
2015-10-25 20:05:40 +03:00
|
|
|
testDejafu :: Show a
|
2015-11-07 21:07:10 +03:00
|
|
|
=> (forall t. ConcST t a)
|
2015-10-08 14:05:59 +03:00
|
|
|
-- ^ The computation to test
|
2015-10-08 17:15:13 +03:00
|
|
|
-> String
|
|
|
|
-- ^ The name of the test.
|
2015-10-08 14:05:59 +03:00
|
|
|
-> Predicate a
|
|
|
|
-- ^ The predicate to check
|
2015-10-08 23:09:48 +03:00
|
|
|
-> Test
|
2015-11-19 16:57:14 +03:00
|
|
|
testDejafu = testDejafu' defaultMemType defaultBounds
|
2015-10-08 17:45:05 +03:00
|
|
|
|
|
|
|
-- | Variant of 'testDejafu' which takes a memory model and
|
2016-04-29 01:00:33 +03:00
|
|
|
-- schedule bounds.
|
2015-10-25 20:05:40 +03:00
|
|
|
testDejafu' :: Show a
|
2015-10-08 17:45:05 +03:00
|
|
|
=> MemType
|
|
|
|
-- ^ The memory model to use for non-synchronised @CRef@ operations.
|
2015-11-19 16:57:14 +03:00
|
|
|
-> Bounds
|
|
|
|
-- ^ The schedule bound.
|
2015-11-07 21:07:10 +03:00
|
|
|
-> (forall t. ConcST t a)
|
2015-10-08 17:45:05 +03:00
|
|
|
-- ^ The computation to test
|
|
|
|
-> String
|
|
|
|
-- ^ The name of the test.
|
|
|
|
-> Predicate a
|
|
|
|
-- ^ The predicate to check
|
2015-10-08 23:09:48 +03:00
|
|
|
-> Test
|
2015-11-19 16:57:14 +03:00
|
|
|
testDejafu' memtype cb conc name p = testDejafus' memtype cb conc [(name, p)]
|
2015-10-08 14:05:59 +03:00
|
|
|
|
|
|
|
-- | Variant of 'testDejafu' which takes a collection of predicates to
|
|
|
|
-- test. This will share work between the predicates, rather than
|
|
|
|
-- running the concurrent computation many times for each predicate.
|
2015-10-25 20:05:40 +03:00
|
|
|
testDejafus :: Show a
|
2015-11-07 21:07:10 +03:00
|
|
|
=> (forall t. ConcST t a)
|
2015-10-08 14:05:59 +03:00
|
|
|
-- ^ The computation to test
|
2015-10-08 17:15:13 +03:00
|
|
|
-> [(String, Predicate a)]
|
|
|
|
-- ^ The list of predicates (with names) to check
|
2015-10-08 23:09:48 +03:00
|
|
|
-> Test
|
2015-11-19 16:57:14 +03:00
|
|
|
testDejafus = testDejafus' defaultMemType defaultBounds
|
2015-10-08 14:05:59 +03:00
|
|
|
|
2016-04-29 01:00:33 +03:00
|
|
|
-- | Variant of 'testDejafus' which takes a memory model and schedule
|
|
|
|
-- bounds.
|
2015-10-25 20:05:40 +03:00
|
|
|
testDejafus' :: Show a
|
2015-10-08 14:05:59 +03:00
|
|
|
=> MemType
|
|
|
|
-- ^ The memory model to use for non-synchronised @CRef@ operations.
|
2015-11-19 16:57:14 +03:00
|
|
|
-> Bounds
|
|
|
|
-- ^ The schedule bounds
|
2015-11-07 21:07:10 +03:00
|
|
|
-> (forall t. ConcST t a)
|
2015-10-08 14:05:59 +03:00
|
|
|
-- ^ The computation to test
|
2015-10-08 17:15:13 +03:00
|
|
|
-> [(String, Predicate a)]
|
|
|
|
-- ^ The list of predicates (with names) to check
|
2015-10-08 23:09:48 +03:00
|
|
|
-> Test
|
2016-04-29 01:00:33 +03:00
|
|
|
testDejafus' = testst
|
2015-10-08 14:05:59 +03:00
|
|
|
|
|
|
|
-- | Variant of 'testDejafu' for computations which do 'IO'.
|
2015-11-07 20:19:40 +03:00
|
|
|
testDejafuIO :: Show a => ConcIO a -> String -> Predicate a -> Test
|
2015-11-19 16:57:14 +03:00
|
|
|
testDejafuIO = testDejafuIO' defaultMemType defaultBounds
|
2015-10-08 17:45:05 +03:00
|
|
|
|
|
|
|
-- | Variant of 'testDejafu'' for computations which do 'IO'.
|
2015-11-19 16:57:14 +03:00
|
|
|
testDejafuIO' :: Show a => MemType -> Bounds -> ConcIO a -> String -> Predicate a -> Test
|
|
|
|
testDejafuIO' memtype cb concio name p = testDejafusIO' memtype cb concio [(name, p)]
|
2015-10-08 14:05:59 +03:00
|
|
|
|
|
|
|
-- | Variant of 'testDejafus' for computations which do 'IO'.
|
2015-11-07 20:19:40 +03:00
|
|
|
testDejafusIO :: Show a => ConcIO a -> [(String, Predicate a)] -> Test
|
2015-11-19 16:57:14 +03:00
|
|
|
testDejafusIO = testDejafusIO' defaultMemType defaultBounds
|
2015-10-08 14:05:59 +03:00
|
|
|
|
|
|
|
-- | Variant of 'dejafus'' for computations which do 'IO'.
|
2015-11-19 16:57:14 +03:00
|
|
|
testDejafusIO' :: Show a => MemType -> Bounds -> ConcIO a -> [(String, Predicate a)] -> Test
|
2015-10-08 23:15:46 +03:00
|
|
|
testDejafusIO' = testio
|
2015-10-08 14:05:59 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- HUnit integration
|
|
|
|
|
2015-10-08 23:15:46 +03:00
|
|
|
-- | Produce a HUnit 'Test' from a Deja Fu test.
|
2016-04-29 01:00:33 +03:00
|
|
|
testst :: Show a => MemType -> Bounds -> (forall t. ConcST t a) -> [(String, Predicate a)] -> Test
|
|
|
|
testst memtype cb conc tests = case map toTest tests of
|
2015-10-08 23:15:46 +03:00
|
|
|
[t] -> t
|
|
|
|
ts -> TestList ts
|
|
|
|
|
|
|
|
where
|
|
|
|
toTest (name, p) = TestLabel name . TestCase $
|
|
|
|
assertString . showErr $ p traces
|
|
|
|
|
2016-05-12 20:21:01 +03:00
|
|
|
traces = sctBoundST memtype cb conc
|
2015-10-08 23:15:46 +03:00
|
|
|
|
|
|
|
-- | Produce a HUnit 'Test' from an IO-using Deja Fu test.
|
2015-11-19 16:57:14 +03:00
|
|
|
testio :: Show a => MemType -> Bounds -> ConcIO a -> [(String, Predicate a)] -> Test
|
|
|
|
testio memtype cb concio tests = case map toTest tests of
|
2015-10-08 23:15:46 +03:00
|
|
|
[t] -> t
|
|
|
|
ts -> TestList ts
|
|
|
|
|
|
|
|
where
|
|
|
|
toTest (name, p) = TestLabel name . TestCase $ do
|
|
|
|
-- Sharing of traces probably not possible (without something
|
|
|
|
-- really unsafe) here, as 'test' doesn't allow side-effects
|
|
|
|
-- (eg, constructing an 'MVar' to share the traces after one
|
|
|
|
-- test computed them).
|
2015-11-19 16:57:14 +03:00
|
|
|
traces <- sctBoundIO memtype cb concio
|
2015-10-08 23:15:46 +03:00
|
|
|
assertString . showErr $ p traces
|
2015-10-08 14:05:59 +03:00
|
|
|
|
2016-04-29 01:00:33 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Utilities
|
|
|
|
|
2015-10-08 14:05:59 +03:00
|
|
|
-- | Convert a test result into an error message on failure (empty
|
|
|
|
-- string on success).
|
|
|
|
showErr :: Show a => Result a -> String
|
|
|
|
showErr res
|
|
|
|
| _pass res = ""
|
2015-10-27 18:39:52 +03:00
|
|
|
| otherwise = "Failed after " ++ show (_casesChecked res) ++ " cases:\n" ++ msg ++ unlines failures ++ rest where
|
|
|
|
|
|
|
|
msg = if null (_failureMsg res) then "" else _failureMsg res ++ "\n"
|
2015-10-08 14:05:59 +03:00
|
|
|
|
2016-04-29 01:00:33 +03:00
|
|
|
failures = intersperse "" . map (indent . showres) . take 5 $ _failures res
|
|
|
|
|
|
|
|
showres (r, t) = either showFail show r ++ " " ++ showTrace t
|
2015-10-08 14:05:59 +03:00
|
|
|
|
|
|
|
rest = if moreThan (_failures res) 5 then "\n\t..." else ""
|
|
|
|
|
|
|
|
-- | Check if a list has more than some number of elements.
|
|
|
|
moreThan :: [a] -> Int -> Bool
|
|
|
|
moreThan [] n = n < 0
|
|
|
|
moreThan _ 0 = True
|
|
|
|
moreThan (_:xs) n = moreThan xs (n-1)
|
|
|
|
|
2016-02-09 21:37:50 +03:00
|
|
|
-- | Indent every line of a string.
|
|
|
|
indent :: String -> String
|
|
|
|
indent = intercalate "\n" . map ('\t':) . lines
|