Skip to content

Commit 489c2cb

Browse files
committed
Remove timeout dependent tests in Closure tests.
1 parent b346151 commit 489c2cb

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

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

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import Network.Transport.Test (TestTransport(..))
66
import Data.ByteString.Lazy (empty)
77
import Data.IORef
88
import Data.Typeable (Typeable)
9-
import Control.Monad (join, replicateM, forever, replicateM_, void, when)
9+
import Data.Maybe
10+
import Control.Monad (join, replicateM, forever, replicateM_, void, when, unless)
1011
import Control.Exception (IOException, throw)
1112
import Control.Concurrent (forkIO, threadDelay)
1213
import Control.Concurrent.MVar
@@ -63,6 +64,9 @@ sendPid toPid = do
6364
wait :: Int -> Process ()
6465
wait = liftIO . threadDelay
6566

67+
expectUnit :: Process ()
68+
expectUnit = expect
69+
6670
isPrime :: Integer -> Process Bool
6771
isPrime n = return . (n `elem`) . takeWhile (<= n) . sieve $ [2..]
6872
where
@@ -85,6 +89,7 @@ remotable [ 'factorial
8589
, 'sendPid
8690
, 'sdictInt
8791
, 'wait
92+
, 'expectUnit
8893
, 'typedPingServer
8994
, 'isPrime
9095
, 'quintuple
@@ -141,11 +146,19 @@ factorial' n = returnCP $(mkStatic 'sdictInt) n `bindCP` factorialOf
141146
waitClosure :: Int -> Closure (Process ())
142147
waitClosure = $(mkClosure 'wait)
143148

144-
simulateNetworkFailure :: TestTransport -> NodeId -> NodeId -> Process ()
149+
simulateNetworkFailure :: TestTransport -> LocalNode -> LocalNode -> Process ()
145150
simulateNetworkFailure TestTransport{..} from to = liftIO $ do
146-
threadDelay 10000
147-
testBreakConnection (nodeAddress from) (nodeAddress to)
148-
threadDelay 10000
151+
m <- newEmptyMVar
152+
_ <- forkProcess to $ getSelfPid >>= liftIO . putMVar m
153+
runProcess from $ do
154+
them <- liftIO $ takeMVar m
155+
pinger <- spawnLocal $ forever $ send them ()
156+
_ <- monitorNode (localNodeId to)
157+
liftIO $ testBreakConnection (nodeAddress $ localNodeId from)
158+
(nodeAddress $ localNodeId to)
159+
NodeMonitorNotification _ _ _ <- expect
160+
kill pinger "finished"
161+
return ()
149162

150163
--------------------------------------------------------------------------------
151164
-- The tests proper --
@@ -363,19 +376,35 @@ testSpawnSupervised TestTransport{..} rtable = do
363376
[node1, node2] <- replicateM 2 $ newLocalNode testTransport rtable
364377
[superPid, childPid] <- replicateM 2 $ newEmptyMVar
365378
thirdProcessDone <- newEmptyMVar
379+
linkUp <- newEmptyMVar
366380

367381
forkProcess node1 $ do
368382
us <- getSelfPid
369383
liftIO $ putMVar superPid us
370-
(child, _ref) <- spawnSupervised (localNodeId node2) (waitClosure 1000000)
371-
liftIO $ do
372-
putMVar childPid child
373-
threadDelay 500000 -- Give the child a chance to link to us
374-
throw supervisorDeath
384+
(child, _ref) <- spawnSupervised (localNodeId node2)
385+
(sendPidClosure us `seqCP` $(mkStaticClosure 'expectUnit))
386+
_ <- expect :: Process ProcessId
387+
388+
liftIO $ do putMVar childPid child
389+
-- Give the child a chance to link to us
390+
takeMVar linkUp
391+
throw supervisorDeath
375392

376393
forkProcess node2 $ do
377394
[super, child] <- liftIO $ mapM readMVar [superPid, childPid]
378395
ref <- monitor child
396+
self <- getSelfPid
397+
let waitForMOrL = do
398+
liftIO $ threadDelay 10000
399+
mpinfo <- getProcessInfo child
400+
case mpinfo of
401+
Nothing -> waitForMOrL
402+
Just pinfo ->
403+
unless (isJust $ lookup self (infoMonitors pinfo)) waitForMOrL
404+
waitForMOrL
405+
liftIO $ putMVar linkUp ()
406+
-- because monitor message was sent before message to process
407+
-- we hope that it will be processed before
379408
ProcessMonitorNotification ref' pid' (DiedException e) <- expect
380409
True <- return $ ref' == ref
381410
&& pid' == child
@@ -465,7 +494,7 @@ testSpawnReconnect testtrans@TestTransport{..} rtable = do
465494

466495
forkProcess node2 $ do
467496
_pid1 <- spawn nid1 ($(mkClosure 'signal) incr)
468-
simulateNetworkFailure testtrans nid2 nid1
497+
simulateNetworkFailure testtrans node2 node1
469498
_pid2 <- spawn nid1 ($(mkClosure 'signal) incr)
470499
_pid3 <- spawn nid1 ($(mkClosure 'signal) incr)
471500

0 commit comments

Comments
 (0)