Skip to content

Commit d2e825b

Browse files
committed
Address flaky event handling MxAgent test
1 parent aea7740 commit d2e825b

1 file changed

Lines changed: 113 additions & 67 deletions

File tree

  • packages/distributed-process-tests/src/Control/Distributed/Process/Tests

‎packages/distributed-process-tests/src/Control/Distributed/Process/Tests/Mx.hs‎

Lines changed: 113 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
{-# LANGUAGE DeriveGeneric #-}
2-
{-# LANGUAGE ParallelListComp #-}
1+
{-# LANGUAGE DeriveGeneric #-}
2+
{-# LANGUAGE LambdaCase #-}
3+
{-# LANGUAGE NumericUnderscores #-}
34

45
module Control.Distributed.Process.Tests.Mx (tests) where
56

7+
import Control.Concurrent.MVar (newEmptyMVar, putMVar, tryTakeMVar)
8+
import Control.Concurrent (threadDelay)
69
import Control.Distributed.Process.Tests.Internal.Utils
710
import Network.Transport.Test (TestTransport(..))
811
import Control.Distributed.Process hiding (bracket, finally, try)
@@ -36,16 +39,15 @@ import Control.Distributed.Process.Management
3639
import Control.Monad (void, unless)
3740
import Control.Monad.Catch(finally, bracket, try)
3841
import Data.Binary
39-
import Data.List (find, sort, intercalate)
40-
import Data.Maybe (isJust, fromJust, isNothing, fromMaybe, catMaybes)
41-
import Data.Typeable
42+
import Data.List (sort, intercalate)
43+
import Data.Maybe (isJust, fromJust, fromMaybe)
4244
import GHC.Generics hiding (from)
4345

4446
import Test.Tasty (TestTree, testGroup)
4547
import Test.Tasty.HUnit (assertBool, assertEqual, testCase)
4648

4749
data Publish = Publish
48-
deriving (Typeable, Generic, Eq)
50+
deriving (Generic, Eq)
4951

5052
instance Binary Publish where
5153

@@ -147,81 +149,125 @@ testAgentMailboxHandling result = do
147149
stash result =<< receiveChanTimeout 1000000 rp
148150
kill agent "finished"
149151

152+
150153
testAgentEventHandling :: TestResult Bool -> Process ()
151154
testAgentEventHandling result = do
152-
us <- getSelfPid
153-
-- because this test is a bit racy, let's ensure it can't run indefinitely
154-
timer <- spawnLocal $ do
155-
pause (1000000 * 60 * 5)
156-
-- okay we've waited 5 mins, let's kill the test off if it's stuck...
157-
stash result False
158-
kill us "Test Timed Out"
155+
let timeout = 1_000_000
156+
157+
(ackSP, ackRP) <- newChan :: Process (SendPort MxEvent, ReceivePort MxEvent)
159158

160159
let initState = [] :: [MxEvent]
161-
(rc, rs) <- newChan
162-
agentPid <- mxAgent (MxAgentId "lifecycle-listener-agent") initState [
163-
(mxSink $ \ev -> do
164-
st <- mxGetLocal
165-
let act =
166-
case ev of
167-
(MxSpawned _) -> mxSetLocal (ev:st)
168-
(MxProcessDied _ _) -> mxSetLocal (ev:st)
169-
_ -> return ()
170-
act >> (liftMX $ sendChan rc ()) >> mxReady),
171-
(mxSink $ \(ev, sp :: SendPort Bool) -> do
172-
st <- mxGetLocal
173-
let found =
174-
case ev of
175-
MxSpawned p ->
176-
isJust $ find (\ev' ->
177-
case ev' of
178-
(MxSpawned p') -> p' == p
179-
_ -> False) st
180-
MxProcessDied p r ->
181-
isJust $ find (\ev' ->
182-
case ev' of
183-
(MxProcessDied p' r') -> p' == p && r == r'
184-
_ -> False) st
185-
_ -> False
186-
liftMX $ sendChan sp found
187-
mxReady)
188-
]
189160

190-
-- TODO: yes, this is racy, but we're at the mercy of the scheduler here...
191-
faff 2000000 rs
161+
agentPid <- mxAgent (MxAgentId "lifecycle-listener-agent") initState [
162+
mxSink $ \ev -> do
163+
case ev of
164+
MxSpawned _ -> do
165+
st <- mxGetLocal
166+
mxSetLocal (ev : st)
167+
liftMX $ sendChan ackSP ev
192168

193-
(sp, rp) <- newChan :: Process (SendPort (), ReceivePort ())
169+
MxProcessDied _ _ -> do
170+
st <- mxGetLocal
171+
mxSetLocal (ev : st)
172+
liftMX $ sendChan ackSP ev
173+
174+
_ -> pure ()
175+
176+
mxReady
194177

195-
pid <- spawnLocal $ expect >>= sendChan sp
178+
, mxSink $ \(ev, sp :: SendPort Bool) -> do
179+
st <- mxGetLocal
180+
181+
let found =
182+
case ev of
183+
MxSpawned p ->
184+
any (\case
185+
MxSpawned p' -> p' == p
186+
_ -> False) st
187+
188+
MxProcessDied p r ->
189+
any (\case
190+
MxProcessDied p' r' ->
191+
p' == p && r' == r
192+
_ -> False) st
193+
194+
_ -> False
195+
196+
liftMX $ sendChan sp found
197+
mxReady
198+
]
199+
200+
(sp, rp) <- newChan :: Process (SendPort (), ReceivePort ())
196201

197-
-- By waiting for a monitor notification, we have a
198-
-- higher probably that the agent has seen the spawn and died events
199-
monitor pid
202+
pid <- spawnLocal $ (expect :: Process ()) >> sendChan sp ()
200203

204+
ref <- monitor pid
201205
send pid ()
202-
rct <- receiveChanTimeout 10000000 rp
203-
unless (isJust rct) $ die "No response on channel"
204206

205-
pmn <- receiveTimeout 2000000 [ match (\ProcessMonitorNotification{} -> return ()) ]
206-
unless (isJust pmn) $ die "No monitor notification arrived"
207+
completed <- receiveChanTimeout timeout rp
208+
terminated <- receiveTimeout timeout
209+
[ matchIf
210+
(\(ProcessMonitorNotification r _ _) -> r == ref)
211+
(\_ -> pure ())
212+
]
213+
214+
takingTooLong <- liftIO newEmptyMVar
215+
216+
spawnLocal . liftIO $ do
217+
threadDelay timeout
218+
putMVar takingTooLong ()
219+
220+
-- race conditions are eliminated by awaiting exactly the events
221+
let awaitEvents seenSpawn seenDeath
222+
| seenSpawn && seenDeath = pure True
223+
| otherwise =
224+
liftIO (tryTakeMVar takingTooLong) >>= \case
225+
-- Awaiting is taking too long
226+
Just () -> pure False
227+
Nothing -> do
228+
next <- receiveChanTimeout timeout ackRP
229+
case next of
230+
Nothing -> pure False
231+
232+
Just ev ->
233+
case ev of
234+
MxSpawned p
235+
| p == pid -> awaitEvents True seenDeath
236+
237+
MxProcessDied p DiedNormal
238+
| p == pid -> awaitEvents seenSpawn True
239+
240+
_ -> awaitEvents seenSpawn seenDeath
241+
207242

208-
-- TODO: yes, this is racy, but we're at the mercy of the scheduler here...
209-
faff 2000000 rs
210243

211-
(replyTo, reply) <- newChan :: Process (SendPort Bool, ReceivePort Bool)
212-
mxNotify (MxSpawned pid, replyTo)
213-
mxNotify (MxProcessDied pid DiedNormal, replyTo)
244+
observed <- awaitEvents False False
214245

215-
seenAlive <- receiveChanTimeout 10000000 reply
216-
seenDead <- receiveChanTimeout 10000000 reply
246+
(seenAlive, seenDead) <-
247+
if observed
248+
then do
249+
(replyTo, reply) <- newChan :: Process (SendPort Bool, ReceivePort Bool)
250+
251+
mxNotify (MxSpawned pid, replyTo)
252+
mxNotify (MxProcessDied pid DiedNormal, replyTo)
253+
254+
alive <- receiveChanTimeout timeout reply
255+
dead <- receiveChanTimeout timeout reply
256+
257+
return (alive, dead)
258+
259+
else return (Nothing, Nothing)
260+
261+
stash result $
262+
and [ completed == Just ()
263+
, isJust terminated
264+
, observed
265+
, seenAlive == Just True
266+
, seenDead == Just True
267+
]
217268

218-
stash result $ foldr (&&) True $ catMaybes [seenAlive, seenDead]
219-
kill timer "test-complete"
220269
kill agentPid "test-complete"
221-
where
222-
faff delay port = do
223-
res <- receiveChanTimeout delay port
224-
unless (isNothing res) $ pause delay
270+
225271

226272
testMxRegEvents :: TestResult () -> Process ()
227273
testMxRegEvents result = do
@@ -262,7 +308,7 @@ testMxRegEvents result = do
262308

263309
reregister label p1
264310
unreg2 <- receiveChanTimeout delay unRegSink
265-
liftIO $ assertEqual mempty (Just (label, p2)) unreg2
311+
liftIO $ assertEqual mempty (Just (label, p2)) unreg2
266312

267313
reg3 <- receiveChanTimeout delay regSink
268314
liftIO $ assertEqual mempty (Just (label, p1)) reg3
@@ -301,7 +347,7 @@ testMxRegMon remoteNode result = do
301347

302348
register label1 p1
303349
reg1 <- receiveChanTimeout delay regSink
304-
liftIO $ assertEqual mempty (Just (label1, p1)) reg1
350+
liftIO $ assertEqual mempty (Just (label1, p1)) reg1
305351

306352
register label2 p1
307353
reg2 <- receiveChanTimeout delay regSink

0 commit comments

Comments
 (0)