Skip to content

Commit

Permalink
Improve counterexamples for +prop_governor_connstatus
Browse files Browse the repository at this point in the history
When the property fails, to aid debugging, show the recent events as a
counter example, and the differing peer status maps.

And rename some variables for clarity.
  • Loading branch information
dcoutts authored and bolt12 committed May 12, 2021
1 parent 3c73575 commit 13ff449
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions ouroboros-network/test/Test/Ouroboros/Network/PeerSelection.hs
Expand Up @@ -538,36 +538,37 @@ prop_governor_gossip_1hr env@GovernorMockEnvironment {
-- | Check the governor's view of connection status does not lag behind reality
-- by too much.
--
prop_governor_connstatus :: GovernorMockEnvironment -> Bool
prop_governor_connstatus :: GovernorMockEnvironment -> Property
prop_governor_connstatus env =
let trace = takeFirstNHours 1
. selectPeerSelectionTraceEvents $
runGovernorInMockEnvironment env
--TODO: check any actually get a true status output and try some deliberate bugs
in all ok (groupBy ((==) `on` fst) trace)
in conjoin (map ok (groupBy ((==) `on` fst) trace))
where
-- We look at events when the environment's view of the state of all the
-- peer connections changed, and check that before simulated time advances
-- the governor's view of the same state was brought in sync.
--
-- We do that by finding the env events and then looking for the last
-- governor state event before time moves on.
ok :: [(Time, TestTraceEvent)] -> Bool
ok :: [(Time, TestTraceEvent)] -> Property
ok trace =
case (lastTrueStatus, lastTestStatus) of
(Nothing, _) -> True
(Just trueStatus, Just testStatus) -> trueStatus == testStatus
(Just trueStatus, Nothing) -> trueStatus == Map.empty
counterexample ("last few events:\n" ++ (unlines . map show) trace) $
case (lastEnvStatus, lastGovStatus) of
(Nothing, _) -> property True
(Just envStatus, Just govStatus) -> envStatus === govStatus
(Just envStatus, Nothing) -> envStatus === Map.empty
where
lastTrueStatus =
lastEnvStatus =
listToMaybe
[ Map.filter (not . isPeerCold) status
| (_, MockEnvEvent (TraceEnvPeersStatus status)) <- reverse trace ]

isPeerCold PeerCold = True
isPeerCold _ = False

lastTestStatus =
lastGovStatus =
listToMaybe
[ Governor.establishedPeersStatus st
| (_, GovernorDebug (TraceGovernorState _ _ st)) <- reverse trace ]
Expand Down

0 comments on commit 13ff449

Please sign in to comment.