@@ -5,6 +5,7 @@ module Control.Distributed.Process.Tests.Mx (tests) where
55
66import Control.Distributed.Process.Tests.Internal.Utils
77import Network.Transport.Test (TestTransport(..))
8+import Control.Exception (SomeException)
89import Control.Distributed.Process hiding (bracket, finally, try)
910import Control.Distributed.Process.Internal.Types
1011 ( ProcessExitException(..)
@@ -38,7 +39,7 @@ import Control.Monad.Catch(finally, bracket, try)
3839import Control.Rematch (equalTo)
3940import Data.Binary
4041import Data.List (find, sort, intercalate)
41−import Data.Maybe (isJust, fromJust, isNothing, fromMaybe, catMaybes)
42+import Data.Maybe (isJust, fromJust, isNothing, fromMaybe)
4243import Data.Typeable
4344import GHC.Generics hiding (from)
4445#if ! MIN_VERSION_base(4,6,0)
@@ -66,7 +67,7 @@ awaitExit pid =
6667 where
6768 withMonitorRef p = bracket (monitor p) unmonitor
6869
69−testAgentBroadcast :: TestResult (Maybe ()) -> Process ()
70+testAgentBroadcast :: TestResult () -> Process ()
7071testAgentBroadcast result = do
7172 (resultSP, resultRP) <- newChan :: Process (SendPort (), ReceivePort ())
7273
@@ -81,10 +82,10 @@ testAgentBroadcast result = do
8182 mxNotify ()
8283 -- Once the publisher has seen our message, it will broadcast the Publish
8384 -- and the consumer will see that and send the result to our typed channel.
84− stash result =<< receiveChanTimeout 10000000 resultRP
85+ stash result =<< receiveChan resultRP
8586
86− kill publisher "finished"
87− kill consumer "finished"
87+ kill publisher "finished" >> awaitExit publisher
88+ kill consumer "finished" >> awaitExit consumer
8889
8990testAgentDualInput :: TestResult (Maybe Int) -> Process ()
9091testAgentDualInput result = do
@@ -99,8 +100,6 @@ testAgentDualInput result = do
99100 else mxReady)
100101 ]
101102
102− mRef <- monitor s
103−
104103 mxNotify (1 :: Int)
105104 nsend "sum-agent" (3 :: Int)
106105 mxNotify (2 :: Int)
@@ -108,10 +107,7 @@ testAgentDualInput result = do
108107 mxNotify (5 :: Int)
109108
110109 stash result =<< receiveChanTimeout 10000000 rp
111− died <- receiveTimeout 10000000 [
112− matchIf (\(ProcessMonitorNotification r _ _) -> r == mRef) (const $ return True)
113− ]
114− died `shouldBe` equalTo (Just True)
110+ awaitExit s
115111
116112testAgentPrioritisation :: TestResult [String] -> Process ()
117113testAgentPrioritisation result = do
@@ -125,10 +121,10 @@ testAgentPrioritisation result = do
125121 (sp, rp) <- newChan
126122 s <- mxAgent (MxAgentId name) ["first"] [
127123 mxSink (\(s :: String) -> do
128− mxUpdateLocal (s:)
124+ mxUpdateLocal ((s:))
129125 st <- mxGetLocal
130126 case length st of
131− n | n == 5 -> do liftMX $ sendChan sp (sort st)
127+ n | n == 5 -> do liftMX $ sendChan sp st
132128 mxDeactivate "finished"
133129 _ -> mxReceive -- go to the mailbox
134130 )
@@ -139,7 +135,7 @@ testAgentPrioritisation result = do
139135 mxNotify "fourth"
140136 nsend name "fifth"
141137
142− stash result . fromMaybe [] =<< receiveChanTimeout 10000000 rp
138+ stash result . sort =<< receiveChan rp
143139 awaitExit s
144140
145141testAgentMailboxHandling :: TestResult (Maybe ()) -> Process ()
@@ -152,19 +148,12 @@ testAgentMailboxHandling result = do
152148 nsend "mailbox-agent" ()
153149
154150 stash result =<< receiveChanTimeout 1000000 rp
155− kill agent "finished"
151+ kill agent "finished" >> awaitExit agent
156152
157153testAgentEventHandling :: TestResult Bool -> Process ()
158154testAgentEventHandling result = do
159− us <- getSelfPid
160− -- because this test is a bit racy, let's ensure it can't run indefinitely
161− timer <- spawnLocal $ do
162− pause (1000000 * 60 * 5)
163− -- okay we've waited 5 mins, let's kill the test off if it's stuck...
164− stash result False
165− kill us "Test Timed Out"
166−
167155 let initState = [] :: [MxEvent]
156+
168157 (rc, rs) <- newChan
169158 agentPid <- mxAgent (MxAgentId "lifecycle-listener-agent") initState [
170159 (mxSink $ \ev -> do
@@ -197,7 +186,8 @@ testAgentEventHandling result = do
197186 -- TODO: yes, this is racy, but we're at the mercy of the scheduler here...
198187 faff 2000000 rs
199188
200− (sp, rp) <- newChan :: Process (SendPort (), ReceivePort ())
189+ _ <- monitor agentPid
190+ (sp, rp) <- newChan
201191
202192 pid <- spawnLocal $ expect >>= sendChan sp
203193
@@ -206,11 +196,9 @@ testAgentEventHandling result = do
206196 monitor pid
207197
208198 send pid ()
209− rct <- receiveChanTimeout 10000000 rp
210− unless (isJust rct) $ die "No response on channel"
199+ () <- receiveChan rp
211200
212− pmn <- receiveTimeout 2000000 [ match (\ProcessMonitorNotification{} -> return ()) ]
213− unless (isJust pmn) $ die "No monitor notification arrived"
201+ receiveWait [ match (\(ProcessMonitorNotification _ _ _) -> return ()) ]
214202
215203 -- TODO: yes, this is racy, but we're at the mercy of the scheduler here...
216204 faff 2000000 rs
@@ -219,16 +207,17 @@ testAgentEventHandling result = do
219207 mxNotify (MxSpawned pid, replyTo)
220208 mxNotify (MxProcessDied pid DiedNormal, replyTo)
221209
222− seenAlive <- receiveChanTimeout 10000000 reply
223− seenDead <- receiveChanTimeout 10000000 reply
210+ seenAlive <- receiveChan reply
211+ seenDead <- receiveChan reply
224212
225− stash result $ foldr (&&) True $ catMaybes [seenAlive, seenDead]
226− kill timer "test-complete"
227− kill agentPid "test-complete"
213+ stash result $ seenAlive && seenDead
214+ kill agentPid "test-complete" >> awaitExit agentPid
228215 where
229216 faff delay port = do
230217 res <- receiveChanTimeout delay port
231− unless (isNothing res) $ pause delay
218+ unless (isNothing res) $ do
219+ pause delay
220+ faff delay port
232221
233222testMxRegEvents :: TestResult () -> Process ()
234223testMxRegEvents result = do
@@ -275,6 +264,7 @@ testMxRegEvents result = do
275264 reg3 `shouldBe` equalTo (Just (label, p1))
276265
277266 mapM_ (flip kill $ "test-complete") [agent, p1, p2]
267+ awaitExit agent
278268
279269testMxRegMon :: LocalNode -> TestResult () -> Process ()
280270testMxRegMon remoteNode result = do
@@ -332,70 +322,70 @@ testMxRegMon remoteNode result = do
332322 evts `shouldContain` (Just (label1, p1))
333323 evts `shouldContain` (Just (label2, p1))
334324
335− kill agent "test-complete"
325+ kill agent "test-complete" >> awaitExit agent
336326
337327ensure :: Process () -> Process () -> Process ()
338328ensure = flip finally
339329
340330testNSend :: (String -> () -> Process ())
341331 -> Maybe LocalNode
332+ -> TestResult Bool
342333 -> Process ()
343−testNSend op n = do
344− us <- getSelfPid
334+testNSend op n r = testMxSend n r $ \p1 sink -> do
345335 let delay = 5000000
346− let label = "testMxSend" ++ (show us)
336+ let label = "testMxSend"
347337 let isValid = isValidLabel n label
348338
349− testMxSend n label $ \p1 sink -> do
350− register label p1
351− reg1 <- receiveChanTimeout delay sink
352− case reg1 of
353− Just (MxRegistered pd lb)
354− | pd == p1 && isValid lb -> return ()
355− _ -> die $ "Reg-Failed: " ++ show reg1
339+ register label p1
340+ reg1 <- receiveChanTimeout delay sink
341+ case reg1 of
342+ Just (MxRegistered pd lb)
343+ | pd == p1 && isValid lb -> return ()
344+ _ -> die $ "Reg-Failed: " ++ show reg1
356345
357− op label ()
346+ op label ()
358347
359− sent <- receiveChanTimeout delay sink
360− case sent of
361− Just (MxSentToName lb by _)
362− | by == us && isValid lb -> return True
363− _ -> die $ "Send-Failed: " ++ show sent
348+ us <- getSelfPid
349+ sent <- receiveChanTimeout delay sink
350+ case sent of
351+ Just (MxSentToName lb by _)
352+ | by == us && isValid lb -> return True
353+ _ -> die $ "Send-Failed: " ++ show sent
364354
365355 where
366− isValidLabel nd l1 l2
356+ isValidLabel n l1 l2
367357 | l2 == l2 = True
368− | isJust nd = l2 == l1 ++ "@" ++ show (localNodeId $ fromJust nd)
358+ | isJust n = l2 == l1 ++ "@" ++ show (localNodeId $ fromJust n)
369359 | otherwise = False
370360
371361testSend :: (ProcessId -> () -> Process ())
372362 -> Maybe LocalNode
363+ -> TestResult Bool
373364 -> Process ()
374−testSend op n = do
365+testSend op n r = testMxSend n r $ \p1 sink -> do
366+ -- initiate a send
367+ op p1 ()
368+
369+ -- verify the management event
375370 us <- getSelfPid
376− let label = "testMxSend" ++ (show us)
377− testMxSend n label $ \p1 sink -> do
378− -- initiate a send
379− op p1 ()
380−
381− -- verify the management event
382− sent <- receiveChanTimeout 5000000 sink
383− case sent of
384− Just (MxSent pidTo pidFrom _)
385− | pidTo == p1 && pidFrom == us -> return True
386− _ -> return False
371+ sent <- receiveChanTimeout 5000000 sink
372+ case sent of
373+ Just (MxSent pidTo pidFrom _)
374+ | pidTo == p1 && pidFrom == us -> return True
375+ _ -> return False
387376
388377testChan :: (SendPort () -> () -> Process ())
389378 -> Maybe LocalNode
379+ -> TestResult Bool
390380 -> Process ()
391−testChan op n = testMxSend n "" $ \p1 sink -> do
381+testChan op n r = testMxSend n r $ \p1 sink -> do
392382
393383 us <- getSelfPid
394384 send p1 us
395385
396− cleared <- receiveChanTimeout 2000000 sink
386+ cleared <- receiveChan sink
397387 case cleared of
398− Just (MxSent pidTo pidFrom _)
388+ (MxSent pidTo pidFrom _)
399389 | pidTo == p1 && pidFrom == us -> return ()
400390 _ -> die "Received uncleared Mx Event"
401391
@@ -414,8 +404,8 @@ testChan op n = testMxSend n "" $ \p1 sink -> do
414404
415405type SendTest = ProcessId -> ReceivePort MxEvent -> Process Bool
416406
417−testMxSend :: Maybe LocalNode -> String -> SendTest -> Process ()
418−testMxSend mNode label test = do
407+testMxSend :: Maybe LocalNode -> TestResult Bool -> SendTest -> Process ()
408+testMxSend mNode result test = do
419409 us <- getSelfPid
420410 (sp, rp) <- newChan
421411 (chan, sink) <- newChan
@@ -439,19 +429,16 @@ testMxSend mNode label test = do
439429 Nothing -> void $ spawnLocal (proc sp)
440430 Just remoteNode -> void $ liftIO $ forkProcess remoteNode $ proc sp
441431
442− p1 <- receiveChanTimeout 2000000 rp
443− unless (isJust p1) $ die "Timed out waiting for ProcessId"
444− res <- try $ test (fromJust p1) sink
445−
446− kill agent "bye"
447− kill (fromJust p1) "bye"
448−
432+ p1 <- receiveChan rp
433+ res <- try (test p1 sink)
449434 case res of
450− Left (ProcessExitException _ m) -> (liftIO $ putStrLn $ "SomeException-" ++ show m) >> die m
451− Right tr -> tr `shouldBe` equalTo True
452−
435+ Left (ProcessExitException _ m) -> (liftIO $ putStrLn $ "SomeException-" ++ show m) >> stash result False
436+ Right tr -> stash result tr
437+ kill agent "bye"
438+ kill p1 "bye"
453439
454440 where
441+ label = "testMxSend"
455442 agentLabel s = "mx-unsafe-check-agent-" ++ show s
456443 proc sp' = getSelfPid >>= sendChan sp' >> go Nothing
457444
@@ -477,7 +464,7 @@ tests TestTransport{..} = do
477464 , testCase "InterAgentBroadcast"
478465 (delayedAssertion
479466 "expected (), but no broadcast was received"
480− node1 (Just ()) testAgentBroadcast)
467+ node1 () testAgentBroadcast)
481468 , testCase "AgentMailboxHandling"
482469 (delayedAssertion
483470 "expected (Just ()), but no regular (mailbox) input was handled"
@@ -502,11 +489,12 @@ tests TestTransport{..} = do
502489 (delayedAssertion
503490 "expected process deaths to result in unregistration events"
504491 node1 () (testMxRegMon node2))
505− , testGroup "SendEvents" $ buildTestCases node1 node2
492+ -- , testGroup "SendEvents" $ buildTestCases node1 node2
506493 ]
507494 ]
508495 where
509− buildTestCases n1 n2 = let nid = localNodeId n2 in build n1 n2 [
496+ buildTestCases n1 n2 =
497+ let nid = localNodeId n2 in build n2 [
510498 ("NSend", [
511499 ("nsend", testNSend nsend)
512500 , ("Unsafe.nsend", testNSend Unsafe.nsend)
@@ -528,13 +516,13 @@ tests TestTransport{..} = do
528516 ]
529517
530518 build :: LocalNode
531− -> LocalNode
532− -> [(String, [(String, (Maybe LocalNode -> Process ()))])]
519+ -> [(String, [(String, (Maybe LocalNode -> TestResult Bool -> Process ()))])]
533520 -> [Test]
534− build n ln specs =
521+ build ln specs =
535522 [ testGroup (intercalate "-" [groupName, caseSuffix]) [
536523 testCase (intercalate "-" [caseName, caseSuffix])
537− (runProcess n (caseImpl caseNode))
524+ (delayedAssertion "expected mx events failed"
525+ ln True (caseImpl caseNode))
538526 | (caseName, caseImpl) <- groupCases
539527 ]
540528 | (groupName, groupCases) <- specs