Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
08ee7c4
typed-protocols: new API
coot Jul 25, 2024
81f2fbe
typed-protocols: Peer pattern synonyms
coot Jul 25, 2024
fe36759
typed-protocols-examples: updated
coot Jul 25, 2024
88d246f
typed-protocols: ActiveAgency
coot Sep 6, 2021
b121a80
typed-protocols: simplify runPeerWithDriver
coot Mar 27, 2022
bdf8100
typed-protocols: internal lemmas module
coot May 23, 2022
564da48
typed-protocols: provide ReflRelativeAgency type aliases
coot Jul 13, 2022
6f5ce41
typed-protocols-examples: socketAsChannel
coot Sep 4, 2021
c69993a
typed-protocols-examples: ReqResp2 example
coot Sep 17, 2021
e4bcf98
typed-protocols-examples: Wedge
coot Sep 18, 2021
86d007a
typed-protocols-examples: requestOnce
coot Sep 28, 2021
b4070d4
typed-protocols-examples: fixed cborg tests
coot Oct 2, 2021
74a64f2
typed-protocols-examples: pipelined tests
coot Oct 2, 2021
5ef5f2d
typed-protocols-examples: unbounded buffered channel
coot Mar 27, 2022
ce833a9
typed-protocols-examples: added runConnectedPeersAsymetric
coot Mar 27, 2022
1ca4aa7
typed-protocols: simplify evidence of termination in a terminal state
coot May 13, 2022
558a9fc
typed-protocols: added StateToken type family to Protocol type class
coot Jun 23, 2022
03270e4
typed-protocols: renamed Pipelined kind to IsPipelined
coot Jul 13, 2022
6b6118d
typed-protocols: added AnyMessageAndAgency pattern synonym
coot Sep 8, 2023
abecdaf
typed-protocols: added application specific singletons for protocol s…
coot Sep 26, 2023
a81caa1
typed-protocols-examples: relaxed constraint in PingPong client
coot Jul 1, 2022
166f5af
typed-protocols-examples: fixed a socket test on macos
coot Aug 17, 2022
f5ccedd
typed-protocols: bump version to `0.2.0.0`
coot Aug 25, 2023
148a91b
Added CHANGELOG and bumped versions of the packages
coot May 17, 2022
6c6a8ce
typed-protocols: moved outstanding counter into IsPipelined kind
coot Jul 23, 2024
d2f1e74
typed-protocols-stateful: non-pipelined stateful Peer
coot Jul 24, 2024
0d9cb77
typed-protocols: moved types to `Core` module
coot Jul 24, 2024
5ec9a8c
typed-protocols: connectPipelined type signature
coot Jul 24, 2024
8d31814
typed-protocols: added PeerPipelined newtype wrapper
coot Jul 24, 2024
b3ba779
Support io-classes-1.7
coot Jul 26, 2024
b23f327
typed-protocols: added haddocks
coot Aug 19, 2024
72a799c
disabled `typed-protocols-doc` in GHA
coot Aug 22, 2024
5cf79f3
stylish-haskell: fixes
coot Aug 22, 2024
b9e71bb
typed-protocols: removed unused extensions
coot Aug 23, 2024
20aa2fe
Improved haddocks, removed unused pragmas (extensions)
coot Aug 23, 2024
bdcf30a
Core module cannot be checked with stylish-haskell
coot Aug 23, 2024
0042b72
Removed Stateful PingPong example
coot Sep 4, 2024
b195c92
Stateful RPC example
coot Sep 5, 2024
f451040
typed-protocols: fixed typos
coot Sep 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
typed-protocols-examples: pipelined tests
* using TBQueue based channels in ST and IO
* using unix named pipes and 'handleAsChannel'
* using sockets and 'socketAsChannel'
  • Loading branch information
coot committed Sep 16, 2024
commit 74a64f2b3bd55c3dbd24d173df1078b1c5922ce1
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ createPipelineTestChannels sz = do
--
-- The Handles should be open in the appropriate read or write mode, and in
-- binary mode. Writes are flushed after each write, so it is safe to use
-- a buffering mode.
-- a buffering mode. On unix named pipes can be used, see
-- 'Network.TypedProtocol.ReqResp.Test.prop_namedPipePipelined_IO'
--
-- For bidirectional handles it is safe to pass the same handle for both.
--
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
Expand Down Expand Up @@ -34,10 +35,17 @@ import Control.Monad.Class.MonadThrow
import Control.Monad.IOSim (runSimOrThrow)
import Control.Monad.ST (runST)
import Control.Tracer (nullTracer)

import Data.Functor.Identity (Identity (..))
import Data.List (inits, tails)

