Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and use a 'failure' helper instead of 'error'
- Loading branch information
Showing
3 changed files
with
18 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,22 @@ | ||
module Test.Util where | ||
|
||
import Cardano.Prelude | ||
import Cardano.Prelude hiding (callStack, throwIO) | ||
import Control.Monad.Class.MonadThrow (MonadThrow (throwIO)) | ||
import Control.Monad.Class.MonadTimer (DiffTime, MonadTimer, timeout) | ||
import Prelude (error) | ||
import Data.String (String) | ||
import GHC.Stack (callStack) | ||
import Test.HUnit.Lang (FailureReason (Reason), HUnitFailure (HUnitFailure)) | ||
|
||
failAfter :: (HasCallStack, MonadTimer m) => DiffTime -> m () -> m () | ||
failure :: (HasCallStack, MonadThrow m) => String -> m a | ||
failure msg = | ||
throwIO (HUnitFailure location $ Reason msg) | ||
where | ||
location = case reverse $ getCallStack callStack of | ||
(_, loc) : _ -> Just loc | ||
_ -> Nothing | ||
|
||
failAfter :: (HasCallStack, MonadTimer m, MonadThrow m) => DiffTime -> m () -> m () | ||
failAfter seconds action = | ||
timeout seconds action >>= \case | ||
-- TODO(SN): use MonadThrow instead? | ||
Nothing -> error $ "Test timed out after " <> show seconds <> " seconds" | ||
Nothing -> failure $ "Test timed out after " <> show seconds <> " seconds" | ||
Just _ -> pure () |