1
1
{-# LANGUAGE DerivingVia #-}
2
2
{-# LANGUAGE FlexibleContexts #-}
3
+ {-# LANGUAGE FlexibleInstances #-}
3
4
{-# LANGUAGE FunctionalDependencies #-}
4
5
{-# LANGUAGE GADTs #-}
5
6
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6
7
{-# LANGUAGE KindSignatures #-}
7
8
{-# LANGUAGE RankNTypes #-}
8
9
{-# LANGUAGE ScopedTypeVariables #-}
9
- {-# LANGUAGE FlexibleInstances #-}
10
10
11
11
-- | Smoke tests for the object diffusion protocol. This uses a trivial object
12
12
-- pool and checks that a few objects can indeed be transferred from the
@@ -22,8 +22,8 @@ module Test.Consensus.MiniProtocol.ObjectDiffusion.Smoke
22
22
import Control.Monad.IOSim (runSimStrictShutdown )
23
23
import Control.ResourceRegistry (forkLinkedThread , waitAnyThread , withRegistry )
24
24
import Control.Tracer (Tracer , nullTracer , traceWith )
25
- import Data.Functor.Contravariant (contramap )
26
25
import Data.Containers.ListUtils (nubOrdOn )
26
+ import Data.Functor.Contravariant (contramap )
27
27
import Network.TypedProtocol.Channel (Channel , createConnectedChannels )
28
28
import Network.TypedProtocol.Codec (AnyMessage )
29
29
import Network.TypedProtocol.Driver.Simple (runPeer , runPipelinedPeer )
@@ -168,9 +168,9 @@ mkMockPoolInterfaces objects = do
168
168
169
169
newtype ProtocolConstants
170
170
= ProtocolConstants (NumObjectsOutstanding , NumObjectIdsReq , NumObjectsReq )
171
- deriving ( Show )
171
+ deriving Show
172
172
173
- instance Arbitrary ProtocolConstants
173
+ instance Arbitrary ProtocolConstants where
174
174
-- REVIEW: not sure about those constraints at all, and don't have the
175
175
-- brainspace right now to reason about them
176
176
arbitrary = do
@@ -179,17 +179,22 @@ instance Arbitrary ProtocolConstants
179
179
maxObjectsToReq <- choose (2 , maxIdsToReq)
180
180
pure $
181
181
ProtocolConstants
182
- ( NumObjectsOutstanding maxFifoSize ,
183
- NumObjectIdsReq maxIdsToReq ,
184
- NumObjectsReq maxObjectsToReq
182
+ ( NumObjectsOutstanding maxFifoSize
183
+ , NumObjectIdsReq maxIdsToReq
184
+ , NumObjectsReq maxObjectsToReq
185
185
)
186
186
187
187
nodeToNodeVersion :: NodeToNodeVersion
188
188
nodeToNodeVersion = NodeToNodeV_14
189
189
190
190
prop_smoke_init_inbound :: ProtocolConstants -> ListWithUniqueIds SmokeObject idTy -> Property
191
191
prop_smoke_init_inbound protocolConstants (ListWithUniqueIds objects) =
192
- prop_smoke_object_diffusion protocolConstants objects runOutboundPeer runInboundPeer (mkMockPoolInterfaces objects)
192
+ prop_smoke_object_diffusion
193
+ protocolConstants
194
+ objects
195
+ runOutboundPeer
196
+ runInboundPeer
197
+ (mkMockPoolInterfaces objects)
193
198
where
194
199
runOutboundPeer outbound outboundChannel tracer =
195
200
runPeer
@@ -207,9 +212,15 @@ prop_smoke_init_inbound protocolConstants (ListWithUniqueIds objects) =
207
212
(objectDiffusionInboundClientPeerPipelined inbound)
208
213
>> pure ()
209
214
210
- prop_smoke_init_outbound :: ProtocolConstants -> ListWithUniqueIds SmokeObject SmokeObjectId -> Property
215
+ prop_smoke_init_outbound ::
216
+ ProtocolConstants -> ListWithUniqueIds SmokeObject SmokeObjectId -> Property
211
217
prop_smoke_init_outbound protocolConstants (ListWithUniqueIds objects) =
212
- prop_smoke_object_diffusion protocolConstants objects runOutboundPeer runInboundPeer (mkMockPoolInterfaces objects)
218
+ prop_smoke_object_diffusion
219
+ protocolConstants
220
+ objects
221
+ runOutboundPeer
222
+ runInboundPeer
223
+ (mkMockPoolInterfaces objects)
213
224
where
214
225
runOutboundPeer outbound outboundChannel tracer =
215
226
runPeer
@@ -264,59 +275,61 @@ prop_smoke_object_diffusion ::
264
275
Property
265
276
prop_smoke_object_diffusion
266
277
(ProtocolConstants (maxFifoSize, maxIdsToReq, maxObjectsToReq))
267
- objects runOutboundPeer runInboundPeer mkPoolInterfaces
268
- =
269
- let
270
- simulationResult = runSimStrictShutdown $ do
271
- let tracer = nullTracer
272
-
273
- traceWith tracer " ========== [ Starting ObjectDiffusion smoke test ] =========="
274
- traceWith tracer (show objects)
275
-
276
- (outboundPoolReader, inboundPoolWriter, getAllInboundPoolContent) <- mkPoolInterfaces
277
- controlMessage <- uncheckedNewTVarM Continue
278
-
279
- let
280
- inbound =
281
- objectDiffusionInbound
282
- tracer
283
- ( maxFifoSize
284
- , maxIdsToReq
285
- , maxObjectsToReq
286
- )
287
- inboundPoolWriter
288
- nodeToNodeVersion
289
-
290
- outbound =
291
- objectDiffusionOutbound
292
- tracer
293
- maxFifoSize
294
- outboundPoolReader
295
- nodeToNodeVersion
296
- (readTVar controlMessage)
297
-
298
- withRegistry $ \ reg -> do
299
- (outboundChannel, inboundChannel) <- createConnectedChannels
300
- outboundThread <-
301
- forkLinkedThread reg " ObjectDiffusion Outbound peer thread" $
302
- runOutboundPeer outbound outboundChannel tracer
303
- inboundThread <-
304
- forkLinkedThread reg " ObjectDiffusion Inbound peer thread" $
305
- runInboundPeer inbound inboundChannel tracer
306
- controlMessageThread <- forkLinkedThread reg " ObjectDiffusion Control thread" $ do
307
- threadDelay 1000 -- give a head start to the other threads
308
- atomically $ writeTVar controlMessage Terminate
309
- threadDelay 1000 -- wait for the other threads to finish
310
- waitAnyThread [outboundThread, inboundThread, controlMessageThread]
311
-
312
- traceWith tracer " ========== [ ObjectDiffusion smoke test finished ] =========="
313
- poolContent <- getAllInboundPoolContent
314
-
315
- traceWith tracer " inboundPoolContent:"
316
- traceWith tracer (show poolContent)
317
- traceWith tracer " ========== ======================================= =========="
318
- pure poolContent
319
- in
320
- case simulationResult of
321
- Right inboundPoolContent -> inboundPoolContent === objects
322
- Left msg -> counterexample (show msg) $ property False
278
+ objects
279
+ runOutboundPeer
280
+ runInboundPeer
281
+ mkPoolInterfaces =
282
+ let
283
+ simulationResult = runSimStrictShutdown $ do
284
+ let tracer = nullTracer
285
+
286
+ traceWith tracer " ========== [ Starting ObjectDiffusion smoke test ] =========="
287
+ traceWith tracer (show objects)
288
+
289
+ (outboundPoolReader, inboundPoolWriter, getAllInboundPoolContent) <- mkPoolInterfaces
290
+ controlMessage <- uncheckedNewTVarM Continue
291
+
292
+ let
293
+ inbound =
294
+ objectDiffusionInbound
295
+ tracer
296
+ ( maxFifoSize
297
+ , maxIdsToReq
298
+ , maxObjectsToReq
299
+ )
300
+ inboundPoolWriter
301
+ nodeToNodeVersion
302
+
303
+ outbound =
304
+ objectDiffusionOutbound
305
+ tracer
306
+ maxFifoSize
307
+ outboundPoolReader
308
+ nodeToNodeVersion
309
+ (readTVar controlMessage)
310
+
311
+ withRegistry $ \ reg -> do
312
+ (outboundChannel, inboundChannel) <- createConnectedChannels
313
+ outboundThread <-
314
+ forkLinkedThread reg " ObjectDiffusion Outbound peer thread" $
315
+ runOutboundPeer outbound outboundChannel tracer
316
+ inboundThread <-
317
+ forkLinkedThread reg " ObjectDiffusion Inbound peer thread" $
318
+ runInboundPeer inbound inboundChannel tracer
319
+ controlMessageThread <- forkLinkedThread reg " ObjectDiffusion Control thread" $ do
320
+ threadDelay 1000 -- give a head start to the other threads
321
+ atomically $ writeTVar controlMessage Terminate
322
+ threadDelay 1000 -- wait for the other threads to finish
323
+ waitAnyThread [outboundThread, inboundThread, controlMessageThread]
324
+
325
+ traceWith tracer " ========== [ ObjectDiffusion smoke test finished ] =========="
326
+ poolContent <- getAllInboundPoolContent
327
+
328
+ traceWith tracer " inboundPoolContent:"
329
+ traceWith tracer (show poolContent)
330
+ traceWith tracer " ========== ======================================= =========="
331
+ pure poolContent
332
+ in
333
+ case simulationResult of
334
+ Right inboundPoolContent -> inboundPoolContent === objects
335
+ Left msg -> counterexample (show msg) $ property False
0 commit comments