Skip to content

Commit d53d14e

Browse files
Niolsneilmayhew
authored andcommitted
Fix tests that relied on default timeouts
1 parent 0b23940 commit d53d14e

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/PeerSimulator/Tests/LinkedThreads.hs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ module Test.Consensus.PeerSimulator.Tests.LinkedThreads (tests) where
99
import Control.Monad.Class.MonadAsync (AsyncCancelled (..))
1010
import Control.Monad.Class.MonadTime.SI (Time (Time))
1111
import Data.Functor (($>))
12-
import Data.Maybe (fromJust)
13-
import Ouroboros.Consensus.Util.IOLike (DiffTime, fromException)
12+
import Ouroboros.Consensus.Util.IOLike (fromException)
1413
import qualified Ouroboros.Network.AnchoredFragment as AF
1514
import Ouroboros.Network.Driver.Limits
1615
(ProtocolLimitFailure (ExceededTimeLimit))
1716
import Ouroboros.Network.Protocol.ChainSync.Codec (mustReplyTimeout)
1817
import Test.Consensus.BlockTree (BlockTree (..))
1918
import Test.Consensus.Genesis.Setup
20-
import Test.Consensus.PeerSimulator.Run (defaultSchedulerConfig)
19+
import Test.Consensus.PeerSimulator.Run
20+
(SchedulerConfig (scEnableChainSyncTimeouts),
21+
defaultSchedulerConfig)
2122
import Test.Consensus.PeerSimulator.StateView
2223
import Test.Consensus.PointSchedule
2324
import Test.Consensus.PointSchedule.Peers (peersOnlyHonest)
@@ -39,13 +40,15 @@ tests = testProperty "ChainSync kills BlockFetch" prop_chainSyncKillsBlockFetch
3940
prop_chainSyncKillsBlockFetch :: Property
4041
prop_chainSyncKillsBlockFetch = do
4142
forAllGenesisTest
42-
(do gt@GenesisTest{gtChainSyncTimeouts} <- genChains (pure 0)
43-
let schedule = dullSchedule gt (fromJust $ mustReplyTimeout gtChainSyncTimeouts)
44-
pure $ gt $> schedule
43+
(do gt@GenesisTest{gtBlockTree} <- genChains (pure 0)
44+
pure $ enableMustReplyTimeout $ gt $> dullSchedule (btTrunk gtBlockTree)
4545
)
46-
defaultSchedulerConfig
46+
47+
defaultSchedulerConfig {scEnableChainSyncTimeouts = True}
48+
4749
-- No shrinking because the schedule is tiny and hand-crafted
4850
(\_ _ -> [])
51+
4952
( \_ stateView@StateView {svTipBlock} ->
5053
svTipBlock == Nothing
5154
&& case exceptionsByComponent ChainSyncClient stateView of
@@ -62,9 +65,11 @@ prop_chainSyncKillsBlockFetch = do
6265
_ -> False
6366
)
6467
where
65-
dullSchedule :: GenesisTest blk () -> DiffTime -> PointSchedule blk
66-
dullSchedule GenesisTest {gtBlockTree} timeout =
67-
let (firstBlock, secondBlock) = case AF.toOldestFirst $ btTrunk gtBlockTree of
68+
timeout = 10
69+
70+
dullSchedule :: AF.AnchoredFragment blk -> PointSchedule blk
71+
dullSchedule trunk =
72+
let (firstBlock, secondBlock) = case AF.toOldestFirst trunk of
6873
b1 : b2 : _ -> (b1, b2)
6974
_ -> error "block tree must have two blocks"
7075
psSchedule = peersOnlyHonest $
@@ -73,3 +78,6 @@ prop_chainSyncKillsBlockFetch = do
7378
]
7479
psMinEndTime = Time $ timeout + 1
7580
in PointSchedule {psSchedule, psStartOrder = [], psMinEndTime}
81+
82+
enableMustReplyTimeout :: GenesisTest blk schedule -> GenesisTest blk schedule
83+
enableMustReplyTimeout gt = gt { gtChainSyncTimeouts = (gtChainSyncTimeouts gt) { mustReplyTimeout = Just timeout } }

