@@ -23,10 +23,11 @@ import Control.Distributed.Process.Management
23 , mxBroadcast
23 , mxBroadcast
26−import Control.Monad (void)
26+import Control.Monad (void, unless)
27+import Control.Rematch (equalTo)
27import Data.Binary
28import Data.Binary
28import Data.List (find, sort)
29import Data.List (find, sort)
29−import Data.Maybe (isJust)
30+import Data.Maybe (isJust, isNothing)
30import Data.Typeable
31import Data.Typeable
31import GHC.Generics
32import GHC.Generics
32#if ! MIN_VERSION_base(4,6,0)
33#if ! MIN_VERSION_base(4,6,0)
@@ -44,6 +45,16 @@ data Publish = Publish
45instance Binary Publish where
46instance Binary Publish where
48+awaitExit :: ProcessId -> Process ()
50+ withMonitorRef pid $ \ref -> do
52+ [ matchIf (\(ProcessMonitorNotification r _ _) -> r == ref)
56+ withMonitorRef pid = bracket (monitor pid) unmonitor
47testAgentBroadcast :: TestResult () -> Process ()
58testAgentBroadcast :: TestResult () -> Process ()
48testAgentBroadcast result = do
59testAgentBroadcast result = do
49 (resultSP, resultRP) <- newChan :: Process (SendPort (), ReceivePort ())
60 (resultSP, resultRP) <- newChan :: Process (SendPort (), ReceivePort ())
@@ -61,13 +72,13 @@ testAgentBroadcast result = do
61 -- and the consumer will see that and send the result to our typed channel.
72 -- and the consumer will see that and send the result to our typed channel.
62 stash result =<< receiveChan resultRP
73 stash result =<< receiveChan resultRP
64− kill publisher "finished"
75+ kill publisher "finished" >> awaitExit publisher
65− kill consumer "finished"
76+ kill consumer "finished" >> awaitExit consumer
67testAgentDualInput :: TestResult (Maybe Int) -> Process ()
78testAgentDualInput :: TestResult (Maybe Int) -> Process ()
68testAgentDualInput result = do
79testAgentDualInput result = do
69 (sp, rp) <- newChan
80 (sp, rp) <- newChan
70− _ <- mxAgent (MxAgentId "sum-agent") (0 :: Int) [
81+ s <- mxAgent (MxAgentId "sum-agent") (0 :: Int) [
71 mxSink $ (\(i :: Int) -> do
82 mxSink $ (\(i :: Int) -> do
72 mxSetLocal . (+i) =<< mxGetLocal
83 mxSetLocal . (+i) =<< mxGetLocal
73 i' <- mxGetLocal
84 i' <- mxGetLocal
@@ -84,6 +95,7 @@ testAgentDualInput result = do
84 mxNotify (5 :: Int)
95 mxNotify (5 :: Int)
86 stash result =<< receiveChanTimeout 10000000 rp
97 stash result =<< receiveChanTimeout 10000000 rp
88testAgentPrioritisation :: TestResult [String] -> Process ()
100testAgentPrioritisation :: TestResult [String] -> Process ()
89testAgentPrioritisation result = do
101testAgentPrioritisation result = do
@@ -95,8 +107,8 @@ testAgentPrioritisation result = do
96 let name = "prioritising-agent"
108 let name = "prioritising-agent"
97 (sp, rp) <- newChan
109 (sp, rp) <- newChan
98− void $ mxAgent (MxAgentId name) ["first"] [
110+ s <- mxAgent (MxAgentId name) ["first"] [
99− mxSink (\(s :: String) -> do
111+ mxSink (\(s :: String) -> do
100 mxUpdateLocal ((s:))
112 mxUpdateLocal ((s:))
101 st <- mxGetLocal
113 st <- mxGetLocal
102 case length st of
114 case length st of
@@ -112,22 +124,25 @@ testAgentPrioritisation result = do
112 nsend name "fifth"
124 nsend name "fifth"
114 stash result . sort =<< receiveChan rp
126 stash result . sort =<< receiveChan rp
116testAgentMailboxHandling :: TestResult (Maybe ()) -> Process ()
129testAgentMailboxHandling :: TestResult (Maybe ()) -> Process ()
117testAgentMailboxHandling result = do
130testAgentMailboxHandling result = do
118 (sp, rp) <- newChan
131 (sp, rp) <- newChan
119− agent <- mxAgent (MxAgentId "listener-agent") () [
132+ agent <- mxAgent (MxAgentId "mailbox-agent") () [
120 mxSink $ \() -> (liftMX $ sendChan sp ()) >> mxReady
133 mxSink $ \() -> (liftMX $ sendChan sp ()) >> mxReady
123− nsend "listener-agent" ()
136+ nsend "mailbox-agent" ()
125 stash result =<< receiveChanTimeout 1000000 rp
138 stash result =<< receiveChanTimeout 1000000 rp
126− kill agent "finished"
139+ kill agent "finished" >> awaitExit agent
128testAgentEventHandling :: TestResult Bool -> Process ()
141testAgentEventHandling :: TestResult Bool -> Process ()
129testAgentEventHandling result = do
142testAgentEventHandling result = do
130 let initState = [] :: [MxEvent]
143 let initState = [] :: [MxEvent]
131 agentPid <- mxAgent (MxAgentId "lifecycle-listener-agent") initState [
146 agentPid <- mxAgent (MxAgentId "lifecycle-listener-agent") initState [
132 (mxSink $ \ev -> do
147 (mxSink $ \ev -> do
133 st <- mxGetLocal
148 st <- mxGetLocal
@@ -136,7 +151,7 @@ testAgentEventHandling result = do
136 (MxSpawned _) -> mxSetLocal (ev:st)
151 (MxSpawned _) -> mxSetLocal (ev:st)
137 (MxProcessDied _ _) -> mxSetLocal (ev:st)
152 (MxProcessDied _ _) -> mxSetLocal (ev:st)
138 _ -> return ()
153 _ -> return ()
139− act >> mxReady),
154+ act >> (liftMX $ sendChan rc ()) >> mxReady),
140 (mxSink $ \(ev, sp :: SendPort Bool) -> do
155 (mxSink $ \(ev, sp :: SendPort Bool) -> do
141 st <- mxGetLocal
156 st <- mxGetLocal
142 let found =
157 let found =
@@ -156,16 +171,26 @@ testAgentEventHandling result = do
174+ -- TODO: yes, this is racy, but we're at the mercy of the scheduler here...
159 _ <- monitor agentPid
177 _ <- monitor agentPid
160 (sp, rp) <- newChan
178 (sp, rp) <- newChan
161− pid <- spawnLocal $ sendChan sp ()
180+ pid <- spawnLocal $ expect >>= sendChan sp
162− () <- receiveChan rp
164 -- By waiting for a monitor notification, we have a
182 -- By waiting for a monitor notification, we have a
165− -- higher probably that the agent has seen
183+ -- higher probably that the agent has seen the spawn and died events
166 monitor pid
184 monitor pid
187+ () <- receiveChan rp
167 receiveWait [ match (\(ProcessMonitorNotification _ _ _) -> return ()) ]
189 receiveWait [ match (\(ProcessMonitorNotification _ _ _) -> return ()) ]
191+ -- TODO: yes, this is racy, but we're at the mercy of the scheduler here...
169 (replyTo, reply) <- newChan :: Process (SendPort Bool, ReceivePort Bool)
194 (replyTo, reply) <- newChan :: Process (SendPort Bool, ReceivePort Bool)
170 mxNotify (MxSpawned pid, replyTo)
195 mxNotify (MxSpawned pid, replyTo)
171 mxNotify (MxProcessDied pid DiedNormal, replyTo)
196 mxNotify (MxProcessDied pid DiedNormal, replyTo)
@@ -174,32 +199,159 @@ testAgentEventHandling result = do
174 seenDead <- receiveChan reply
199 seenDead <- receiveChan reply
176 stash result $ seenAlive && seenDead
201 stash result $ seenAlive && seenDead
202+ kill agentPid "test-complete" >> awaitExit agentPid
204+ faff delay port = do
205+ res <- receiveChanTimeout delay port
206+ unless (isNothing res) $ do
210+testMxRegEvents :: TestResult () -> Process ()
211+testMxRegEvents result = do
212+ {- This test only deals with the local case, to ensure that we are being
213+ notified in the expected order - the remote cases related to the
214+ behaviour of the node controller are contained in the CH test suite. -}
215+ ensure (stash result ()) $ do
216+ let label = "testMxRegEvents"
217+ let agentLabel = "mxRegEvents-agent"
219+ (regChan, regSink) <- newChan
220+ (unRegChan, unRegSink) <- newChan
221+ agent <- mxAgent (MxAgentId agentLabel) () [
224+ MxRegistered pid label'
225+ | label' == label -> liftMX $ sendChan regChan (label', pid)
226+ MxUnRegistered pid label'
227+ | label' == label -> liftMX $ sendChan unRegChan (label', pid)
232+ p1 <- spawnLocal expect
233+ p2 <- spawnLocal expect
236+ reg1 <- receiveChanTimeout delay regSink
237+ reg1 `shouldBe` equalTo (Just (label, p1))
240+ unreg1 <- receiveChanTimeout delay unRegSink
241+ unreg1 `shouldBe` equalTo (Just (label, p1))
244+ reg2 <- receiveChanTimeout delay regSink
245+ reg2 `shouldBe` equalTo (Just (label, p2))
248+ unreg2 <- receiveChanTimeout delay unRegSink
249+ unreg2 `shouldBe` equalTo (Just (label, p2))
251+ reg3 <- receiveChanTimeout delay regSink
252+ reg3 `shouldBe` equalTo (Just (label, p1))
254+ mapM_ (flip kill $ "test-complete") [agent, p1, p2]
257+testMxRegMon :: LocalNode -> TestResult () -> Process ()
258+testMxRegMon remoteNode result = do
259+ ensure (stash result ()) $ do
260+ -- ensure that when a registered process dies, we get a notification that
261+ -- it has been unregistered as well as seeing the name get removed
262+ let label1 = "aaaaa"
263+ let label2 = "bbbbb"
264+ let isValid l = l == label1 || l == label2
265+ let agentLabel = "mxRegMon-agent"
267+ (regChan, regSink) <- newChan
268+ (unRegChan, unRegSink) <- newChan
269+ agent <- mxAgent (MxAgentId agentLabel) () [
272+ MxRegistered pid label
273+ | isValid label -> liftMX $ sendChan regChan (label, pid)
274+ MxUnRegistered pid label
275+ | isValid label -> liftMX $ sendChan unRegChan (label, pid)
281+ liftIO $ forkProcess remoteNode $ do
282+ getSelfPid >>= sendChan sp
283+ expect :: Process ()
285+ p1 <- receiveChan rp
288+ reg1 <- receiveChanTimeout delay regSink
289+ reg1 `shouldBe` equalTo (Just (label1, p1))
292+ reg2 <- receiveChanTimeout delay regSink
293+ reg2 `shouldBe` equalTo (Just (label2, p1))
295+ n1 <- whereis label1
296+ n1 `shouldBe` equalTo (Just p1)
298+ n2 <- whereis label2
299+ n2 `shouldBe` equalTo (Just p1)
303+ unreg1 <- receiveChanTimeout delay unRegSink
304+ unreg2 <- receiveChanTimeout delay unRegSink
306+ let evts = [unreg1, unreg2]
307+ -- we can't rely on the order of the values in the node controller's
308+ -- map (it's either racy to do so, or no such guarantee exists for Data.Map),
309+ -- so we simply verify that we received the un-registration events we expect
310+ evts `shouldContain` (Just (label1, p1))
311+ evts `shouldContain` (Just (label2, p1))
313+ kill agent "test-complete" >> awaitExit agent
315+ensure :: Process () -> Process () -> Process ()
316+ensure = flip finally
178tests :: TestTransport -> IO [Test]
318tests :: TestTransport -> IO [Test]
179tests TestTransport{..} = do
319tests TestTransport{..} = do
180 node1 <- newLocalNode testTransport initRemoteTable
320 node1 <- newLocalNode testTransport initRemoteTable
321+ node2 <- newLocalNode testTransport initRemoteTable
182− testGroup "Mx Agents" [
323+ testGroup "Mx Agents" [
183− testCase "Event Handling"
324+ testCase "Event Handling"
184− (delayedAssertion
325+ (delayedAssertion
185− "expected True, but events where not as expected"
326+ "expected True, but events where not as expected"
186− node1 True testAgentEventHandling)
327+ node1 True testAgentEventHandling)
187− , testCase "Inter-Agent Broadcast"
328+ , testCase "Inter-Agent Broadcast"
188− (delayedAssertion
329+ (delayedAssertion
189− "expected (), but no broadcast was received"
330+ "expected (), but no broadcast was received"
190− node1 () testAgentBroadcast)
331+ node1 () testAgentBroadcast)
191− , testCase "Agent Mailbox Handling"
332+ , testCase "Agent Mailbox Handling"
192− (delayedAssertion
333+ (delayedAssertion
193− "expected (Just ()), but no regular (mailbox) input was handled"
334+ "expected (Just ()), but no regular (mailbox) input was handled"
194− node1 (Just ()) testAgentMailboxHandling)
335+ node1 (Just ()) testAgentMailboxHandling)
195− , testCase "Agent Dual Input Handling"
336+ , testCase "Agent Dual Input Handling"
196− (delayedAssertion
337+ (delayedAssertion
197− "expected sum = 15, but the result was Nothing"
338+ "expected sum = 15, but the result was Nothing"
198− node1 (Just 15 :: Maybe Int) testAgentDualInput)
339+ node1 (Just 15 :: Maybe Int) testAgentDualInput)
199− , testCase "Agent Input Prioritisation"
340+ , testCase "Agent Input Prioritisation"
200− (delayedAssertion
341+ (delayedAssertion
201− "expected [first, second, third, fourth, fifth], but result diverged"
342+ "expected [first, second, third, fourth, fifth], but result diverged"
202− node1 (sort ["first", "second",
343+ node1 (sort ["first", "second",
203− "third", "fourth",
344+ "third", "fourth",
204− "fifth"]) testAgentPrioritisation)
345+ "fifth"]) testAgentPrioritisation)
347+ , testGroup "Mx Events" [
348+ testCase "Name Registration Events"
350+ "expected registration events to map to the correct ProcessId"
351+ node1 () testMxRegEvents)
352+ , testCase "Post Death Name UnRegistration Events"
354+ "expected process deaths to result in unregistration events"
355+ node1 () (testMxRegMon node2))