@@ -8,6 +8,7 @@ import Control.Concurrent.MVar
88import Network.Socket (SockAddr(..))
99import Control.Concurrent.Async
1010import Control.Monad
11+import Control.Concurrent
1112
1213import Network.RPC.Curryer.Server
1314import Network.RPC.Curryer.Client
@@ -15,13 +16,15 @@ import Network.RPC.Curryer.Client
1516testTree :: TestTree
1617testTree = testGroup "basic" [testCase "simple" testSimpleCall,
1718 testCase "client async" testAsyncServerCall,
18− testCase "server async" testAsyncClientCall
19+ testCase "server async" testAsyncClientCall,
20+ testCase "client sync timeout" testSyncClientCallTimeout
1921 ]
2022
2123--client-to-server request
2224data TestRequest = AddTwoNumbersReq Int Int
2325 | TestAsyncReq String
2426 | TestCallMeBackReq String
27+ | DelayMicrosecondsReq Int
2528 deriving (Generic, Show)
2629 deriving Serialise via WineryVariant TestRequest
2730
@@ -29,6 +32,7 @@ data TestRequest = AddTwoNumbersReq Int Int
2932data TestResponse = AddTwoNumbersResp Int
3033 | AsyncHello String
3134 | TestCallMeBackResp String
35+ | DelayMicrosecondsResp
3236 deriving (Generic, Show, Eq)
3337 deriving Serialise via WineryVariant TestResponse
3438
@@ -45,6 +49,9 @@ testServerMessageHandler mAsyncMVar msg = do
4549 Just mvar -> putMVar mvar s
4650 pure NoResponse
4751 TestAsyncReq _ -> pure NoResponse
52+ DelayMicrosecondsReq ms -> do
53+ threadDelay ms
54+ pure (HandlerResponse DelayMicrosecondsResp)
4855
4956-- test a simple client-to-server round-trip function execution
5057testSimpleCall :: Assertion
@@ -106,3 +113,23 @@ testAsyncClientCall = do
106113 assertEqual "async message" "hi server" asyncMessage
107114 close conn
108115 cancel server
116+
117+testSyncClientCallTimeout :: Assertion
118+testSyncClientCallTimeout = do
119+ readyVar <- newEmptyMVar
120+
121+ server <- async (serve (pure Nothing) (testServerMessageHandler Nothing) localHostAddr 0 (Just readyVar))
122+ --wait for server to be ready
123+ (SockAddrInet port _) <- takeMVar readyVar
124+ let clientHandler :: AsyncMessageHandler TestResponse
125+ clientHandler _ = error "async handler called"
126+ mkConn :: IO (Connection TestResponse)
127+ mkConn = connect clientHandler localHostAddr port
128+ conn <- mkConn
129+ let c :: IO (Either ConnectionError TestResponse)
130+ c = callTimeout (Just 1000) conn (DelayMicrosecondsReq 500)
131+ x <- c
132+ assertEqual "client sync timeout" (Left TimeoutError) x
133+ close conn
134+ cancel server
135+