Skip to content

Commit

Permalink
Use implicit fixtures, refactor poll and send, and add a new inva…
Browse files Browse the repository at this point in the history
…lid tx test

Implicit fixtures, i.e. using ImplicitParams to plumb test fixtures
around, may make the plumbing a bit easier.

Renamed polling and sending to `poll` and `send`. Now also they deal
in more useful types. Request keys are no longer returned by `send`,
because they can be computed by the caller.

`send` and `poll` now also include assertions which are always useful
to test.
  • Loading branch information
edmundnoble committed Dec 20, 2024
1 parent 0f68569 commit 5ad8117
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 158 deletions.
1 change: 1 addition & 0 deletions chainweb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ test-suite chainweb-tests
, pact-tng:pact-request-api
, pact-tng:test-utils
, patience >= 0.3
, prettyprinter
, property-matchers ^>= 0.2
, pretty-show
, quickcheck-instances >= 0.3
Expand Down
35 changes: 26 additions & 9 deletions test/unit/Chainweb/Test/Pact5/CutFixture.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{-# language
BangPatterns
, ConstraintKinds
, DataKinds
, DeriveAnyClass
, DerivingStrategies
, FlexibleContexts
, ImplicitParams
, ImportQualifiedPost
, LambdaCase
, NumericUnderscores
Expand All @@ -15,14 +17,18 @@
, TemplateHaskell
, TypeApplications
#-}
{-# LANGUAGE MultiParamTypeClasses #-}

-- | A fixture which provides access to the internals of a running node, with
-- multiple chains. Usually, you initialize it with `mkFixture`, insert
-- transactions into the mempool as desired, and use `advanceAllChains` to
-- trigger mining on all chains at once.
module Chainweb.Test.Pact5.CutFixture
( Fixture(..)
, HasFixture(..)
, mkFixture
, withFixture
, withFixture'
, fixtureCutDb
, fixturePayloadDb
, fixtureWebBlockHeaderDb
Expand Down Expand Up @@ -83,6 +89,7 @@ import GHC.Stack
import Network.HTTP.Client qualified as HTTP
import Pact.Core.Command.Types
import Pact.Core.Hash qualified as Pact5
import GHC.Exts (WithDict(..))

data Fixture = Fixture
{ _fixtureCutDb :: CutDb RocksDbTable
Expand All @@ -94,6 +101,9 @@ data Fixture = Fixture
}
makeLenses ''Fixture

class HasFixture where
cutFixture :: IO Fixture

mkFixture :: ChainwebVersion -> PactServiceConfig -> RocksDb -> ResourceT IO Fixture
mkFixture v pactServiceConfig baseRdb = do
logger <- liftIO getTestLogger
Expand All @@ -115,17 +125,25 @@ mkFixture v pactServiceConfig baseRdb = do
, _fixtureMempools = OnChains $ fst <$> perChain
, _fixturePactQueues = OnChains $ snd <$> perChain
}
_ <- liftIO $ advanceAllChains fixture
_ <- withFixture fixture $ liftIO advanceAllChains
return fixture

withFixture' :: IO Fixture -> (HasFixture => a) -> a
withFixture' fixture tests =
withDict @HasFixture fixture tests

withFixture :: Fixture -> (HasFixture => a) -> a
withFixture fixture tests =
withFixture' (return fixture) tests

-- | Advance all chains by one block, filling that block with whatever is in
-- their mempools at the time.
--
advanceAllChains
:: HasCallStack
=> Fixture
-> IO (Cut, ChainMap (Vector (CommandResult Pact5.Hash Text)))
advanceAllChains Fixture{..} = do
:: (HasCallStack, HasFixture)
=> IO (Cut, ChainMap (Vector (CommandResult Pact5.Hash Text)))
advanceAllChains = do
Fixture{..} <- cutFixture
let v = _chainwebVersion _fixtureCutDb
latestCut <- liftIO $ _fixtureCutDb ^. cut
let blockHeights = fmap (view blockHeight) $ latestCut ^. cutMap
Expand All @@ -149,10 +167,9 @@ advanceAllChains Fixture{..} = do
return (finalCut, onChains perChainCommandResults)

advanceAllChains_
:: HasCallStack
=> Fixture
-> IO ()
advanceAllChains_ f = void $ advanceAllChains f
:: (HasCallStack, HasFixture)
=> IO ()
advanceAllChains_ = void advanceAllChains

withTestCutDb :: (Logger logger)
=> logger
Expand Down
Loading

0 comments on commit 5ad8117

Please sign in to comment.