Skip to content

Commit

Permalink
consensus: change onEachTick to tickWatcher
Browse files Browse the repository at this point in the history
This commit is propagating the new Watcher abstraction through an abbreviation.
  • Loading branch information
nfrisby committed Jan 26, 2021
1 parent 35a9664 commit 674cee0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
28 changes: 11 additions & 17 deletions ouroboros-consensus-test/src/Test/Util/LogicalClock.hs
Expand Up @@ -16,7 +16,7 @@ module Test.Util.LogicalClock (
, new
, sufficientTimeFor
-- * Scheduling actions
, onEachTick
, tickWatcher
, onTick
, blockUntilTick
) where
Expand Down Expand Up @@ -83,22 +83,16 @@ tickDelay = 0.5
-------------------------------------------------------------------------------}

-- | Execute action on every clock tick
--
-- Returns a handle to cancel the thread.
onEachTick :: (IOLike m, HasCallStack)
=> ResourceRegistry m
-> LogicalClock m
-> String
-> (Tick -> m ())
-> m (m ())
onEachTick registry clock threadLabel action =
cancelThread <$>
forkLinkedWatcher registry threadLabel Watcher {
wFingerprint = id
, wInitial = Nothing
, wNotify = action
, wReader = getCurrentTick clock
}
tickWatcher :: LogicalClock m
-> (Tick -> m ())
-> Watcher m Tick Tick
tickWatcher clock action =
Watcher {
wFingerprint = id
, wInitial = Nothing
, wNotify = action
, wReader = getCurrentTick clock
}

-- | Execute action once at the specified tick
onTick :: (IOLike m, HasCallStack)
Expand Down
Expand Up @@ -62,7 +62,7 @@ import Ouroboros.Consensus.Util.Condense
import Ouroboros.Consensus.Util.IOLike
import Ouroboros.Consensus.Util.ResourceRegistry
import Ouroboros.Consensus.Util.STM (Fingerprint (..),
WithFingerprint (..))
WithFingerprint (..), forkLinkedWatcher)

import Test.Util.LogicalClock (LogicalClock, NumTicks (..), Tick (..))
import qualified Test.Util.LogicalClock as LogicalClock
Expand Down Expand Up @@ -305,7 +305,12 @@ runChainSync securityParam (ClientUpdates clientUpdates)

-- Schedule updates of the client and server chains
varLastUpdate <- uncheckedNewTVarM 0
void $ LogicalClock.onEachTick registry clock "scheduled updates" $ \tick -> do
let forkLinkedTickWatcher :: (Tick -> m ()) -> m ()
forkLinkedTickWatcher =
void
. forkLinkedWatcher registry "scheduled updates"
. LogicalClock.tickWatcher clock
forkLinkedTickWatcher $ \tick -> do
-- Stop updating the client and server chains when the chain sync client
-- has thrown an exception or has gracefully terminated, so that at the
-- end, we can read the chains in the states they were in when the
Expand Down

0 comments on commit 674cee0

Please sign in to comment.