@@ -3,9 +3,16 @@ module Control.Distributed.Process.Tests.Mx (tests) where
4import Control.Distributed.Process.Tests.Internal.Utils
4import Control.Distributed.Process.Tests.Internal.Utils
5import Network.Transport.Test (TestTransport(..))
5import Network.Transport.Test (TestTransport(..))
6−
6+import Control.Exception (SomeException)
7−import Control.Distributed.Process
7+import Control.Distributed.Process hiding (bracket, finally, try)
8+import Control.Distributed.Process.Internal.Types (ProcessExitException(..))
8import Control.Distributed.Process.Node
9import Control.Distributed.Process.Node
10+import qualified Control.Distributed.Process.UnsafePrimitives as Unsafe
9import Control.Distributed.Process.Management
16import Control.Distributed.Process.Management
10 ( MxEvent(..)
17 ( MxEvent(..)
11 , MxAgentId(..)
18 , MxAgentId(..)
@@ -22,12 +29,13 @@ import Control.Distributed.Process.Management
22 , mxBroadcast
29 , mxBroadcast
24import Control.Monad (void, unless)
31import Control.Monad (void, unless)
32+import Control.Monad.Catch(finally, bracket, try)
25import Control.Rematch (equalTo)
33import Control.Rematch (equalTo)
26import Data.Binary
34import Data.Binary
27import Data.List (find, sort)
35import Data.List (find, sort)
28import Data.Maybe (isJust, isNothing)
36import Data.Maybe (isJust, isNothing)
29import Data.Typeable
37import Data.Typeable
30−import GHC.Generics
38+import GHC.Generics hiding (from)
31#if ! MIN_VERSION_base(4,6,0)
39#if ! MIN_VERSION_base(4,6,0)
32import Prelude hiding (catch, log)
40import Prelude hiding (catch, log)
@@ -51,7 +59,7 @@ awaitExit pid =
51 (\_ -> return ())
59 (\_ -> return ())
54− withMonitorRef pid = bracket (monitor pid) unmonitor
62+ withMonitorRef p = bracket (monitor p) unmonitor
56testAgentBroadcast :: TestResult () -> Process ()
64testAgentBroadcast :: TestResult () -> Process ()
57testAgentBroadcast result = do
65testAgentBroadcast result = do
@@ -313,43 +321,163 @@ testMxRegMon remoteNode result = do
313ensure :: Process () -> Process () -> Process ()
321ensure :: Process () -> Process () -> Process ()
314ensure = flip finally
322ensure = flip finally
324+type SendTest = ProcessId -> ReceivePort MxEvent -> Process Bool
326+testNSend :: (String -> () -> Process ()) -> Maybe LocalNode -> TestResult Bool -> Process ()
327+testNSend op n r = testMxSend n r $ \p1 sink -> do
329+ let label = "testMxSend"
332+ reg1 <- receiveChanTimeout delay sink
334+ Just (MxRegistered pd lb)
335+ | pd == p1 && lb == label -> return ()
336+ _ -> die $ "Reg-Failed: " ++ show reg1
341+ sent <- receiveChanTimeout delay sink
343+ Just (MxSentToName lb by _)
344+ | by == us && lb == label -> return True
345+ _ -> die $ "Send-Failed: " ++ show sent
347+testSend :: (ProcessId -> () -> Process ()) -> Maybe LocalNode -> TestResult Bool -> Process ()
348+testSend op n r = testMxSend n r $ \p1 sink -> do
352+ -- verify the management event
354+ sent <- receiveChanTimeout 5000000 sink
356+ Just (MxSent pidTo pidFrom _)
357+ | pidTo == p1 && pidFrom == us -> return True
360+testMxSend :: Maybe LocalNode -> TestResult Bool -> SendTest -> Process ()
361+testMxSend mNode result test = do
363+ (chan, sink) <- newChan
364+ agent <- mxAgent (MxAgentId $ agentLabel us) () [
367+ m@(MxSent _ fromPid _)
368+ | fromPid == us -> liftMX $ sendChan chan m
369+ m@(MxSentToName _ fromPid _)
370+ | fromPid == us -> liftMX $ sendChan chan m
371+ m@(MxRegistered _ name)
372+ | name == label -> liftMX $ sendChan chan m
379+ Nothing -> void $ spawnLocal (proc sp)
380+ Just remoteNode -> void $ liftIO $ forkProcess remoteNode $ proc sp
382+ p1 <- receiveChan rp
383+ res <- try (test p1 sink)
385+ Left (ProcessExitException _ m) -> (liftIO $ putStrLn $ "SomeException-" ++ show m) >> stash result False
386+ Right tr -> stash result tr
391+ label = "testMxSend"
392+ agentLabel s = "mx-unsafe-check-agent-" ++ show s
393+ proc sp' = getSelfPid >>= sendChan sp' >> expect :: Process ()
316tests :: TestTransport -> IO [Test]
395tests :: TestTransport -> IO [Test]
317tests TestTransport{..} = do
396tests TestTransport{..} = do
318 node1 <- newLocalNode testTransport initRemoteTable
397 node1 <- newLocalNode testTransport initRemoteTable
319 node2 <- newLocalNode testTransport initRemoteTable
398 node2 <- newLocalNode testTransport initRemoteTable
399+ let nid = localNodeId node2
321− testGroup "Mx Agents" [
401+ testGroup "MxAgents" [
322− testCase "Event Handling"
402+ testCase "EventHandling"
323 (delayedAssertion
403 (delayedAssertion
324 "expected True, but events where not as expected"
404 "expected True, but events where not as expected"
325 node1 True testAgentEventHandling)
405 node1 True testAgentEventHandling)
326− , testCase "Inter-Agent Broadcast"
406+ , testCase "InterAgentBroadcast"
327 (delayedAssertion
407 (delayedAssertion
328 "expected (), but no broadcast was received"
408 "expected (), but no broadcast was received"
329 node1 () testAgentBroadcast)
409 node1 () testAgentBroadcast)
330− , testCase "Agent Mailbox Handling"
410+ , testCase "AgentMailboxHandling"
331 (delayedAssertion
411 (delayedAssertion
332 "expected (Just ()), but no regular (mailbox) input was handled"
412 "expected (Just ()), but no regular (mailbox) input was handled"
333 node1 (Just ()) testAgentMailboxHandling)
413 node1 (Just ()) testAgentMailboxHandling)
334− , testCase "Agent Dual Input Handling"
414+ , testCase "AgentDualInputHandling"
335 (delayedAssertion
415 (delayedAssertion
336 "expected sum = 15, but the result was Nothing"
416 "expected sum = 15, but the result was Nothing"
337 node1 (Just 15 :: Maybe Int) testAgentDualInput)
417 node1 (Just 15 :: Maybe Int) testAgentDualInput)
338− , testCase "Agent Input Prioritisation"
418+ , testCase "AgentInputPrioritisation"
339 (delayedAssertion
419 (delayedAssertion
340 "expected [first, second, third, fourth, fifth], but result diverged"
420 "expected [first, second, third, fourth, fifth], but result diverged"
341 node1 (sort ["first", "second",
421 node1 (sort ["first", "second",
342 "third", "fourth",
422 "third", "fourth",
343 "fifth"]) testAgentPrioritisation)
423 "fifth"]) testAgentPrioritisation)
345− , testGroup "Mx Events" [
425+ , testGroup "MxEvents" [
346− testCase "Name Registration Events"
426+ testCase "NameRegistrationEvents"
347 (delayedAssertion
427 (delayedAssertion
348 "expected registration events to map to the correct ProcessId"
428 "expected registration events to map to the correct ProcessId"
349 node1 () testMxRegEvents)
429 node1 () testMxRegEvents)
350− , testCase "Post Death Name UnRegistration Events"
430+ , testCase "PostDeathNameUnRegistrationEvents"
351 (delayedAssertion
431 (delayedAssertion
352 "expected process deaths to result in unregistration events"
432 "expected process deaths to result in unregistration events"
353 node1 () (testMxRegMon node2))
433 node1 () (testMxRegMon node2))
434+ , testGroup "SentEvents" [
435+ testGroup "RemoteTargets" [
436+ testCase "Unsafe.nsend"
437+ (delayedAssertion "expected mx events failed"
438+ node1 True (testNSend Unsafe.nsend $ Just node2))
439+ , testCase "Unsafe.nsendRemote"
440+ (delayedAssertion "expected mx events failed"
441+ node1 True (testNSend (Unsafe.nsendRemote nid) $ Just node2))
442+ , testCase "Unsafe.send"
443+ (delayedAssertion "expected mx events failed"
444+ node1 True (testSend Unsafe.send $ Just node2))
445+ , testCase "Unsafe.usend"
446+ (delayedAssertion "expected mx events failed"
447+ node1 True (testSend Unsafe.usend $ Just node2))
449+ (delayedAssertion "expected mx events failed"
450+ node1 True (testNSend nsend $ Just node2))
451+ , testCase "nsendRemote"
452+ (delayedAssertion "expected mx events failed"
453+ node1 True (testNSend (nsendRemote nid) $ Just node2))
455+ (delayedAssertion "expected mx events failed"
456+ node1 True (testSend send $ Just node2))
458+ (delayedAssertion "expected mx events failed"
459+ node1 True (testSend usend $ Just node2))
461+ , testGroup "LocalTargets" [
462+ testCase "Unsafe.nsend"
463+ (delayedAssertion "expected mx events failed"
464+ node1 True (testNSend Unsafe.nsend Nothing))
465+ , testCase "Unsafe.send"
466+ (delayedAssertion "expected mx events failed"
467+ node1 True (testSend Unsafe.send Nothing))
468+ , testCase "Unsafe.usend"
469+ (delayedAssertion "expected mx events failed"
470+ node1 True (testSend Unsafe.usend Nothing))
472+ (delayedAssertion "expected mx events failed"
473+ node1 True (testNSend nsend Nothing))
475+ (delayedAssertion "expected mx events failed"
476+ node1 True (testSend send Nothing))
478+ (delayedAssertion "expected mx events failed"
479+ node1 True (testSend usend Nothing))