@@ -3,9 +3,16 @@ module Control.Distributed.Process.Tests.Mx (tests) where
33
44import Control.Distributed.Process.Tests.Internal.Utils
55import Network.Transport.Test (TestTransport(..))
6−
7−import Control.Distributed.Process
6+import Control.Exception (SomeException)
7+import Control.Distributed.Process hiding (bracket, finally, try)
8+import Control.Distributed.Process.Internal.Types (ProcessExitException(..))
89import Control.Distributed.Process.Node
10+import qualified Control.Distributed.Process.UnsafePrimitives as Unsafe
11+ ( send
12+ , nsend
13+ , nsendRemote
14+ , usend
15+ )
916import Control.Distributed.Process.Management
1017 ( MxEvent(..)
1118 , MxAgentId(..)
@@ -22,12 +29,13 @@ import Control.Distributed.Process.Management
2229 , mxBroadcast
2330 )
2431import Control.Monad (void, unless)
32+import Control.Monad.Catch(finally, bracket, try)
2533import Control.Rematch (equalTo)
2634import Data.Binary
2735import Data.List (find, sort)
28−import Data.Maybe (isJust, isNothing)
36+import Data.Maybe (isJust, fromJust, isNothing)
2937import Data.Typeable
30−import GHC.Generics
38+import GHC.Generics hiding (from)
3139#if ! MIN_VERSION_base(4,6,0)
3240import Prelude hiding (catch, log)
3341#endif
@@ -51,7 +59,7 @@ awaitExit pid =
5159 (\_ -> return ())
5260 ]
5361 where
54− withMonitorRef pid = bracket (monitor pid) unmonitor
62+ withMonitorRef p = bracket (monitor p) unmonitor
5563
5664testAgentBroadcast :: TestResult () -> Process ()
5765testAgentBroadcast result = do
@@ -313,43 +321,173 @@ testMxRegMon remoteNode result = do
313321ensure :: Process () -> Process () -> Process ()
314322ensure = flip finally
315323
324+type SendTest = ProcessId -> ReceivePort MxEvent -> Process Bool
325+
326+testNSend :: (String -> () -> Process ())
327+ -> Maybe LocalNode
328+ -> TestResult Bool
329+ -> Process ()
330+testNSend op n r = testMxSend n r $ \p1 sink -> do
331+ let delay = 5000000
332+ let label = "testMxSend"
333+ let isValid = isValidLabel n label
334+
335+ register label p1
336+ reg1 <- receiveChanTimeout delay sink
337+ case reg1 of
338+ Just (MxRegistered pd lb)
339+ | pd == p1 && isValid lb -> return ()
340+ _ -> die $ "Reg-Failed: " ++ show reg1
341+
342+ op label ()
343+
344+ us <- getSelfPid
345+ sent <- receiveChanTimeout delay sink
346+ case sent of
347+ Just (MxSentToName lb by _)
348+ | by == us && isValid lb -> return True
349+ _ -> die $ "Send-Failed: " ++ show sent
350+
351+ where
352+ isValidLabel n l1 l2
353+ | l2 == l2 = True
354+ | isJust n = l2 == l1 ++ "@" ++ show (localNodeId $ fromJust n)
355+ | otherwise = False
356+
357+testSend :: (ProcessId -> () -> Process ()) -> Maybe LocalNode -> TestResult Bool -> Process ()
358+testSend op n r = testMxSend n r $ \p1 sink -> do
359+ -- initiate a send
360+ op p1 ()
361+
362+ -- verify the management event
363+ us <- getSelfPid
364+ sent <- receiveChanTimeout 5000000 sink
365+ case sent of
366+ Just (MxSent pidTo pidFrom _)
367+ | pidTo == p1 && pidFrom == us -> return True
368+ _ -> return False
369+
370+testMxSend :: Maybe LocalNode -> TestResult Bool -> SendTest -> Process ()
371+testMxSend mNode result test = do
372+ us <- getSelfPid
373+ (chan, sink) <- newChan
374+ agent <- mxAgent (MxAgentId $ agentLabel us) () [
375+ mxSink $ \ev -> do
376+ case ev of
377+ m@(MxSent _ fromPid _)
378+ | fromPid == us -> liftMX $ sendChan chan m
379+ m@(MxSentToName _ fromPid _)
380+ | fromPid == us -> liftMX $ sendChan chan m
381+ m@(MxRegistered _ name)
382+ | name == label -> liftMX $ sendChan chan m
383+ _ -> return ()
384+ mxReady
385+ ]
386+
387+ (sp, rp) <- newChan
388+ case mNode of
389+ Nothing -> void $ spawnLocal (proc sp)
390+ Just remoteNode -> void $ liftIO $ forkProcess remoteNode $ proc sp
391+
392+ p1 <- receiveChan rp
393+ res <- try (test p1 sink)
394+ case res of
395+ Left (ProcessExitException _ m) -> (liftIO $ putStrLn $ "SomeException-" ++ show m) >> stash result False
396+ Right tr -> stash result tr
397+ kill agent "bye"
398+ kill p1 "bye"
399+
400+ where
401+ label = "testMxSend"
402+ agentLabel s = "mx-unsafe-check-agent-" ++ show s
403+ proc sp' = getSelfPid >>= sendChan sp' >> expect :: Process ()
404+
316405tests :: TestTransport -> IO [Test]
317406tests TestTransport{..} = do
318407 node1 <- newLocalNode testTransport initRemoteTable
319408 node2 <- newLocalNode testTransport initRemoteTable
409+ let nid = localNodeId node2
320410 return [
321− testGroup "Mx Agents" [
322− testCase "Event Handling"
411+ testGroup "MxAgents" [
412+ testCase "EventHandling"
323413 (delayedAssertion
324414 "expected True, but events where not as expected"
325415 node1 True testAgentEventHandling)
326− , testCase "Inter-Agent Broadcast"
416+ , testCase "InterAgentBroadcast"
327417 (delayedAssertion
328418 "expected (), but no broadcast was received"
329419 node1 () testAgentBroadcast)
330− , testCase "Agent Mailbox Handling"
420+ , testCase "AgentMailboxHandling"
331421 (delayedAssertion
332422 "expected (Just ()), but no regular (mailbox) input was handled"
333423 node1 (Just ()) testAgentMailboxHandling)
334− , testCase "Agent Dual Input Handling"
424+ , testCase "AgentDualInputHandling"
335425 (delayedAssertion
336426 "expected sum = 15, but the result was Nothing"
337427 node1 (Just 15 :: Maybe Int) testAgentDualInput)
338− , testCase "Agent Input Prioritisation"
428+ , testCase "AgentInputPrioritisation"
339429 (delayedAssertion
340430 "expected [first, second, third, fourth, fifth], but result diverged"
341431 node1 (sort ["first", "second",
342432 "third", "fourth",
343433 "fifth"]) testAgentPrioritisation)
344434 ]
345− , testGroup "Mx Events" [
346− testCase "Name Registration Events"
435+ , testGroup "MxEvents" [
436+ testCase "NameRegistrationEvents"
347437 (delayedAssertion
348438 "expected registration events to map to the correct ProcessId"
349439 node1 () testMxRegEvents)
350− , testCase "Post Death Name UnRegistration Events"
440+ , testCase "PostDeathNameUnRegistrationEvents"
351441 (delayedAssertion
352442 "expected process deaths to result in unregistration events"
353443 node1 () (testMxRegMon node2))
444+ , testGroup "SentEvents" [
445+ testGroup "RemoteTargets" [
446+ testCase "Unsafe.nsend"
447+ (delayedAssertion "expected mx events failed"
448+ node1 True (testNSend Unsafe.nsend $ Just node2))
449+ , testCase "Unsafe.nsendRemote"
450+ (delayedAssertion "expected mx events failed"
451+ node1 True (testNSend (Unsafe.nsendRemote nid) $ Just node2))
452+ , testCase "Unsafe.send"
453+ (delayedAssertion "expected mx events failed"
454+ node1 True (testSend Unsafe.send $ Just node2))
455+ , testCase "Unsafe.usend"
456+ (delayedAssertion "expected mx events failed"
457+ node1 True (testSend Unsafe.usend $ Just node2))
458+ , testCase "nsend"
459+ (delayedAssertion "expected mx events failed"
460+ node1 True (testNSend nsend $ Just node2))
461+ , testCase "nsendRemote"
462+ (delayedAssertion "expected mx events failed"
463+ node1 True (testNSend (nsendRemote nid) $ Just node2))
464+ , testCase "send"
465+ (delayedAssertion "expected mx events failed"
466+ node1 True (testSend send $ Just node2))
467+ , testCase "usend"
468+ (delayedAssertion "expected mx events failed"
469+ node1 True (testSend usend $ Just node2))
470+ ]
471+ , testGroup "LocalTargets" [
472+ testCase "Unsafe.nsend"
473+ (delayedAssertion "expected mx events failed"
474+ node1 True (testNSend Unsafe.nsend Nothing))
475+ , testCase "Unsafe.send"
476+ (delayedAssertion "expected mx events failed"
477+ node1 True (testSend Unsafe.send Nothing))
478+ , testCase "Unsafe.usend"
479+ (delayedAssertion "expected mx events failed"
480+ node1 True (testSend Unsafe.usend Nothing))
481+ , testCase "nsend"
482+ (delayedAssertion "expected mx events failed"
483+ node1 True (testNSend nsend Nothing))
484+ , testCase "send"
485+ (delayedAssertion "expected mx events failed"
486+ node1 True (testSend send Nothing))
487+ , testCase "usend"
488+ (delayedAssertion "expected mx events failed"
489+ node1 True (testSend usend Nothing))
490+ ]
491+ ]
492+ ]
354493 ]
355− ]