Skip to content

Commit 14782a9

Browse files
committed
Add additional communication in monitor tests.
Network-transport are not guaranteed to emit connection failure event unless there were communication between local and remote endpoints. As a result additional communication was introduced in order introduce that communication.
1 parent da3cf40 commit 14782a9

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/Control/Distributed/Process/Tests/CH.hs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Control.Monad (replicateM_, replicateM, forever, void, unless)
2121
import Control.Exception (SomeException, throwIO)
2222
import qualified Control.Exception as Ex (catch)
2323
import Control.Applicative ((<$>), (<*>), pure, (<|>))
24-
import qualified Network.Transport as NT (closeEndPoint)
24+
import qualified Network.Transport as NT (closeEndPoint, EndPointAddress)
2525
import Control.Distributed.Process
2626
import Control.Distributed.Process.Internal.Types
2727
( NodeId(nodeAddress)
@@ -302,19 +302,25 @@ testMonitorRemoteDeadProcess TestTransport{..} mOrL un = do
302302
testMonitorDisconnect :: TestTransport -> Bool -> Bool -> Assertion
303303
testMonitorDisconnect TestTransport{..} mOrL un = do
304304
processAddr <- newEmptyMVar
305+
processAddr2 <- newEmptyMVar
305306
monitorSetup <- newEmptyMVar
306307
done <- newEmptyMVar
307308

308309
forkIO $ do
309310
localNode <- newLocalNode testTransport initRemoteTable
310-
addr <- forkProcess localNode . liftIO $ threadDelay 1000000
311+
addr <- forkProcess localNode $ expect
312+
addr2 <- forkProcess localNode $ return ()
311313
putMVar processAddr addr
312314
readMVar monitorSetup
313315
NT.closeEndPoint (localEndPoint localNode)
316+
putMVar processAddr2 addr2
314317

315318
forkIO $ do
316319
localNode <- newLocalNode testTransport initRemoteTable
317320
theirAddr <- readMVar processAddr
321+
forkProcess localNode $ do
322+
lc <- liftIO $ readMVar processAddr2
323+
send lc ()
318324
runProcess localNode $ do
319325
monitorTestProcess theirAddr mOrL un DiedDisconnect (Just monitorSetup) done
320326

@@ -583,17 +589,22 @@ testMonitorLiveNode :: TestTransport -> Assertion
583589
testMonitorLiveNode TestTransport{..} = do
584590
[node1, node2] <- replicateM 2 $ newLocalNode testTransport initRemoteTable
585591
ready <- newEmptyMVar
592+
readyr <- newEmptyMVar
586593
done <- newEmptyMVar
587594

595+
p <- forkProcess node1 $ return ()
588596
forkProcess node2 $ do
589597
ref <- monitorNode (localNodeId node1)
590598
liftIO $ putMVar ready ()
599+
liftIO $ takeMVar readyr
600+
send p ()
591601
NodeMonitorNotification ref' nid _ <- expect
592602
True <- return $ ref == ref' && nid == localNodeId node1
593603
liftIO $ putMVar done ()
594604

595605
takeMVar ready
596606
closeLocalNode node1
607+
putMVar readyr ()
597608

598609
takeMVar done
599610

@@ -688,7 +699,6 @@ testReconnect :: TestTransport -> Assertion
688699
testReconnect TestTransport{..} = do
689700
[node1, node2] <- replicateM 2 $ newLocalNode testTransport initRemoteTable
690701
let nid1 = localNodeId node1
691-
nid2 = localNodeId node2
692702
processA <- newEmptyMVar
693703
[sendTestOk, registerTestOk] <- replicateM 2 newEmptyMVar
694704

@@ -709,7 +719,8 @@ testReconnect TestTransport{..} = do
709719
send them "message 1" >> liftIO (threadDelay 100000)
710720

711721
-- Simulate network failure
712-
liftIO $ testBreakConnection (nodeAddress nid1) (nodeAddress nid2)
722+
liftIO $ syncBreakConnection testBreakConnection node1 node2
723+
713724

714725
-- Should not arrive
715726
send them "message 2"
@@ -734,7 +745,7 @@ testReconnect TestTransport{..} = do
734745

735746

736747
-- Simulate network failure
737-
liftIO $ testBreakConnection (nodeAddress nid1) (nodeAddress nid2)
748+
liftIO $ syncBreakConnection testBreakConnection node1 node2
738749

739750
-- This will happen due to implicit reconnect
740751
registerRemoteAsync nid1 "b" us
@@ -1388,3 +1399,17 @@ tests testtrans = return [
13881399
, testCase "Reconnect" (testReconnect testtrans)
13891400
]
13901401
]
1402+
1403+
syncBreakConnection :: (NT.EndPointAddress -> NT.EndPointAddress -> IO ()) -> LocalNode -> LocalNode -> IO ()
1404+
syncBreakConnection breakConnection nid0 nid1 = do
1405+
m <- newEmptyMVar
1406+
_ <- forkProcess nid1 $ getSelfPid >>= liftIO . putMVar m
1407+
runProcess nid0 $ do
1408+
them <- liftIO $ takeMVar m
1409+
pinger <- spawnLocal $ forever $ send them ()
1410+
_ <- monitorNode (localNodeId nid1)
1411+
liftIO $ breakConnection (nodeAddress $ localNodeId nid0)
1412+
(nodeAddress $ localNodeId nid1)
1413+
NodeMonitorNotification _ _ _ <- expect
1414+
kill pinger "finished"
1415+
return ()

tests/runInMemory.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ main = do
1616
(transport, internals) <- createTransportExposeInternals
1717
ts <- tests TestTransport
1818
{ testTransport = transport
19-
, testBreakConnection = \addr1 addr2 -> do breakConnection internals addr1 addr2 "user error"
20-
threadDelay 100000
19+
, testBreakConnection = \addr1 addr2 -> breakConnection internals addr1 addr2 "user error"
2120
}
2221
args <- getArgs
2322
-- Tests are time sensitive. Running the tests concurrently can slow them

0 commit comments

Comments
 (0)