@@ -1,7 +1,7 @@
1module TestTransport where
1module TestTransport where
3import Prelude hiding (catch)
3import Prelude hiding (catch)
4−import Control.Concurrent (forkIO, threadDelay)
4+import Control.Concurrent (forkIO)
5import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar, readMVar)
5import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar, readMVar)
6import Control.Monad (replicateM, replicateM_, when, guard, forM_)
6import Control.Monad (replicateM, replicateM_, when, guard, forM_)
7import Control.Monad.Reader (ReaderT, runReaderT, ask)
7import Control.Monad.Reader (ReaderT, runReaderT, ask)
@@ -40,16 +40,20 @@ echoServer endpoint = do
43−expect :: EndPoint -> (Event -> Bool) -> IO ()
43+expect :: EndPoint -> (Event -> (Bool, a)) -> IO a
44expect endpoint predicate = do
44expect endpoint predicate = do
45 event <- receive endpoint
45 event <- receive endpoint
46 mbool <- catch (Right <$> evaluate (predicate event)) (return . Left)
46 mbool <- catch (Right <$> evaluate (predicate event)) (return . Left)
47 case mbool of
47 case mbool of
48− Left err -> do tlog $ "Unexpected event " ++ show event
48+ Left err -> do tlog $ "Unexpected event " ++ show event
49− throw (err :: SomeException)
49+ throw (err :: SomeException)
50− Right False -> do tlog $ "Unexpected event " ++ show event
50+ Right (False, _) -> do tlog $ "Unexpected event " ++ show event
51− fail "Unexpected event"
51+ fail "Unexpected event"
52− _ -> return ()
52+ Right (True, a) -> return a
54+expect' :: EndPoint -> (Event -> Bool) -> IO ()
55+expect' endpoint predicate =
56+ expect endpoint (\event -> (predicate event, ()))
54ping :: EndPoint -> EndPointAddress -> Int -> ByteString -> IO ()
58ping :: EndPoint -> EndPointAddress -> Int -> ByteString -> IO ()
55ping endpoint server numPings msg = do
59ping endpoint server numPings msg = do
@@ -59,13 +63,13 @@ ping endpoint server numPings msg = do
60 -- Wait for the server to open reply connection
64 -- Wait for the server to open reply connection
61 tlog "Wait for ConnectionOpened message"
65 tlog "Wait for ConnectionOpened message"
62− expect endpoint (\(ConnectionOpened _ _ _) -> True)
66+ cid <- expect endpoint (\(ConnectionOpened cid _ _) -> (True, cid))
64 -- Send pings and wait for reply
68 -- Send pings and wait for reply
65 tlog "Send ping and wait for reply"
69 tlog "Send ping and wait for reply"
66 replicateM_ numPings $ do
70 replicateM_ numPings $ do
67 send conn [msg]
71 send conn [msg]
68− expect endpoint (\(Received _ [reply]) -> reply == msg)
72+ expect' endpoint (\(Received cid' [reply]) -> cid == cid' && reply == msg)
70 -- Close the connection
74 -- Close the connection
71 tlog "Close the connection"
75 tlog "Close the connection"
@@ -73,7 +77,7 @@ ping endpoint server numPings msg = do
74 -- Wait for the server to close its connection to us
78 -- Wait for the server to close its connection to us
75 tlog "Wait for ConnectionClosed message"
79 tlog "Wait for ConnectionClosed message"
76− expect endpoint (\(ConnectionClosed _) -> True)
80+ expect' endpoint (\(ConnectionClosed cid') -> cid == cid')
79 tlog "Ping client done"
83 tlog "Ping client done"
@@ -388,18 +392,19 @@ testSendAfterClose transport _ = do
388 -- connection (i.e., on a closed connection while there is still another
392 -- connection (i.e., on a closed connection while there is still another
389 -- connection open)
393 -- connection open)
390 close conn2
394 close conn2
391− send conn2 ["ping2"]
395+ Left (FailedWith SendConnectionClosed _) <- send conn2 ["ping2"]
393 -- Now close the first connection, and output on it (i.e., output while
397 -- Now close the first connection, and output on it (i.e., output while
394 -- there are no lightweight connection at all anymore)
398 -- there are no lightweight connection at all anymore)
395 close conn1
399 close conn1
396− send conn1 ["ping1"]
400+ Left (FailedWith SendConnectionClosed _) <- send conn2 ["ping2"]
399 putMVar clientDone ()
402 putMVar clientDone ()
401 takeMVar clientDone
404 takeMVar clientDone
403-- Test that closing the same connection twice has no effect
408-- Test that closing the same connection twice has no effect
404testCloseTwice :: Transport -> Int -> IO ()
409testCloseTwice :: Transport -> Int -> IO ()
405testCloseTwice transport _ = do
410testCloseTwice transport _ = do
@@ -422,9 +427,12 @@ testCloseTwice transport _ = do
422 close conn1
427 close conn1
424 -- Verify expected response from the echo server
429 -- 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)
428 putMVar clientDone ()
436 putMVar clientDone ()
430 takeMVar clientDone
438 takeMVar clientDone