-
Notifications
You must be signed in to change notification settings - Fork 86
/
Test.hs
169 lines (138 loc) · 5.38 KB
/
Test.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Ouroboros.Network.Protocol.KeepAlive.Test where
import Control.Monad.ST (runST)
import Control.Monad.Class.MonadST
import Control.Monad.Class.MonadSTM
import Control.Monad.Class.MonadAsync
import Control.Monad.Class.MonadThrow
import Control.Monad.IOSim (runSimOrThrow)
import Control.Tracer (nullTracer)
import qualified Codec.CBOR.Read as CBOR
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as BL
import Network.TypedProtocol.Core
import Network.TypedProtocol.Proofs
import Ouroboros.Network.Channel
import Ouroboros.Network.Codec hiding (prop_codec)
import Ouroboros.Network.Driver.Limits
import Ouroboros.Network.Driver.Simple (runConnectedPeers)
import Ouroboros.Network.Protocol.KeepAlive.Type
import Ouroboros.Network.Protocol.KeepAlive.Client
import Ouroboros.Network.Protocol.KeepAlive.Server
import Ouroboros.Network.Protocol.KeepAlive.Codec
import Ouroboros.Network.Protocol.KeepAlive.Examples
import Ouroboros.Network.Protocol.KeepAlive.Direct
import Test.Ouroboros.Network.Testing.Utils
( splits2
, splits3
)
import Test.QuickCheck
import Text.Show.Functions ()
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.QuickCheck (testProperty)
--
-- The list of all properties
--
tests :: TestTree
tests = testGroup "Ouroboros.Network.Protocol.KeepAlive"
[ testProperty "direct" prop_direct
, testProperty "connect" prop_connect
, testProperty "channel ST" prop_channel_ST
, testProperty "channel IO" prop_channel_IO
, testProperty "codec" prop_codec
, testProperty "codec 2-splits" prop_codec_splits2
, testProperty "codec 3-splits" (withMaxSuccess 33 prop_codec_splits3)
, testProperty "byteLimits" prop_byteLimits
]
--
-- Properties going directly, not via Peer.
--
prop_direct :: (Int -> Int) -> NonNegative Int -> Property
prop_direct f (NonNegative n) =
runSimOrThrow
(direct
keepAliveServerCount
(keepAliveClientApply f 0 n))
===
(n, foldr (.) id (replicate n f) 0)
--
-- Properties using connect
--
prop_connect :: (Int -> Int)
-> NonNegative Int
-> Bool
prop_connect f (NonNegative n) =
case runSimOrThrow
(connect
(keepAliveServerPeer keepAliveServerCount)
(keepAliveClientPeer $ keepAliveClientApply f 0 n))
of (s, c, TerminalStates TokDone TokDone) ->
(s, c) == (n, foldr (.) id (replicate n f) 0)
--
-- Properties using channels, codecs and drivers.
--
prop_channel :: ( MonadST m
, MonadSTM m
, MonadAsync m
, MonadCatch m
)
=> (Int -> Int)
-> Int
-> m Property
prop_channel f n = do
(s, c) <- runConnectedPeers createConnectedChannels
nullTracer
codecKeepAlive
server client
return ((s, c) === (n, foldr (.) id (replicate n f) 0))
where
server = keepAliveServerPeer keepAliveServerCount
client = keepAliveClientPeer (keepAliveClientApply f 0 n)
prop_channel_ST :: (Int -> Int)
-> NonNegative Int
-> Property
prop_channel_ST f (NonNegative n) =
runSimOrThrow (prop_channel f n)
prop_channel_IO :: (Int -> Int)
-> NonNegative Int
-> Property
prop_channel_IO f (NonNegative n) =
ioProperty (prop_channel f n)
--
-- Codec tests
--
instance Arbitrary (AnyMessageAndAgency KeepAlive) where
arbitrary = do
c <- arbitrary
oneof
[ pure $ AnyMessageAndAgency (ClientAgency TokClient) (MsgKeepAlive $ Cookie c)
, pure $ AnyMessageAndAgency (ServerAgency TokServer) (MsgKeepAliveResponse $ Cookie c)
, pure $ AnyMessageAndAgency (ClientAgency TokClient) MsgDone
]
instance Show (AnyMessageAndAgency KeepAlive) where
show (AnyMessageAndAgency _ msg) = show msg
instance Eq (AnyMessage KeepAlive) where
AnyMessage (MsgKeepAlive cookieA) == AnyMessage (MsgKeepAlive cookieB) = cookieA == cookieB
AnyMessage (MsgKeepAliveResponse cookieA) == AnyMessage (MsgKeepAliveResponse cookieB) = cookieA == cookieB
AnyMessage MsgDone == AnyMessage MsgDone = True
_ == _ = False
prop_codec :: AnyMessageAndAgency KeepAlive -> Bool
prop_codec msg =
runST (prop_codecM codecKeepAlive msg)
prop_codec_splits2 :: AnyMessageAndAgency KeepAlive -> Bool
prop_codec_splits2 msg =
runST (prop_codec_splitsM splits2 codecKeepAlive msg)
prop_codec_splits3 :: AnyMessageAndAgency KeepAlive -> Bool
prop_codec_splits3 msg =
runST (prop_codec_splitsM splits3 codecKeepAlive msg)
prop_byteLimits :: AnyMessageAndAgency KeepAlive
-> Bool
prop_byteLimits (AnyMessageAndAgency agency msg) =
dataSize (encode agency msg)
<= sizeLimitForState agency
where
Codec { encode } = (codecKeepAlive :: Codec KeepAlive CBOR.DeserialiseFailure IO ByteString)
ProtocolSizeLimits { sizeLimitForState, dataSize } = byteLimitsKeepAlive (fromIntegral . BL.length)