@@ -6,7 +6,8 @@ import Network.Transport.Test (TestTransport(..))
6
6
import Data.ByteString.Lazy (empty )
7
7
import Data.IORef
8
8
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 )
10
11
import Control.Exception (IOException , throw )
11
12
import Control.Concurrent (forkIO , threadDelay )
12
13
import Control.Concurrent.MVar
@@ -63,6 +64,9 @@ sendPid toPid = do
63
64
wait :: Int -> Process ()
64
65
wait = liftIO . threadDelay
65
66
67
+ expectUnit :: Process ()
68
+ expectUnit = expect
69
+
66
70
isPrime :: Integer -> Process Bool
67
71
isPrime n = return . (n `elem` ) . takeWhile (<= n) . sieve $ [2 .. ]
68
72
where
@@ -85,6 +89,7 @@ remotable [ 'factorial
85
89
, 'sendPid
86
90
, 'sdictInt
87
91
, 'wait
92
+ , 'expectUnit
88
93
, 'typedPingServer
89
94
, 'isPrime
90
95
, 'quintuple
@@ -141,11 +146,19 @@ factorial' n = returnCP $(mkStatic 'sdictInt) n `bindCP` factorialOf
141
146
waitClosure :: Int -> Closure (Process () )
142
147
waitClosure = $ (mkClosure 'wait)
143
148
144
- simulateNetworkFailure :: TestTransport -> NodeId -> NodeId -> Process ()
149
+ simulateNetworkFailure :: TestTransport -> LocalNode -> LocalNode -> Process ()
145
150
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 ()
149
162
150
163
--------------------------------------------------------------------------------
151
164
-- The tests proper --
@@ -363,19 +376,35 @@ testSpawnSupervised TestTransport{..} rtable = do
363
376
[node1, node2] <- replicateM 2 $ newLocalNode testTransport rtable
364
377
[superPid, childPid] <- replicateM 2 $ newEmptyMVar
365
378
thirdProcessDone <- newEmptyMVar
379
+ linkUp <- newEmptyMVar
366
380
367
381
forkProcess node1 $ do
368
382
us <- getSelfPid
369
383
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
375
392
376
393
forkProcess node2 $ do
377
394
[super, child] <- liftIO $ mapM readMVar [superPid, childPid]
378
395
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
379
408
ProcessMonitorNotification ref' pid' (DiedException e) <- expect
380
409
True <- return $ ref' == ref
381
410
&& pid' == child
@@ -465,7 +494,7 @@ testSpawnReconnect testtrans@TestTransport{..} rtable = do
465
494
466
495
forkProcess node2 $ do
467
496
_pid1 <- spawn nid1 ($ (mkClosure 'signal) incr)
468
- simulateNetworkFailure testtrans nid2 nid1
497
+ simulateNetworkFailure testtrans node2 node1
469
498
_pid2 <- spawn nid1 ($ (mkClosure 'signal) incr)
470
499
_pid3 <- spawn nid1 ($ (mkClosure 'signal) incr)
471
500
0 commit comments