|
| 1 | +{-# OPTIONS_GHC -Wno-orphans #-} |
| 2 | + |
| 3 | +module Cardano.Network.Protocol.ChainSync.Codec.TimeLimits.Test where |
| 4 | + |
| 5 | +import Control.Monad.Class.MonadTime.SI ( DiffTime ) |
| 6 | +import System.Random (mkStdGen) |
| 7 | + |
| 8 | +import Cardano.Network.PeerSelection.PeerTrustable (PeerTrustable (..)) |
| 9 | +import Cardano.Network.Protocol.ChainSync.Codec.TimeLimits (timeLimitsChainSync) |
| 10 | + |
| 11 | +import Ouroboros.Network.Protocol.ChainSync.Codec (ChainSyncIdleTimeout (..), |
| 12 | + maxChainSyncTimeout, minChainSyncTimeout) |
| 13 | +import Ouroboros.Network.Protocol.ChainSync.Type (SingChainSync (..), |
| 14 | + SingNextKind (..)) |
| 15 | +import Ouroboros.Network.Protocol.Limits (ProtocolTimeLimitsWithRnd (..), |
| 16 | + shortWait) |
| 17 | + |
| 18 | +import Test.QuickCheck (Arbitrary (..), Property, oneof, property, elements) |
| 19 | +import Test.Tasty (TestTree, testGroup) |
| 20 | +import Test.Tasty.QuickCheck (testProperty, (===)) |
| 21 | + |
| 22 | + |
| 23 | +tests :: TestTree |
| 24 | +tests = |
| 25 | + testGroup "Ouroboros.Network.Protocol" |
| 26 | + [ testGroup "ChainSync" |
| 27 | + [ testGroup "TimeLimits" |
| 28 | + [ testProperty "timeout in 'StIntersect'" |
| 29 | + prop_short_wait_timeout_in_intersect |
| 30 | + , testProperty "timeout in 'StNext' 'StCanWait'" |
| 31 | + prop_short_wait_timeout_in_canwait |
| 32 | + , testProperty "timeout in 'StIdle' state" |
| 33 | + prop_timeout_in_idle |
| 34 | + , testProperty "timeout range for non-trustable peers in 'StNext' 'StMustReply' state" |
| 35 | + prop_timeout_range_for_not_trustable_in_mustreply |
| 36 | + , testProperty "no timeout for trustable peers in 'StNext' 'StMustReply' state" |
| 37 | + prop_no_timeout_for_trustable_peers_in_mustreply |
| 38 | + ] |
| 39 | + ] |
| 40 | + ] |
| 41 | + |
| 42 | +-- | For state 'StIntersect', the timeout is always 'shortWait' |
| 43 | +prop_short_wait_timeout_in_intersect |
| 44 | + :: PeerTrustable -> ChainSyncIdleTimeout -> Int -> Property |
| 45 | +prop_short_wait_timeout_in_intersect peerTrustable idleTimeout seed = |
| 46 | + timeout === shortWait |
| 47 | + where |
| 48 | + limits = timeLimitsChainSync idleTimeout peerTrustable |
| 49 | + (timeout, _) = |
| 50 | + timeLimitForStateWithRnd limits SingIntersect (mkStdGen seed) |
| 51 | + |
| 52 | +-- | For state 'StNext' 'StCanAwait', the timeout is always 'shortWait' |
| 53 | +prop_short_wait_timeout_in_canwait |
| 54 | + :: PeerTrustable -> ChainSyncIdleTimeout -> Int -> Property |
| 55 | +prop_short_wait_timeout_in_canwait peerTrustable idleTimeout seed = |
| 56 | + timeout === shortWait |
| 57 | + where |
| 58 | + limits = timeLimitsChainSync idleTimeout peerTrustable |
| 59 | + (timeout, _) = |
| 60 | + timeLimitForStateWithRnd limits (SingNext SingCanAwait) (mkStdGen seed) |
| 61 | + |
| 62 | +-- | For state 'StIdle', the timeout is 'ChainSyncIdleTimeout' |
| 63 | +prop_timeout_in_idle |
| 64 | + :: PeerTrustable -> ChainSyncIdleTimeout -> Int -> Property |
| 65 | +prop_timeout_in_idle peerTrustable idleTimeout seed = |
| 66 | + timeout === timeout' |
| 67 | + where |
| 68 | + timeout' = case idleTimeout of |
| 69 | + ChainSyncNoIdleTimeout -> Nothing |
| 70 | + ChainSyncIdleTimeout t -> Just t |
| 71 | + limits = timeLimitsChainSync idleTimeout peerTrustable |
| 72 | + (timeout, _) = timeLimitForStateWithRnd limits SingIdle (mkStdGen seed) |
| 73 | + |
| 74 | +-- | For non-trustable peers and in 'StNext' 'StMustReply' state, the timeout is |
| 75 | +-- always within the specified range |
| 76 | +prop_timeout_range_for_not_trustable_in_mustreply |
| 77 | + :: ChainSyncIdleTimeout -> Int -> Property |
| 78 | +prop_timeout_range_for_not_trustable_in_mustreply idleTimeout seed = |
| 79 | + property $ |
| 80 | + maybe |
| 81 | + False |
| 82 | + (\t -> t >= minChainSyncTimeout && t <= maxChainSyncTimeout) |
| 83 | + timeout |
| 84 | + where |
| 85 | + limits = timeLimitsChainSync idleTimeout IsNotTrustable |
| 86 | + (timeout, _) = |
| 87 | + timeLimitForStateWithRnd limits (SingNext SingMustReply) (mkStdGen seed) |
| 88 | + |
| 89 | +-- | For trustable peers, there's never a timeout in 'StNext' 'StMustReply' |
| 90 | +-- state |
| 91 | +prop_no_timeout_for_trustable_peers_in_mustreply |
| 92 | + :: ChainSyncIdleTimeout -> Int -> Property |
| 93 | +prop_no_timeout_for_trustable_peers_in_mustreply idleTimeout seed = |
| 94 | + timeout === Nothing |
| 95 | + where |
| 96 | + limits = timeLimitsChainSync idleTimeout IsTrustable |
| 97 | + (timeout, _) = |
| 98 | + timeLimitForStateWithRnd limits (SingNext SingMustReply) (mkStdGen seed) |
| 99 | + |
| 100 | + |
| 101 | +-- TODO Duplicated code: move ouroboros-network-tests-lib OrphanInstances |
| 102 | + |
| 103 | +instance Arbitrary PeerTrustable where |
| 104 | + arbitrary = elements [IsTrustable, IsNotTrustable] |
| 105 | + |
| 106 | +instance Arbitrary ChainSyncIdleTimeout where |
| 107 | + arbitrary = oneof |
| 108 | + [ pure ChainSyncNoIdleTimeout |
| 109 | + , ChainSyncIdleTimeout <$> arbitrary |
| 110 | + ] |
| 111 | + |
| 112 | +instance Arbitrary DiffTime where |
| 113 | + arbitrary = fromRational <$> arbitrary |
| 114 | + |
| 115 | +instance Show ChainSyncIdleTimeout where |
| 116 | + show ChainSyncNoIdleTimeout = "ChainSyncNoIdleTimeout" |
| 117 | + show (ChainSyncIdleTimeout t) = "ChainSyncIdleTimeout " ++ show t |
0 commit comments