@@ -23,11 +23,11 @@ import Control.Distributed.Process.Management
2323 , mxBroadcast
2424 , mxGetId
2525 )
26−import Control.Monad (void)
26+import Control.Monad (void, unless)
2727import Control.Rematch (equalTo)
2828import Data.Binary
2929import Data.List (find, sort)
30−import Data.Maybe (isJust)
30+import Data.Maybe (isJust, isNothing)
3131import Data.Typeable
3232import GHC.Generics
3333#if ! MIN_VERSION_base(4,6,0)
@@ -141,6 +141,8 @@ testAgentMailboxHandling result = do
141141testAgentEventHandling :: TestResult Bool -> Process ()
142142testAgentEventHandling result = do
143143 let initState = [] :: [MxEvent]
144+
145+ (rc, rs) <- newChan
144146 agentPid <- mxAgent (MxAgentId "lifecycle-listener-agent") initState [
145147 (mxSink $ \ev -> do
146148 st <- mxGetLocal
@@ -149,7 +151,7 @@ testAgentEventHandling result = do
149151 (MxSpawned _) -> mxSetLocal (ev:st)
150152 (MxProcessDied _ _) -> mxSetLocal (ev:st)
151153 _ -> return ()
152− act >> mxReady),
154+ act >> (liftMX $ sendChan rc ()) >> mxReady),
153155 (mxSink $ \(ev, sp :: SendPort Bool) -> do
154156 st <- mxGetLocal
155157 let found =
@@ -169,16 +171,26 @@ testAgentEventHandling result = do
169171 mxReady)
170172 ]
171173
174+ -- TODO: yes, this is racy, but we're at the mercy of the scheduler here...
175+ faff 2000000 rs
176+
172177 _ <- monitor agentPid
173178 (sp, rp) <- newChan
174− pid <- spawnLocal $ sendChan sp ()
175− () <- receiveChan rp
179+
180+ pid <- spawnLocal $ expect >>= sendChan sp
176181
177182 -- By waiting for a monitor notification, we have a
178− -- higher probably that the agent has seen
183+ -- higher probably that the agent has seen the spawn and died events
179184 monitor pid
185+
186+ send pid ()
187+ () <- receiveChan rp
188+
180189 receiveWait [ match (\(ProcessMonitorNotification _ _ _) -> return ()) ]
181190
191+ -- TODO: yes, this is racy, but we're at the mercy of the scheduler here...
192+ faff 2000000 rs
193+
182194 (replyTo, reply) <- newChan :: Process (SendPort Bool, ReceivePort Bool)
183195 mxNotify (MxSpawned pid, replyTo)
184196 mxNotify (MxProcessDied pid DiedNormal, replyTo)
@@ -188,6 +200,12 @@ testAgentEventHandling result = do
188200
189201 stash result $ seenAlive && seenDead
190202 kill agentPid "test-complete" >> awaitExit agentPid
203+ where
204+ faff delay port = do
205+ res <- receiveChanTimeout delay port
206+ unless (isNothing res) $ do
207+ pause delay
208+ faff delay port
191209
192210testMxRegEvents :: TestResult () -> Process ()
193211testMxRegEvents result = do