@@ -4,12 +4,11 @@ module TestTransport where
44import Prelude hiding (catch, (>>=), (>>), return, fail)
55import TestAuxiliary (forkTry, runTests, trySome, randomThreadDelay)
66import Control.Concurrent (forkIO, killThread, yield)
7−import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar, readMVar)
8−import Control.Exception (evaluate, throw)
7+import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar, readMVar, tryTakeMVar)
8+import Control.Exception (evaluate, throw, throwIO)
99import Control.Monad (replicateM, replicateM_, when, guard, forM, forM_)
1010import Control.Monad.Error ()
11−import Network.Transport hiding (connect)
12−import qualified Network.Transport as NT
11+import Network.Transport
1312import Network.Transport.Internal (tlog)
1413import Network.Transport.Util (spawn)
1514import System.Random (randomIO)
@@ -22,10 +21,6 @@ import Data.Maybe (catMaybes)
2221import Data.List (permutations)
2322import Traced
2423
25−-- | We overload connect to always pass the default hints
26−connect :: EndPoint -> EndPointAddress -> Reliability -> IO (Either (TransportError ConnectErrorCode) Connection)
27−connect ep addr rel = NT.connect ep addr rel defaultConnectHints
28−
2924-- | Server that echoes messages straight back to the origin endpoint.
3025echoServer :: EndPoint -> IO ()
3126echoServer endpoint = do
@@ -37,7 +32,7 @@ echoServer endpoint = do
3732 case event of
3833 ConnectionOpened cid rel addr -> do
3934 tlog $ "Opened new connection " ++ show cid
40− Right conn <- connect endpoint addr rel
35+ Right conn <- connect endpoint addr rel defaultConnectHints
4136 go (Map.insert cid conn cs)
4237 Received cid payload -> do
4338 send (Map.findWithDefault (error $ "Received: Invalid cid " ++ show cid) cid cs) payload
@@ -59,7 +54,7 @@ ping :: EndPoint -> EndPointAddress -> Int -> ByteString -> IO ()
5954ping endpoint server numPings msg = do
6055 -- Open connection to the server
6156 tlog "Connect to echo server"
62− Right conn <- connect endpoint server ReliableOrdered
57+ Right conn <- connect endpoint server ReliableOrdered defaultConnectHints
6358
6459 -- Wait for the server to open reply connection
6560 tlog "Wait for ConnectionOpened message"
@@ -126,10 +121,10 @@ testConnections transport numPings = do
126121 Right endpoint <- newEndPoint transport
127122
128123 -- Open two connections to the server
129− Right conn1 <- connect endpoint server ReliableOrdered
124+ Right conn1 <- connect endpoint server ReliableOrdered defaultConnectHints
130125 ConnectionOpened serv1 _ _ <- receive endpoint
131126
132− Right conn2 <- connect endpoint server ReliableOrdered
127+ Right conn2 <- connect endpoint server ReliableOrdered defaultConnectHints
133128 ConnectionOpened serv2 _ _ <- receive endpoint
134129
135130 -- One thread to send "pingA" on the first connection
@@ -164,10 +159,10 @@ testCloseOneConnection transport numPings = do
164159 Right endpoint <- newEndPoint transport
165160
166161 -- Open two connections to the server
167− Right conn1 <- connect endpoint server ReliableOrdered
162+ Right conn1 <- connect endpoint server ReliableOrdered defaultConnectHints
168163 ConnectionOpened serv1 _ _ <- receive endpoint
169164
170− Right conn2 <- connect endpoint server ReliableOrdered
165+ Right conn2 <- connect endpoint server ReliableOrdered defaultConnectHints
171166 ConnectionOpened serv2 _ _ <- receive endpoint
172167
173168 -- One thread to send "pingA" on the first connection
@@ -212,7 +207,7 @@ testCloseOneDirection transport numPings = do
212207
213208 -- Connect to B
214209 tlog "Connect to B"
215− Right conn <- readMVar addrB >>= \addr -> connect endpoint addr ReliableOrdered
210+ Right conn <- readMVar addrB >>= \addr -> connect endpoint addr ReliableOrdered defaultConnectHints
216211
217212 -- Wait for B to connect to us
218213 tlog "Wait for B"
@@ -252,7 +247,7 @@ testCloseOneDirection transport numPings = do
252247
253248 -- Connect to A
254249 tlog "Connect to A"
255− Right conn <- readMVar addrA >>= \addr -> connect endpoint addr ReliableOrdered
250+ Right conn <- readMVar addrA >>= \addr -> connect endpoint addr ReliableOrdered defaultConnectHints
256251
257252 -- Wait for A's pings
258253 tlog "Wait for pings from A"
@@ -331,7 +326,7 @@ testCloseReopen transport numPings = do
331326 forM_ [1 .. numRepeats] $ \i -> do
332327 tlog "A connecting"
333328 -- Connect to B
334− Right conn <- readMVar addrB >>= \addr -> connect endpoint addr ReliableOrdered
329+ Right conn <- readMVar addrB >>= \addr -> connect endpoint addr ReliableOrdered defaultConnectHints
335330
336331 tlog "A pinging"
337332 -- Say hi
@@ -368,7 +363,7 @@ testParallelConnects transport numPings = do
368363
369364 -- Spawn lots of clients
370365 forM_ [1 .. numPings] $ \i -> forkTry $ do
371− Right conn <- connect endpoint server ReliableOrdered
366+ Right conn <- connect endpoint server ReliableOrdered defaultConnectHints
372367 send conn [pack $ "ping" ++ show i]
373368 send conn [pack $ "ping" ++ show i]
374369 close conn
@@ -393,8 +388,8 @@ testSendAfterClose transport numRepeats = do
393388
394389 -- We request two lightweight connections
395390 replicateM numRepeats $ do
396− Right conn1 <- connect endpoint server ReliableOrdered
397− Right conn2 <- connect endpoint server ReliableOrdered
391+ Right conn1 <- connect endpoint server ReliableOrdered defaultConnectHints
392+ Right conn2 <- connect endpoint server ReliableOrdered defaultConnectHints
398393
399394 -- Close the second, but leave the first open; then output on the second
400395 -- connection (i.e., on a closed connection while there is still another
@@ -424,8 +419,8 @@ testCloseTwice transport numRepeats = do
424419
425420 replicateM numRepeats $ do
426421 -- We request two lightweight connections
427− Right conn1 <- connect endpoint server ReliableOrdered
428− Right conn2 <- connect endpoint server ReliableOrdered
422+ Right conn1 <- connect endpoint server ReliableOrdered defaultConnectHints
423+ Right conn2 <- connect endpoint server ReliableOrdered defaultConnectHints
429424
430425 -- Close the second one twice
431426 close conn2
@@ -455,7 +450,7 @@ testConnectToSelf transport numPings = do
455450 Right endpoint <- newEndPoint transport
456451
457452 tlog "Creating self-connection"
458− Right conn <- connect endpoint (address endpoint) ReliableOrdered
453+ Right conn <- connect endpoint (address endpoint) ReliableOrdered defaultConnectHints
459454
460455 tlog "Talk to myself"
461456
@@ -496,8 +491,8 @@ testConnectToSelfTwice transport numPings = do
496491 Right endpoint <- newEndPoint transport
497492
498493 tlog "Creating self-connection"
499− Right conn1 <- connect endpoint (address endpoint) ReliableOrdered
500− Right conn2 <- connect endpoint (address endpoint) ReliableOrdered
494+ Right conn1 <- connect endpoint (address endpoint) ReliableOrdered defaultConnectHints
495+ Right conn2 <- connect endpoint (address endpoint) ReliableOrdered defaultConnectHints
501496
502497 tlog "Talk to myself"
503498
@@ -541,9 +536,9 @@ testCloseSelf newTransport = do
541536 Right transport <- newTransport
542537 Right endpoint1 <- newEndPoint transport
543538 Right endpoint2 <- newEndPoint transport
544− Right conn1 <- connect endpoint1 (address endpoint1) ReliableOrdered
545− Right conn2 <- connect endpoint1 (address endpoint1) ReliableOrdered
546− Right conn3 <- connect endpoint2 (address endpoint2) ReliableOrdered
539+ Right conn1 <- connect endpoint1 (address endpoint1) ReliableOrdered defaultConnectHints
540+ Right conn2 <- connect endpoint1 (address endpoint1) ReliableOrdered defaultConnectHints
541+ Right conn3 <- connect endpoint2 (address endpoint2) ReliableOrdered defaultConnectHints
547542
548543 -- Close the conneciton and try to send
549544 close conn1
@@ -554,13 +549,13 @@ testCloseSelf newTransport = do
554549 -- to the second endpoint should still be fine
555550 closeEndPoint endpoint1
556551 Left (TransportError SendFailed _) <- send conn2 ["ping"]
557− Left (TransportError ConnectFailed _) <- connect endpoint1 (address endpoint1) ReliableOrdered
552+ Left (TransportError ConnectFailed _) <- connect endpoint1 (address endpoint1) ReliableOrdered defaultConnectHints
558553 Right () <- send conn3 ["ping"]
559554
560555 -- Close the transport; now the second should no longer work
561556 closeTransport transport
562557 Left (TransportError SendFailed _) <- send conn3 ["ping"]
563− Left (TransportError ConnectFailed _) <- connect endpoint2 (address endpoint2) ReliableOrdered
558+ Left (TransportError ConnectFailed _) <- connect endpoint2 (address endpoint2) ReliableOrdered defaultConnectHints
564559
565560 return ()
566561
@@ -592,7 +587,7 @@ testCloseEndPoint transport _ = do
592587 ConnectionOpened cid ReliableOrdered addr <- receive endpoint ; True <- return $ addr == theirAddr
593588 Received cid' ["ping"] <- receive endpoint ; True <- return $ cid == cid'
594589
595− Right conn <- connect endpoint theirAddr ReliableOrdered
590+ Right conn <- connect endpoint theirAddr ReliableOrdered defaultConnectHints
596591 send conn ["pong"]
597592
598593 ConnectionClosed cid'' <- receive endpoint ; True <- return $ cid == cid''
@@ -614,7 +609,7 @@ testCloseEndPoint transport _ = do
614609 putMVar clientAddr1 (address endpoint)
615610
616611 -- Connect to the server, then close the endpoint without disconnecting explicitly
617− Right _ <- connect endpoint theirAddr ReliableOrdered
612+ Right _ <- connect endpoint theirAddr ReliableOrdered defaultConnectHints
618613 closeEndPoint endpoint
619614 EndPointClosed <- receive endpoint
620615 return ()
@@ -624,7 +619,7 @@ testCloseEndPoint transport _ = do
624619 Right endpoint <- newEndPoint transport
625620 putMVar clientAddr2 (address endpoint)
626621
627− Right conn <- connect endpoint theirAddr ReliableOrdered
622+ Right conn <- connect endpoint theirAddr ReliableOrdered defaultConnectHints
628623 send conn ["ping"]
629624
630625 -- Reply from the server
@@ -642,7 +637,7 @@ testCloseEndPoint transport _ = do
642637 () <- close conn
643638
644639 -- And so should an attempt to connect
645− Left (TransportError ConnectFailed _) <- connect endpoint theirAddr ReliableOrdered
640+ Left (TransportError ConnectFailed _) <- connect endpoint theirAddr ReliableOrdered defaultConnectHints
646641
647642 return ()
648643
@@ -677,7 +672,7 @@ testCloseTransport newTransport = do
677672 ConnectionOpened cid2 ReliableOrdered addr' <- receive endpoint ; True <- return $ addr' == theirAddr2
678673 Received cid2' ["ping"] <- receive endpoint ; True <- return $ cid2' == cid2
679674
680− Right conn <- connect endpoint theirAddr2 ReliableOrdered
675+ Right conn <- connect endpoint theirAddr2 ReliableOrdered defaultConnectHints
681676 send conn ["pong"]
682677
683678 -- Client now closes down its transport. We should receive connection closed messages (we don't know the precise order, however)
@@ -703,13 +698,13 @@ testCloseTransport newTransport = do
703698 putMVar clientAddr1 (address endpoint1)
704699
705700 -- Connect to the server, then close the endpoint without disconnecting explicitly
706− Right _ <- connect endpoint1 theirAddr ReliableOrdered
701+ Right _ <- connect endpoint1 theirAddr ReliableOrdered defaultConnectHints
707702
708703 -- Set up an endpoint with one outgoing and out incoming connection
709704 Right endpoint2 <- newEndPoint transport
710705 putMVar clientAddr2 (address endpoint2)
711706
712− Right conn <- connect endpoint2 theirAddr ReliableOrdered
707+ Right conn <- connect endpoint2 theirAddr ReliableOrdered defaultConnectHints
713708 send conn ["ping"]
714709
715710 -- Reply from the server
@@ -730,8 +725,8 @@ testCloseTransport newTransport = do
730725 () <- close conn
731726
732727 -- And so should an attempt to connect on either endpoint
733− Left (TransportError ConnectFailed _) <- connect endpoint1 theirAddr ReliableOrdered
734− Left (TransportError ConnectFailed _) <- connect endpoint2 theirAddr ReliableOrdered
728+ Left (TransportError ConnectFailed _) <- connect endpoint1 theirAddr ReliableOrdered defaultConnectHints
729+ Left (TransportError ConnectFailed _) <- connect endpoint2 theirAddr ReliableOrdered defaultConnectHints
735730
736731 -- And finally, so should an attempt to create a new endpoint
737732 Left (TransportError NewEndPointFailed _) <- newEndPoint transport
@@ -760,7 +755,7 @@ testConnectClosedEndPoint transport = do
760755 Right endpoint <- newEndPoint transport
761756 readMVar serverClosed
762757
763− Left (TransportError ConnectNotFound _) <- readMVar serverAddr >>= \addr -> connect endpoint addr ReliableOrdered
758+ Left (TransportError ConnectNotFound _) <- readMVar serverAddr >>= \addr -> connect endpoint addr ReliableOrdered defaultConnectHints
764759
765760 putMVar clientDone ()
766761
@@ -794,7 +789,7 @@ testSendException newTransport = do
794789 Right endpoint2 <- newEndPoint transport
795790
796791 -- Connect endpoint1 to endpoint2
797− Right conn <- connect endpoint1 (address endpoint2) ReliableOrdered
792+ Right conn <- connect endpoint1 (address endpoint2) ReliableOrdered defaultConnectHints
798793 ConnectionOpened _ _ _ <- receive endpoint2
799794
800795 -- Send an exceptional value
@@ -807,7 +802,7 @@ testSendException newTransport = do
807802 ErrorEvent (TransportError (EventConnectionLost _ [_]) _) <- receive endpoint2
808803
809804 -- A new connection will re-establish the connection
810− Right conn2 <- connect endpoint1 (address endpoint2) ReliableOrdered
805+ Right conn2 <- connect endpoint1 (address endpoint2) ReliableOrdered defaultConnectHints
811806 send conn2 ["ping"]
812807 close conn2
813808
@@ -836,7 +831,7 @@ testKill newTransport numThreads = do
836831 done <- newEmptyMVar
837832 tid <- forkIO $ do
838833 randomThreadDelay 10
839− Right conn <- connect endpoint1 (address endpoint2) ReliableOrdered
834+ Right conn <- connect endpoint1 (address endpoint2) ReliableOrdered defaultConnectHints
840835 randomThreadDelay 10
841836 Right () <- send conn ["ping"]
842837 randomThreadDelay 10
@@ -869,8 +864,13 @@ testCrossing :: Transport -> Int -> IO ()
869864testCrossing transport numRepeats = do
870865 [aAddr, bAddr] <- replicateM 2 newEmptyMVar
871866 [aDone, bDone] <- replicateM 2 newEmptyMVar
867+ [aTimeout, bTimeout] <- replicateM 2 newEmptyMVar
872868 go <- newEmptyMVar
873869
870+ let hints = defaultConnectHints {
871+ connectTimeout = Just 5000000
872+ }
873+
874874 -- A
875875 forkTry $ do
876876 Right endpoint <- newEndPoint transport
@@ -879,8 +879,16 @@ testCrossing transport numRepeats = do
879879
880880 replicateM_ numRepeats $ do
881881 takeMVar go >> yield
882− Right conn <- connect endpoint theirAddress ReliableOrdered
883− close conn
882+ -- Because we are creating lots of connections, it's possible that
883+ -- connect times out (for instance, in the TCP transport,
884+ -- Network.Socket.connect may time out). We shouldn't regard this as an
885+ -- error in the Transport, though.
886+ connectResult <- connect endpoint theirAddress ReliableOrdered hints
887+ case connectResult of
888+ Right conn -> close conn
889+ Left (TransportError ConnectTimeout _) -> putMVar aTimeout ()
890+ Left (TransportError ConnectFailed _) -> readMVar bTimeout
891+ Left err -> throwIO . userError $ "testCrossed: " ++ show err
884892 putMVar aDone ()
885893
886894 -- B
@@ -891,12 +899,19 @@ testCrossing transport numRepeats = do
891899
892900 replicateM_ numRepeats $ do
893901 takeMVar go >> yield
894− Right conn <- connect endpoint theirAddress ReliableOrdered
895− close conn
902+ connectResult <- connect endpoint theirAddress ReliableOrdered hints
903+ case connectResult of
904+ Right conn -> close conn
905+ Left (TransportError ConnectTimeout _) -> putMVar bTimeout ()
906+ Left (TransportError ConnectFailed _) -> readMVar aTimeout
907+ Left err -> throwIO . userError $ "testCrossed: " ++ show err
896908 putMVar bDone ()
897909
898910 -- Driver
899911 forM_ [1 .. numRepeats] $ \_i -> do
912+ -- putStrLn $ "Round " ++ show _i
913+ tryTakeMVar aTimeout
914+ tryTakeMVar bTimeout
900915 putMVar go ()
901916 putMVar go ()
902917 takeMVar aDone