ouroboros-consensus-diffusion/test/consensus-test/Test/Consensus/PeerSimulator/Tests/Timeouts.hs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
module Test.Consensus.PeerSimulator.Tests.Timeouts (tests) where
66

77
import Data.Functor (($>))
8-
import Data.Maybe (fromJust)
98
import Ouroboros.Consensus.Util.Condense
109
import Ouroboros.Consensus.Util.IOLike (DiffTime, Time (Time),
1110
fromException)
@@ -15,7 +14,9 @@ import Ouroboros.Network.Driver.Limits
1514
import Ouroboros.Network.Protocol.ChainSync.Codec (mustReplyTimeout)
1615
import Test.Consensus.BlockTree (btTrunk)
1716
import Test.Consensus.Genesis.Setup
18-
import Test.Consensus.PeerSimulator.Run (defaultSchedulerConfig)
17+
import Test.Consensus.PeerSimulator.Run
18+
(SchedulerConfig (scEnableChainSyncTimeouts),
19+
defaultSchedulerConfig)
1920
import Test.Consensus.PeerSimulator.StateView
2021
import Test.Consensus.PointSchedule
2122
import Test.Consensus.PointSchedule.Peers (peersOnlyAdversary,
@@ -38,12 +39,11 @@ prop_timeouts :: Bool -> Property
3839
prop_timeouts mustTimeout = do
3940
forAllGenesisTest
4041

41-
(do gt@GenesisTest{gtChainSyncTimeouts, gtBlockTree} <- genChains (pure 0)
42-
let schedule = dullSchedule (fromJust $ mustReplyTimeout gtChainSyncTimeouts) (btTrunk gtBlockTree)
43-
pure $ gt $> schedule
42+
(do gt@GenesisTest{gtBlockTree} <- genChains (pure 0)
43+
pure $ enableMustReplyTimeout $ gt $> dullSchedule (btTrunk gtBlockTree)
4444
)
45-
-- Timeouts are enabled by default
46-
defaultSchedulerConfig
45+
46+
defaultSchedulerConfig {scEnableChainSyncTimeouts = True}
4747

4848
-- Here we can't shrink because we exploit the properties of the point schedule to wait
4949
-- at the end of the test for the adversaries to get disconnected, by adding an extra point.
@@ -60,9 +60,11 @@ prop_timeouts mustTimeout = do
6060
)
6161

6262
where
63-
dullSchedule :: AF.HasHeader blk => DiffTime -> AF.AnchoredFragment blk -> PointSchedule blk
64-
dullSchedule _ (AF.Empty _) = error "requires a non-empty block tree"
65-
dullSchedule timeout (_ AF.:> tipBlock) =
63+
timeout = 10
64+
65+
dullSchedule :: AF.HasHeader blk => AF.AnchoredFragment blk -> PointSchedule blk
66+
dullSchedule (AF.Empty _) = error "requires a non-empty block tree"
67+
dullSchedule (_ AF.:> tipBlock) =
6668
let offset :: DiffTime = if mustTimeout then 1 else -1
6769
psSchedule = (if mustTimeout then peersOnlyAdversary else peersOnlyHonest) $ [
6870
(Time 0, scheduleTipPoint tipBlock),
@@ -72,3 +74,6 @@ prop_timeouts mustTimeout = do
7274
-- This keeps the test running long enough to pass the timeout by 'offset'.
7375
psMinEndTime = Time $ timeout + offset
7476
in PointSchedule {psSchedule, psStartOrder = [], psMinEndTime}
77+
78+
enableMustReplyTimeout :: GenesisTest blk schedule -> GenesisTest blk schedule
79+
enableMustReplyTimeout gt = gt { gtChainSyncTimeouts = (gtChainSyncTimeouts gt) { mustReplyTimeout = Just timeout } }

0 commit comments

Comments
 (0)