Skip to content

Commit

Permalink
Fix quickcheck state machine tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jasagredo committed Oct 25, 2021
1 parent f79d430 commit 35e19ce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ type TestFollower m blk = WithEq (Follower m blk (AllComponents blk))
data ChainDBState m blk = ChainDBState
{ chainDB :: ChainDB m blk
, internal :: ChainDB.Internal m blk
, addBlockAsync :: Async m Void
, addBlockAsync :: Thread m Void
-- ^ Background thread that adds blocks to the ChainDB
}
deriving NoThunks via AllowThunk (ChainDBState m blk)
Expand All @@ -319,8 +319,7 @@ open
=> ChainDbArgs Identity m blk -> m (ChainDBState m blk)
open args = do
(chainDB, internal) <- openDBInternal args False
addBlockAsync <- async (intAddBlockRunner internal)
link addBlockAsync
addBlockAsync <- forkThreadAndLink (cdbRegistry args) "Add block runner" (intAddBlockRunner internal)
return ChainDBState { chainDB, internal, addBlockAsync }

-- PRECONDITION: the ChainDB is closed
Expand All @@ -333,7 +332,7 @@ reopen ChainDBEnv { varDB, args } = do

close :: IOLike m => ChainDBState m blk -> m ()
close ChainDBState { chainDB, addBlockAsync } = do
cancel addBlockAsync
cancelThread addBlockAsync
closeDB chainDB

run :: forall m blk.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module Ouroboros.Consensus.Util.ResourceRegistry (
-- * Combinators primarily for testing
, closeRegistry
, countResources
, forkThreadAndLink
, unsafeNewRegistry
-- * opaque
, ResourceRegistry
Expand Down Expand Up @@ -1132,6 +1133,23 @@ forkThread rr label body = snd <$>
removeThread tid
void $ removeResource rid

-- | Convenience function to group actions done in ChainDB QuickCheck state
-- machine tests.
--
-- It is just a combination of 'forkLinkedThread' together with linking the
-- exceptions thrown in the async thread to the main one.
forkThreadAndLink :: forall m a. (IOLike m, HasCallStack)
=> ResourceRegistry m
-> String -- ^ Label for the thread
-> m a
-> m (Thread m a)
forkThreadAndLink rr label body = do
t <- forkThread rr label body
link $ threadAsync t
linkToRegistry t
return t


-- | Bracketed version of 'forkThread'
--
-- The analogue of 'withAsync' for the registry.
Expand Down

0 comments on commit 35e19ce

Please sign in to comment.