@@ -1,7 +1,7 @@
11module TestTransport where
22
33import Prelude hiding (catch)
4−import Control.Concurrent (forkIO, threadDelay)
4+import Control.Concurrent (forkIO)
55import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar, readMVar)
66import Control.Monad (replicateM, replicateM_, when, guard, forM_)
77import Control.Monad.Reader (ReaderT, runReaderT, ask)
@@ -40,16 +40,20 @@ echoServer endpoint = do
4040 -- Ignore
4141 go cs
4242
43−expect :: EndPoint -> (Event -> Bool) -> IO ()
43+expect :: EndPoint -> (Event -> (Bool, a)) -> IO a
4444expect endpoint predicate = do
4545 event <- receive endpoint
4646 mbool <- catch (Right <$> evaluate (predicate event)) (return . Left)
4747 case mbool of
48− Left err -> do tlog $ "Unexpected event " ++ show event
49− throw (err :: SomeException)
50− Right False -> do tlog $ "Unexpected event " ++ show event
51− fail "Unexpected event"
52− _ -> return ()
48+ Left err -> do tlog $ "Unexpected event " ++ show event
49+ throw (err :: SomeException)
50+ Right (False, _) -> do tlog $ "Unexpected event " ++ show event
51+ fail "Unexpected event"
52+ Right (True, a) -> return a
53+
54+expect' :: EndPoint -> (Event -> Bool) -> IO ()
55+expect' endpoint predicate =
56+ expect endpoint (\event -> (predicate event, ()))
5357
5458ping :: EndPoint -> EndPointAddress -> Int -> ByteString -> IO ()
5559ping endpoint server numPings msg = do
@@ -59,13 +63,13 @@ ping endpoint server numPings msg = do
5963
6064 -- Wait for the server to open reply connection
6165 tlog "Wait for ConnectionOpened message"
62− expect endpoint (\(ConnectionOpened _ _ _) -> True)
66+ cid <- expect endpoint (\(ConnectionOpened cid _ _) -> (True, cid))
6367
6468 -- Send pings and wait for reply
6569 tlog "Send ping and wait for reply"
6670 replicateM_ numPings $ do
6771 send conn [msg]
68− expect endpoint (\(Received _ [reply]) -> reply == msg)
72+ expect' endpoint (\(Received cid' [reply]) -> cid == cid' && reply == msg)
6973
7074 -- Close the connection
7175 tlog "Close the connection"
@@ -73,7 +77,7 @@ ping endpoint server numPings msg = do
7377
7478 -- Wait for the server to close its connection to us
7579 tlog "Wait for ConnectionClosed message"
76− expect endpoint (\(ConnectionClosed _) -> True)
80+ expect' endpoint (\(ConnectionClosed cid') -> cid == cid')
7781
7882 -- Done
7983 tlog "Ping client done"
@@ -388,18 +392,19 @@ testSendAfterClose transport _ = do
388392 -- connection (i.e., on a closed connection while there is still another
389393 -- connection open)
390394 close conn2
391− send conn2 ["ping2"]
395+ Left (FailedWith SendConnectionClosed _) <- send conn2 ["ping2"]
392396
393397 -- Now close the first connection, and output on it (i.e., output while
394398 -- there are no lightweight connection at all anymore)
395399 close conn1
396− send conn1 ["ping1"]
400+ Left (FailedWith SendConnectionClosed _) <- send conn2 ["ping2"]
397401
398− threadDelay 100000
399402 putMVar clientDone ()
400403
401404 takeMVar clientDone
402405
406+
407+
403408-- Test that closing the same connection twice has no effect
404409testCloseTwice :: Transport -> Int -> IO ()
405410testCloseTwice transport _ = do
@@ -422,9 +427,12 @@ testCloseTwice transport _ = do
422427 close conn1
423428
424429 -- Verify expected response from the echo server
425− -- TODO
430+ cid1 <- expect endpoint (\(ConnectionOpened cid _ _) -> (True, cid))
431+ cid2 <- expect endpoint (\(ConnectionOpened cid _ _) -> (True, cid))
432+ expect' endpoint (\(ConnectionClosed cid) -> cid == cid2)
433+ expect' endpoint (\(Received cid msg) -> cid == cid1 && msg == ["ping"])
434+ expect' endpoint (\(ConnectionClosed cid) -> cid == cid1)
426435
427− threadDelay 1000000
428436 putMVar clientDone ()
429437
430438 takeMVar clientDone