Skip to content

Commit

Permalink
Add Sim runSimOrThrow util
Browse files Browse the repository at this point in the history
Very useful for tests where we don't expect failure and it's fine to
throw the exception in the context of a test.
  • Loading branch information
dcoutts committed Feb 17, 2019
1 parent a53d3c0 commit a36d4a4
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion io-sim/src/Control/Monad/IOSim.hs
Expand Up @@ -13,6 +13,7 @@
module Control.Monad.IOSim (
SimM,
runSim,
runSimOrThrow,
runSimStrictShutdown,
Failure(..),
runSimTrace,
Expand All @@ -38,7 +39,8 @@ import qualified Data.Map.Strict as Map
import qualified Data.Set as Set

import Control.Exception
( Exception(..), SomeException, ErrorCall(..), assert )
( Exception(..), SomeException
, ErrorCall(..), throw, assert )
import qualified System.IO.Error as IO.Error (userError)

import Control.Monad
Expand Down Expand Up @@ -379,9 +381,20 @@ data Failure =
| FailureSloppyShutdown
deriving Show

instance Exception Failure

runSim :: forall a. (forall s. SimM s a) -> Either Failure a
runSim mainAction = traceResult False (runSimTrace mainAction)

-- | For quick experiments and tests it is often appropriate and convenient to
-- simply throw failures as exceptions.
--
runSimOrThrow :: forall a. (forall s. SimM s a) -> a
runSimOrThrow mainAction =
case runSim mainAction of
Left e -> throw e
Right x -> x

-- | Like 'runSim' but also fail if when the main thread terminates, there
-- are other threads still running or blocked. If one is trying to follow
-- a strict thread cleanup policy then this helps testing for that.
Expand Down

0 comments on commit a36d4a4

Please sign in to comment.