import qualified Data.ByteString.Lazy as LBS
import Data.List (inits, tails)
#if !defined(mingw32_HOST_OS)
import qualified Network.Socket as Socket
import System.Directory (removeFile)
import System.IO
import qualified System.Posix.Files as Posix
#endif

import Test.QuickCheck
import Test.Tasty (TestTree, testGroup)
Expand All @@ -61,6 +69,10 @@ tests = testGroup "Network.TypedProtocol.PingPong"
, testProperty "connect_pipelined 5" prop_connect_pipelined5
, testProperty "channel ST" prop_channel_ST
, testProperty "channel IO" prop_channel_IO
#if !defined(mingw32_HOST_OS)
, testProperty "namedPipePipelined" prop_namedPipePipelined_IO
, testProperty "socketPipelined" prop_socketPipelined_IO
#endif
, testGroup "Codec"
[ testProperty "codec" prop_codec_PingPong
, testProperty "codec 2-splits" prop_codec_splits2_PingPong
Expand Down Expand Up @@ -313,6 +325,64 @@ prop_channel_ST n =
runSimOrThrow (prop_channel n)


#if !defined(mingw32_HOST_OS)
prop_namedPipePipelined_IO :: NonNegative Int
-> Property
prop_namedPipePipelined_IO (NonNegative n) = ioProperty $ do
let client = pingPongClientPeer (pingPongClientCount n)
server = pingPongServerPeer pingPongServerCount

let cliPath = "client.sock"
srvPath = "server.sock"
mode = Posix.ownerModes

Posix.createNamedPipe cliPath mode
Posix.createNamedPipe srvPath mode

bracket (openFile cliPath ReadWriteMode)
(\_ -> removeFile cliPath)
$ \cliHandle ->
bracket (openFile srvPath ReadWriteMode)
(\_ -> removeFile srvPath)
$ \srvHandle -> do
((), n') <- runConnectedPeers (return ( handlesAsChannel cliHandle srvHandle
, handlesAsChannel srvHandle cliHandle
))
nullTracer
CBOR.codecPingPong client server
return (n' == n)
#endif


#if !defined(mingw32_HOST_OS)
prop_socketPipelined_IO :: NonNegative Int
-> Property
prop_socketPipelined_IO (NonNegative n) = ioProperty $ do
ai : _ <- Socket.getAddrInfo Nothing (Just "127.0.0.1") Nothing
bracket
((,) <$> Socket.openSocket ai
<*> Socket.openSocket ai)
( \ (sock, sock') -> Socket.close sock
>> Socket.close sock')
$ \ (sock, sock') -> do
Socket.bind sock (Socket.addrAddress ai)
addr <- Socket.getSocketName sock
Socket.listen sock 1
Socket.connect sock' addr
bracket (fst <$> Socket.accept sock) Socket.close
$ \sock'' -> do
let client = pingPongClientPeer (pingPongClientCount n)
server = pingPongServerPeer pingPongServerCount

((), n') <- runConnectedPeers (return ( socketAsChannel sock'
, socketAsChannel sock''
))
nullTracer
CBOR.codecPingPong client server
return (n' == n)
#endif


--
-- Codec properties
--
Expand Down
112 changes: 110 additions & 2 deletions typed-protocols-examples/test/Network/TypedProtocol/ReqResp/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
Expand Down Expand Up @@ -29,16 +30,25 @@ import Network.TypedProtocol.ReqResp.Examples
import Network.TypedProtocol.ReqResp.Server
import Network.TypedProtocol.ReqResp.Type

import Control.Exception (throw)
import Control.Monad.Class.MonadAsync
import Control.Monad.Class.MonadST
import Control.Monad.Class.MonadSTM
import Control.Monad.Class.MonadThrow
import Control.Monad.IOSim (runSimOrThrow)
import Control.Monad.Class.MonadTimer.SI
import Control.Monad.IOSim
import Control.Monad.ST (runST)
import Control.Tracer (nullTracer)

import Data.Functor.Identity (Identity (..))
import Data.List (mapAccumL)
import Data.List (intercalate, mapAccumL)
import Data.Tuple (swap)
#if !defined(mingw32_HOST_OS)
import qualified Network.Socket as Socket
import System.Directory (removeFile)
import System.IO
import qualified System.Posix.Files as Posix
#endif

import Network.TypedProtocol.PingPong.Tests (splits2, splits2BS,
splits3, splits3BS)
Expand All @@ -61,6 +71,12 @@ tests = testGroup "Network.TypedProtocol.ReqResp"
, testProperty "connectPipelined" prop_connectPipelined
, testProperty "channel ST" prop_channel_ST
, testProperty "channel IO" prop_channel_IO
, testProperty "channelPipelined ST" prop_channelPipelined_ST
, testProperty "channelPipelined IO" prop_channelPipelined_IO
#if !defined(mingw32_HOST_OS)
, testProperty "namedPipePipelined" prop_namedPipePipelined_IO
, testProperty "socketPipelined" prop_socketPipelined_IO
#endif
, testGroup "Codec"
[ testProperty "codec" prop_codec_ReqResp
, testProperty "codec 2-splits" prop_codec_splits2_ReqResp
Expand Down Expand Up @@ -186,6 +202,7 @@ prop_channel f xs = do
server = reqRespServerPeer (reqRespServerMapAccumL
(\a -> pure . f a) 0)


prop_channel_IO :: (Int -> Int -> (Int, Int)) -> [Int] -> Property
prop_channel_IO f xs =
ioProperty (prop_channel f xs)
Expand All @@ -195,6 +212,97 @@ prop_channel_ST f xs =
runSimOrThrow (prop_channel f xs)


prop_channelPipelined :: ( MonadLabelledSTM m, MonadAsync m, MonadCatch m
, MonadDelay m, MonadST m)
=> (Int -> Int -> (Int, Int)) -> [Int]
-> m Bool
prop_channelPipelined f xs = do
(c, s) <- runConnectedPeersPipelined
(createPipelineTestChannels 100)
nullTracer
CBOR.codecReqResp
client server
return ((s, c) == mapAccumL f 0 xs)
where
client = reqRespClientPeerPipelined (reqRespClientMapPipelined xs)
server = reqRespServerPeer (reqRespServerMapAccumL
(\a -> pure . f a) 0)

prop_channelPipelined_IO :: (Int -> Int -> (Int, Int)) -> [Int] -> Property
prop_channelPipelined_IO f xs =
ioProperty (prop_channelPipelined f xs)

prop_channelPipelined_ST :: (Int -> Int -> (Int, Int)) -> [Int] -> Property
prop_channelPipelined_ST f xs =
let tr = runSimTrace (prop_channelPipelined f xs) in
counterexample (intercalate "\n" $ map show $ traceEvents tr)
$ case traceResult True tr of
Left err -> throw err
Right res -> res


#if !defined(mingw32_HOST_OS)
prop_namedPipePipelined_IO :: (Int -> Int -> (Int, Int)) -> [Int]
-> Property
prop_namedPipePipelined_IO f xs = ioProperty $ do
let client = reqRespClientPeerPipelined (reqRespClientMapPipelined xs)
server = reqRespServerPeer (reqRespServerMapAccumL
(\a -> pure . f a) 0)
let cliPath = "client.sock"
srvPath = "server.sock"
mode = Posix.ownerModes

Posix.createNamedPipe cliPath mode
Posix.createNamedPipe srvPath mode

bracket (openFile cliPath ReadWriteMode)
(\_ -> removeFile cliPath)
$ \cliHandle ->
bracket (openFile srvPath ReadWriteMode)
(\_ -> removeFile srvPath)
$ \srvHandle -> do
(c, s) <- runConnectedPeersPipelined
(return ( handlesAsChannel cliHandle srvHandle
, handlesAsChannel srvHandle cliHandle
))
nullTracer
CBOR.codecReqResp
client server
return ((s, c) == mapAccumL f 0 xs)
#endif


#if !defined(mingw32_HOST_OS)
prop_socketPipelined_IO :: (Int -> Int -> (Int, Int)) -> [Int]
-> Property
prop_socketPipelined_IO f xs = ioProperty $ do
ai : _ <- Socket.getAddrInfo Nothing (Just "127.0.0.1") Nothing
bracket
((,) <$> Socket.openSocket ai
<*> Socket.openSocket ai)
( \ (sock, sock') -> Socket.close sock
>> Socket.close sock')
$ \ (sock, sock') -> do
Socket.bind sock (Socket.addrAddress ai)
addr <- Socket.getSocketName sock
Socket.listen sock 1
Socket.connect sock' addr
bracket (fst <$> Socket.accept sock) Socket.close
$ \sock'' -> do
let client = reqRespClientPeerPipelined (reqRespClientMapPipelined xs)
server = reqRespServerPeer (reqRespServerMapAccumL
(\a -> pure . f a) 0)

(c, s) <- runConnectedPeersPipelined
(return ( socketAsChannel sock'
, socketAsChannel sock''
))
nullTracer
CBOR.codecReqResp
client server
return ((s, c) == mapAccumL f 0 xs)
#endif

--
-- Codec properties
--
Expand Down
6 changes: 6 additions & 0 deletions typed-protocols-examples/typed-protocols-examples.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ test-suite test
, QuickCheck
, tasty
, tasty-quickcheck

if !os(windows)
build-depends: directory
, network
, unix

default-language: Haskell2010
ghc-options: -rtsopts
-Wall
Expand Down