@@ -1,7 +1,7 @@
1module TestTransport where
1module TestTransport where
3import Prelude hiding (catch)
3import Prelude hiding (catch)
4−import Control.Concurrent (forkIO)
4+import TestAuxiliary (forkTry, runTest)
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)
@@ -15,8 +15,6 @@ import Data.ByteString (ByteString)
15import Data.ByteString.Char8 (pack)
15import Data.ByteString.Char8 (pack)
16import Data.Map (Map)
16import Data.Map (Map)
17import qualified Data.Map as Map (empty, insert, delete, findWithDefault, adjust, null, toList, map)
17import qualified Data.Map as Map (empty, insert, delete, findWithDefault, adjust, null, toList, map)
18−import System.IO (hFlush, stdout)
19−import System.Timeout (timeout)
21-- Server that echoes messages straight back to the origin endpoint.
19-- Server that echoes messages straight back to the origin endpoint.
22echoServer :: EndPoint -> IO ()
20echoServer :: EndPoint -> IO ()
@@ -90,7 +88,7 @@ testPingPong transport numPings = do
90 result <- newEmptyMVar
88 result <- newEmptyMVar
92 -- Client
90 -- Client
93− forkIO $ do
91+ forkTry $ do
94 tlog "Ping client"
92 tlog "Ping client"
95 Right endpoint <- newEndPoint transport
93 Right endpoint <- newEndPoint transport
96 ping endpoint server numPings "ping"
94 ping endpoint server numPings "ping"
@@ -104,7 +102,7 @@ testEndPoints transport numPings = do
104 server <- spawn transport echoServer
102 server <- spawn transport echoServer
105 dones <- replicateM 2 newEmptyMVar
103 dones <- replicateM 2 newEmptyMVar
107− forM_ (zip dones ['A'..]) $ \(done, name) -> forkIO $ do
105+ forM_ (zip dones ['A'..]) $ \(done, name) -> forkTry $ do
108 let name' :: ByteString
106 let name' :: ByteString
109 name' = pack [name]
107 name' = pack [name]
110 Right endpoint <- newEndPoint transport
108 Right endpoint <- newEndPoint transport
@@ -121,7 +119,7 @@ testConnections transport numPings = do
121 result <- newEmptyMVar
119 result <- newEmptyMVar
123 -- Client
121 -- Client
124− forkIO $ do
122+ forkTry $ do
125 Right endpoint <- newEndPoint transport
123 Right endpoint <- newEndPoint transport
127 -- Open two connections to the server
125 -- Open two connections to the server
@@ -132,10 +130,10 @@ testConnections transport numPings = do
132 ConnectionOpened serv2 _ _ <- receive endpoint
130 ConnectionOpened serv2 _ _ <- receive endpoint
134 -- One thread to send "pingA" on the first connection
132 -- One thread to send "pingA" on the first connection
135− forkIO $ replicateM_ numPings $ send conn1 ["pingA"]
133+ forkTry $ replicateM_ numPings $ send conn1 ["pingA"]
137 -- One thread to send "pingB" on the second connection
135 -- One thread to send "pingB" on the second connection
138− forkIO $ replicateM_ numPings $ send conn2 ["pingB"]
136+ forkTry $ replicateM_ numPings $ send conn2 ["pingB"]
140 -- Verify server responses
138 -- Verify server responses
141 let verifyResponse 0 = putMVar result ()
139 let verifyResponse 0 = putMVar result ()
@@ -159,7 +157,7 @@ testCloseOneConnection transport numPings = do
159 result <- newEmptyMVar
157 result <- newEmptyMVar
161 -- Client
159 -- Client
162− forkIO $ do
160+ forkTry $ do
163 Right endpoint <- newEndPoint transport
161 Right endpoint <- newEndPoint transport
165 -- Open two connections to the server
163 -- Open two connections to the server
@@ -170,12 +168,12 @@ testCloseOneConnection transport numPings = do
170 ConnectionOpened serv2 _ _ <- receive endpoint
168 ConnectionOpened serv2 _ _ <- receive endpoint
172 -- One thread to send "pingA" on the first connection
170 -- One thread to send "pingA" on the first connection
173− forkIO $ do
171+ forkTry $ do
174 replicateM_ numPings $ send conn1 ["pingA"]
172 replicateM_ numPings $ send conn1 ["pingA"]
175 close conn1
173 close conn1
177 -- One thread to send "pingB" on the second connection
175 -- One thread to send "pingB" on the second connection
178− forkIO $ replicateM_ (numPings * 2) $ send conn2 ["pingB"]
176+ forkTry $ replicateM_ (numPings * 2) $ send conn2 ["pingB"]
180 -- Verify server responses
178 -- Verify server responses
181 let verifyResponse 0 = putMVar result ()
179 let verifyResponse 0 = putMVar result ()
@@ -203,7 +201,7 @@ testCloseOneDirection transport numPings = do
203 doneB <- newEmptyMVar
201 doneB <- newEmptyMVar
206− forkIO $ do
204+ forkTry $ do
207 tlog "A"
205 tlog "A"
208 Right endpoint <- newEndPoint transport
206 Right endpoint <- newEndPoint transport
209 tlog (show (address endpoint))
207 tlog (show (address endpoint))
@@ -239,7 +237,7 @@ testCloseOneDirection transport numPings = do
239 putMVar doneA ()
237 putMVar doneA ()
242− forkIO $ do
240+ forkTry $ do
244 Right endpoint <- newEndPoint transport
242 Right endpoint <- newEndPoint transport
245 tlog (show (address endpoint))
243 tlog (show (address endpoint))
@@ -318,7 +316,7 @@ testCloseReopen transport numPings = do
318 let numRepeats = 2 :: Int
316 let numRepeats = 2 :: Int
321− forkIO $ do
319+ forkTry $ do
322 Right endpoint <- newEndPoint transport
320 Right endpoint <- newEndPoint transport
324 forM_ [1 .. numRepeats] $ \i -> do
322 forM_ [1 .. numRepeats] $ \i -> do
@@ -337,7 +335,7 @@ testCloseReopen transport numPings = do
337 tlog "A finishing"
335 tlog "A finishing"
340− forkIO $ do
338+ forkTry $ do
341 Right endpoint <- newEndPoint transport
339 Right endpoint <- newEndPoint transport
342 putMVar addrB (address endpoint)
340 putMVar addrB (address endpoint)
@@ -360,13 +358,13 @@ testParallelConnects transport numPings = do
360 Right endpoint <- newEndPoint transport
358 Right endpoint <- newEndPoint transport
362 -- Spawn lots of clients
360 -- Spawn lots of clients
363− forM_ [1 .. numPings] $ \i -> forkIO $ do
361+ forM_ [1 .. numPings] $ \i -> forkTry $ do
364 Right conn <- connect endpoint server ReliableOrdered
362 Right conn <- connect endpoint server ReliableOrdered
365 send conn [pack $ "ping" ++ show i]
363 send conn [pack $ "ping" ++ show i]
366 send conn [pack $ "ping" ++ show i]
364 send conn [pack $ "ping" ++ show i]
367 close conn
365 close conn
369− forkIO $ do
367+ forkTry $ do
370 eventss <- collect endpoint (numPings * 4)
368 eventss <- collect endpoint (numPings * 4)
371 -- Check that no pings got sent to the wrong connection
369 -- Check that no pings got sent to the wrong connection
372 forM_ eventss $ \(_, [[ping1], [ping2]]) ->
370 forM_ eventss $ \(_, [[ping1], [ping2]]) ->
@@ -381,7 +379,7 @@ testSendAfterClose transport _ = do
381 server <- spawn transport echoServer
379 server <- spawn transport echoServer
382 clientDone <- newEmptyMVar
380 clientDone <- newEmptyMVar
384− forkIO $ do
382+ forkTry $ do
385 Right endpoint <- newEndPoint transport
383 Right endpoint <- newEndPoint transport
387 -- We request two lightweight connections
385 -- We request two lightweight connections
@@ -403,15 +401,13 @@ testSendAfterClose transport _ = do
404 takeMVar clientDone
402 takeMVar clientDone
408-- Test that closing the same connection twice has no effect
404-- Test that closing the same connection twice has no effect
409testCloseTwice :: Transport -> Int -> IO ()
405testCloseTwice :: Transport -> Int -> IO ()
410testCloseTwice transport _ = do
406testCloseTwice transport _ = do
411 server <- spawn transport echoServer
407 server <- spawn transport echoServer
412 clientDone <- newEmptyMVar
408 clientDone <- newEmptyMVar
414− forkIO $ do
410+ forkTry $ do
415 Right endpoint <- newEndPoint transport
411 Right endpoint <- newEndPoint transport
417 -- We request two lightweight connections
413 -- We request two lightweight connections
@@ -437,30 +433,20 @@ testCloseTwice transport _ = do
438 takeMVar clientDone
434 takeMVar clientDone
440−runTestIO :: String -> IO () -> IO ()
441−runTestIO description test = do
442− putStr $ "Running " ++ show description ++ ": "
447−runTest :: String -> (Transport -> Int -> IO ()) -> ReaderT (Transport, Int) IO ()
436+runTransportTest :: String -> (Transport -> Int -> IO ()) -> ReaderT (Transport, Int) IO ()
448−runTest description test = do
437+runTransportTest description test = do
449 (transport, numPings) <- ask
438 (transport, numPings) <- ask
450− done <- liftIO $ timeout 10000000 $ runTestIO description (test transport numPings)
439+ liftIO $ runTest description (test transport numPings)
452− Just () -> return ()
453− Nothing -> error "timeout"
455-- Transport tests
441-- Transport tests
456testTransport :: Transport -> IO ()
442testTransport :: Transport -> IO ()
457testTransport transport = flip runReaderT (transport, 10000) $ do
443testTransport transport = flip runReaderT (transport, 10000) $ do
458− runTest "PingPong" testPingPong
444+ runTransportTest "PingPong" testPingPong
459− runTest "EndPoints" testEndPoints
445+ runTransportTest "EndPoints" testEndPoints
460− runTest "Connections" testConnections
446+ runTransportTest "Connections" testConnections
461− runTest "CloseOneConnection" testCloseOneConnection
447+ runTransportTest "CloseOneConnection" testCloseOneConnection
462− runTest "CloseOneDirection" testCloseOneDirection
448+ runTransportTest "CloseOneDirection" testCloseOneDirection
463− runTest "CloseReopen" testCloseReopen
449+ runTransportTest "CloseReopen" testCloseReopen
464− runTest "ParallelConnects" testParallelConnects
450+ runTransportTest "ParallelConnects" testParallelConnects
465− runTest "SendAfterClose" testSendAfterClose
451+ runTransportTest "SendAfterClose" testSendAfterClose
466− runTest "CloseTwice" testCloseTwice
452+ runTransportTest "CloseTwice" testCloseTwice