-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace force with strict fields and nothunk test
`force` didn't help. It might have made things worse. Testing in ghci with `:set +s` and `iterate (updateStats msg1) s0 !! 100000` we get: With force -> (0.42 secs, 297,818,512 bytes) No force -> (0.16 secs, 86,713,928 bytes) No force but with strict record fields -> (0.09 secs, 74,874,224 bytes) So this commit simply uses strict record fields, instead. And we also add nothunks tests as a sanity-check.
- Loading branch information
Showing
5 changed files
with
87 additions
and
17 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
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,19 +1,66 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# OPTIONS_GHC -fno-warn-orphans #-} | ||
module Cardano.Wallet.NetworkSpec | ||
( spec | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.Gen | ||
( genBlockHeader, genSlotNo ) | ||
import Cardano.Wallet.Network | ||
( ErrPostTx (..) ) | ||
( ErrPostTx (..), FollowLog (..), emptyStats, updateStats ) | ||
import Cardano.Wallet.Primitive.Types | ||
( BlockHeader (..), SlotNo (..) ) | ||
import Data.Time.Clock | ||
( getCurrentTime ) | ||
import NoThunks.Class | ||
( wNoThunks ) | ||
import Test.Hspec | ||
( Spec, describe, it ) | ||
( Spec, describe, expectationFailure, it ) | ||
|
||
import qualified Data.List.NonEmpty as NE | ||
import Test.QuickCheck | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "Pointless tests to cover 'Show' instances for errors" $ do | ||
testShow $ ErrPostTxBadRequest mempty | ||
testShow $ ErrPostTxProtocolFailure mempty | ||
|
||
describe "updateStats" $ do | ||
it "results in no unexpected thunks" $ property $ \(msg :: FollowLog ()) -> do | ||
-- This test is not /fully/ fool-proof. Adding lots of nested types to | ||
-- LogState and logic in updateStats not covered by the generator | ||
-- might cause us to miss a thunk. | ||
-- | ||
-- But it should be decent enough. | ||
t <- getCurrentTime | ||
let s0 = emptyStats t | ||
let s = updateStats msg s0 | ||
wNoThunks [] s >>= \case | ||
Nothing -> return () | ||
Just x -> expectationFailure $ show x | ||
|
||
|
||
instance Arbitrary (FollowLog msg) where | ||
arbitrary = oneof | ||
[ MsgApplyBlocks | ||
<$> arbitrary | ||
<*> ((NE.fromList . getNonEmpty) <$> arbitrary) | ||
, MsgDidRollback | ||
<$> genSlotNo | ||
<*> genSlotNo | ||
, MsgFollowerTip . Just | ||
<$> genBlockHeader (SlotNo 3) | ||
, pure $ MsgFollowerTip Nothing | ||
, pure MsgHaltMonitoring | ||
] | ||
-- Shrinking not that important here | ||
|
||
instance Arbitrary BlockHeader where | ||
arbitrary = genBlockHeader =<< genSlotNo | ||
|
||
testShow :: Show a => a -> Spec | ||
testShow a = it (show a) True |