|
1 | | -{-# LANGUAGE DeriveGeneric #-} |
2 | | -{-# LANGUAGE ParallelListComp #-} |
| 1 | +{-# LANGUAGE DeriveGeneric #-} |
| 2 | +{-# LANGUAGE LambdaCase #-} |
| 3 | +{-# LANGUAGE NumericUnderscores #-} |
3 | 4 |
|
4 | 5 | module Control.Distributed.Process.Tests.Mx (tests) where |
5 | 6 |
|
| 7 | +import Control.Concurrent.MVar (newEmptyMVar, putMVar, tryTakeMVar) |
| 8 | +import Control.Concurrent (threadDelay) |
6 | 9 | import Control.Distributed.Process.Tests.Internal.Utils |
7 | 10 | import Network.Transport.Test (TestTransport(..)) |
8 | 11 | import Control.Distributed.Process hiding (bracket, finally, try) |
@@ -36,16 +39,15 @@ import Control.Distributed.Process.Management |
36 | 39 | import Control.Monad (void, unless) |
37 | 40 | import Control.Monad.Catch(finally, bracket, try) |
38 | 41 | 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) |
42 | 44 | import GHC.Generics hiding (from) |
43 | 45 |
|
44 | 46 | import Test.Tasty (TestTree, testGroup) |
45 | 47 | import Test.Tasty.HUnit (assertBool, assertEqual, testCase) |
46 | 48 |
|
47 | 49 | data Publish = Publish |
48 | | - deriving (Typeable, Generic, Eq) |
| 50 | + deriving (Generic, Eq) |
49 | 51 |
|
50 | 52 | instance Binary Publish where |
51 | 53 |
|
@@ -147,81 +149,125 @@ testAgentMailboxHandling result = do |
147 | 149 | stash result =<< receiveChanTimeout 1000000 rp |
148 | 150 | kill agent "finished" |
149 | 151 |
|
| 152 | + |
150 | 153 | testAgentEventHandling :: TestResult Bool -> Process () |
151 | 154 | 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) |
159 | 158 |
|
160 | 159 | 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 | | - ] |
189 | 160 |
|
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 |
192 | 168 |
|
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 |
194 | 177 |
|
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 ()) |
196 | 201 |
|
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 () |
200 | 203 |
|
| 204 | + ref <- monitor pid |
201 | 205 | send pid () |
202 | | - rct <- receiveChanTimeout 10000000 rp |
203 | | - unless (isJust rct) $ die "No response on channel" |
204 | 206 |
|
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 | + |
207 | 242 |
|
208 | | - -- TODO: yes, this is racy, but we're at the mercy of the scheduler here... |
209 | | - faff 2000000 rs |
210 | 243 |
|
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 |
214 | 245 |
|
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 | + ] |
217 | 268 |
|
218 | | - stash result $ foldr (&&) True $ catMaybes [seenAlive, seenDead] |
219 | | - kill timer "test-complete" |
220 | 269 | kill agentPid "test-complete" |
221 | | - where |
222 | | - faff delay port = do |
223 | | - res <- receiveChanTimeout delay port |
224 | | - unless (isNothing res) $ pause delay |
| 270 | + |
225 | 271 |
|
226 | 272 | testMxRegEvents :: TestResult () -> Process () |
227 | 273 | testMxRegEvents result = do |
@@ -262,7 +308,7 @@ testMxRegEvents result = do |
262 | 308 |
|
263 | 309 | reregister label p1 |
264 | 310 | unreg2 <- receiveChanTimeout delay unRegSink |
265 | | - liftIO $ assertEqual mempty (Just (label, p2)) unreg2 |
| 311 | + liftIO $ assertEqual mempty (Just (label, p2)) unreg2 |
266 | 312 |
|
267 | 313 | reg3 <- receiveChanTimeout delay regSink |
268 | 314 | liftIO $ assertEqual mempty (Just (label, p1)) reg3 |
@@ -301,7 +347,7 @@ testMxRegMon remoteNode result = do |
301 | 347 |
|
302 | 348 | register label1 p1 |
303 | 349 | reg1 <- receiveChanTimeout delay regSink |
304 | | - liftIO $ assertEqual mempty (Just (label1, p1)) reg1 |
| 350 | + liftIO $ assertEqual mempty (Just (label1, p1)) reg1 |
305 | 351 |
|
306 | 352 | register label2 p1 |
307 | 353 | reg2 <- receiveChanTimeout delay regSink |
|
0 commit comments