Skip to content

Commit

Permalink
connection-manager: manage its own StdGen
Browse files Browse the repository at this point in the history
There's no need to pass `StdGen` to `PrunePolicy` from the
`InboundGovernor` when it's more natural to get it from connection
manager.

This opens a possibility to use `InboundObservableState` for different
purposes (and in a different way).
  • Loading branch information
coot committed May 7, 2024
1 parent 9765081 commit 4155800
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 253 deletions.
11 changes: 6 additions & 5 deletions ouroboros-network-framework/demo/connection-manager.hs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ withBidirectionalConnectionManager
-> DiffTime -- protocol idle timeout
-> DiffTime -- wait time timeout
-> Maybe peerAddr
-> Random.StdGen
-> ClientAndServerData
-- ^ series of request possible to do with the bidirectional connection
-- manager towards some peer.
Expand All @@ -204,6 +205,7 @@ withBidirectionalConnectionManager snocket makeBearer socket
protocolIdleTimeout
timeWaitTimeout
localAddress
stdGen
ClientAndServerData {
hotInitiatorRequests,
warmInitiatorRequests,
Expand All @@ -219,8 +221,6 @@ withBidirectionalConnectionManager snocket makeBearer socket
hotRequestsVar <- LazySTM.newTVarIO hotInitiatorRequests
warmRequestsVar <- LazySTM.newTVarIO warmInitiatorRequests
establishedRequestsVar <- LazySTM.newTVarIO establishedInitiatorRequests
-- we are not using the randomness
observableStateVar <- Server.newObservableStateVarFromSeed 0
let muxTracer = ("mux",) `contramap` nullTracer -- mux tracer

withConnectionManager
Expand All @@ -240,6 +240,7 @@ withBidirectionalConnectionManager snocket makeBearer socket
cmOutboundIdleTimeout = protocolIdleTimeout,
connectionDataFlow = \_ _ -> Duplex,
cmPrunePolicy = simplePrunePolicy,
cmStdGen = stdGen,
cmConnectionsLimits = AcceptedConnectionsLimit {
acceptedConnectionsHardLimit = maxBound,
acceptedConnectionsSoftLimit = maxBound,
Expand Down Expand Up @@ -282,8 +283,7 @@ withBidirectionalConnectionManager snocket makeBearer socket
serverConnectionLimits = AcceptedConnectionsLimit maxBound maxBound 0,
serverConnectionManager = connectionManager,
serverInboundIdleTimeout = Just protocolIdleTimeout,
serverInboundInfoChannel = inbgovInfoChannel,
serverObservableStateVar = observableStateVar
serverInboundInfoChannel = inbgovInfoChannel
}
)
(\thread -> link thread
Expand Down Expand Up @@ -461,10 +461,11 @@ bidirectionalExperiment
timeWaitTimeout
localAddr remoteAddr
clientAndServerData = do
stdGen <- Random.newStdGen
withBidirectionalConnectionManager
snocket makeBearer socket0
protocolIdleTimeout timeWaitTimeout
(Just localAddr) clientAndServerData $
(Just localAddr) stdGen clientAndServerData $
\connectionManager _serverAddr -> forever' $ do
-- runInitiatorProtocols returns a list of results per each protocol
-- in each bucket (warm \/ hot \/ established); but we run only one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
module Test.Ouroboros.Network.Server2.IO (tests) where

import Control.Monad.Class.MonadThrow
import System.Random (mkStdGen)

import Test.QuickCheck
import Test.Tasty (TestTree, testGroup)
Expand Down Expand Up @@ -52,9 +53,10 @@ tests =
--

prop_unidirectional_IO
:: ClientAndServerData Int
:: Fixed Int
-> ClientAndServerData Int
-> Property
prop_unidirectional_IO clientAndServerData =
prop_unidirectional_IO (Fixed rnd) clientAndServerData =
ioProperty $ do
withIOManager $ \iomgr ->
bracket
Expand All @@ -66,6 +68,7 @@ prop_unidirectional_IO clientAndServerData =
Socket.bind socket (Socket.addrAddress addr)
Socket.listen socket maxBound
unidirectionalExperiment
(mkStdGen rnd)
ioTimeouts
(socketSnocket iomgr)
Mux.makeSocketBearer
Expand All @@ -76,10 +79,11 @@ prop_unidirectional_IO clientAndServerData =


prop_bidirectional_IO
:: ClientAndServerData Int
:: Fixed Int
-> ClientAndServerData Int
-> ClientAndServerData Int
-> Property
prop_bidirectional_IO data0 data1 =
prop_bidirectional_IO (Fixed rnd) data0 data1 =
ioProperty $ do
withIOManager $ \iomgr ->
bracket
Expand Down Expand Up @@ -108,6 +112,7 @@ prop_bidirectional_IO data0 data1 =

bidirectionalExperiment
True
(mkStdGen rnd)
ioTimeouts
(socketSnocket iomgr)
Mux.makeSocketBearer
Expand Down
3 changes: 3 additions & 0 deletions ouroboros-network-framework/ouroboros-network-framework.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ library testlib
, bytestring
, cborg
, containers
, random
, serialise

, QuickCheck
Expand Down Expand Up @@ -186,6 +187,7 @@ test-suite sim-tests
, iproute
, network
, pretty-simple
, random
, serialise
, text
, time
Expand Down Expand Up @@ -251,6 +253,7 @@ test-suite io-tests
, dns
, iproute
, network
, random
, time
, with-utf8

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import Data.Monoid (All (..))
import Data.Text.Lazy qualified as Text
import Data.Void (Void)
import Quiet
import System.Random qualified as Random
import Text.Pretty.Simple (defaultOutputOptionsNoColor, pShowOpt)

import Network.Mux.Bearer
Expand Down Expand Up @@ -689,14 +690,15 @@ instance Arbitrary SkewedBool where
-- exception or being killed by an asynchronous asynchronous exception.
--
prop_valid_transitions
:: SkewedBool
:: Fixed Int
-> SkewedBool
-- ^ bind to local address or not
-> RefinedScheduleMap Addr
-- ^ A list of addresses to which we connect or which connect to us. We use
-- 'Blind' since we show the arguments using `counterexample` in a nicer
-- way.
-> Property
prop_valid_transitions (SkewedBool bindToLocalAddress) scheduleMap =
prop_valid_transitions (Fixed rnd) (SkewedBool bindToLocalAddress) scheduleMap =
let tr = runSimTrace experiment in
-- `selectTraceEventsDynamic`, can throw 'Failure', hence we run
-- `traceResults` first.
Expand Down Expand Up @@ -765,6 +767,7 @@ prop_valid_transitions (SkewedBool bindToLocalAddress) scheduleMap =
cmConfigureSocket = \_ _ -> return (),
connectionDataFlow = \(Version df) _ -> df,
cmPrunePolicy = simplePrunePolicy,
cmStdGen = Random.mkStdGen rnd,
cmConnectionsLimits = AcceptedConnectionsLimit {
acceptedConnectionsHardLimit = maxBound,
acceptedConnectionsSoftLimit = maxBound,
Expand Down Expand Up @@ -987,6 +990,7 @@ prop_valid_transitions (SkewedBool bindToLocalAddress) scheduleMap =
unit_overwritten :: Property
unit_overwritten =
prop_valid_transitions
(Fixed 42)
(SkewedBool True)
(ScheduleMap $ Map.fromList
[ ( TestAddress 1
Expand Down Expand Up @@ -1036,6 +1040,7 @@ unit_overwritten =
unit_timeoutExpired :: Property
unit_timeoutExpired =
prop_valid_transitions
(Fixed 42)
(SkewedBool True)
(ScheduleMap $ Map.fromList
[ ( TestAddress 1
Expand Down

0 comments on commit 4155800

Please sign in to comment.