Skip to content

Commit 2df2eba

Browse files
committed
Add and use a 'failure' helper instead of 'error'
1 parent 74df97b commit 2df2eba

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

hydra-node/hydra-node.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ test-suite tests
187187
, QuickCheck
188188
, serialise
189189
, typed-protocols-examples
190+
, HUnit
190191
build-tool-depends:
191192
hspec-discover:hspec-discover
192193
ghc-options:

hydra-node/test/Hydra/BehaviorSpec.hs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ import Test.Hspec (
5555
shouldNotBe,
5656
shouldReturn,
5757
)
58-
import Test.Util (failAfter)
59-
import Prelude (error)
58+
import Test.Util (failAfter, failure)
6059

6160
spec :: Spec
6261
spec = describe "Behavior of one ore more hydra-nodes" $ do
@@ -299,8 +298,7 @@ startHydraNode' snapshotStrategy nodeId connectToChain = do
299298
atomically $ do
300299
st' <- queryLedgerState node
301300
check (st == st')
302-
-- TODO(SN): use MonadThrow instead?
303-
when (isNothing result) $ error ("Expected ledger state of node " <> show nodeId <> " to be " <> show st)
301+
when (isNothing result) $ failure ("Expected ledger state of node " <> show nodeId <> " to be " <> show st)
304302
, nodeId
305303
, capturedLogs
306304
}

hydra-node/test/Test/Util.hs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
module Test.Util where
22

3-
import Cardano.Prelude
3+
import Cardano.Prelude hiding (callStack, throwIO)
4+
import Control.Monad.Class.MonadThrow (MonadThrow (throwIO))
45
import Control.Monad.Class.MonadTimer (DiffTime, MonadTimer, timeout)
5-
import Prelude (error)
6+
import Data.String (String)
7+
import GHC.Stack (callStack)
8+
import Test.HUnit.Lang (FailureReason (Reason), HUnitFailure (HUnitFailure))
69

7-
failAfter :: (HasCallStack, MonadTimer m) => DiffTime -> m () -> m ()
10+
failure :: (HasCallStack, MonadThrow m) => String -> m a
11+
failure msg =
12+
throwIO (HUnitFailure location $ Reason msg)
13+
where
14+
location = case reverse $ getCallStack callStack of
15+
(_, loc) : _ -> Just loc
16+
_ -> Nothing
17+
18+
failAfter :: (HasCallStack, MonadTimer m, MonadThrow m) => DiffTime -> m () -> m ()
819
failAfter seconds action =
920
timeout seconds action >>= \case
10-
-- TODO(SN): use MonadThrow instead?
11-
Nothing -> error $ "Test timed out after " <> show seconds <> " seconds"
21+
Nothing -> failure $ "Test timed out after " <> show seconds <> " seconds"
1222
Just _ -> pure ()

0 commit comments

Comments
 (0)