@@ -45,6 +45,16 @@ data Publish = Publish
45
45
46
46
instance Binary Publish where
47
47
48
+ awaitExit :: ProcessId -> Process ()
49
+ awaitExit pid =
50
+ withMonitorRef pid $ \ ref -> do
51
+ receiveWait
52
+ [ matchIf (\ (ProcessMonitorNotification r _ _) -> r == ref)
53
+ (\ _ -> return () )
54
+ ]
55
+ where
56
+ withMonitorRef pid = bracket (P. monitor pid) P. unmonitor
57
+
48
58
testAgentBroadcast :: TestResult () -> Process ()
49
59
testAgentBroadcast result = do
50
60
(resultSP, resultRP) <- newChan :: Process (SendPort () , ReceivePort () )
@@ -62,13 +72,13 @@ testAgentBroadcast result = do
62
72
-- and the consumer will see that and send the result to our typed channel.
63
73
stash result =<< receiveChan resultRP
64
74
65
- kill publisher " finished"
66
- kill consumer " finished"
75
+ kill publisher " finished" >>= awaitExit publisher
76
+ kill consumer " finished" >>= awaitExit consumer
67
77
68
78
testAgentDualInput :: TestResult (Maybe Int ) -> Process ()
69
79
testAgentDualInput result = do
70
80
(sp, rp) <- newChan
71
- _ <- mxAgent (MxAgentId " sum-agent" ) (0 :: Int ) [
81
+ s <- mxAgent (MxAgentId " sum-agent" ) (0 :: Int ) [
72
82
mxSink $ (\ (i :: Int ) -> do
73
83
mxSetLocal . (+ i) =<< mxGetLocal
74
84
i' <- mxGetLocal
@@ -85,6 +95,7 @@ testAgentDualInput result = do
85
95
mxNotify (5 :: Int )
86
96
87
97
stash result =<< receiveChanTimeout 10000000 rp
98
+ awaitExit s
88
99
89
100
testAgentPrioritisation :: TestResult [String ] -> Process ()
90
101
testAgentPrioritisation result = do
@@ -96,8 +107,8 @@ testAgentPrioritisation result = do
96
107
97
108
let name = " prioritising-agent"
98
109
(sp, rp) <- newChan
99
- void $ mxAgent (MxAgentId name) [" first" ] [
100
- mxSink (\ (s :: String ) -> do
110
+ s <- mxAgent (MxAgentId name) [" first" ] [
111
+ mxSink (\ (s :: String ) -> do
101
112
mxUpdateLocal ((s: ))
102
113
st <- mxGetLocal
103
114
case length st of
@@ -113,6 +124,7 @@ testAgentPrioritisation result = do
113
124
nsend name " fifth"
114
125
115
126
stash result . sort =<< receiveChan rp
127
+ awaitExit s
116
128
117
129
testAgentMailboxHandling :: TestResult (Maybe () ) -> Process ()
118
130
testAgentMailboxHandling result = do
@@ -124,7 +136,7 @@ testAgentMailboxHandling result = do
124
136
nsend " mailbox-agent" ()
125
137
126
138
stash result =<< receiveChanTimeout 1000000 rp
127
- kill agent " finished"
139
+ kill agent " finished" >>= awaitExit agent
128
140
129
141
testAgentEventHandling :: TestResult Bool -> Process ()
130
142
testAgentEventHandling result = do
@@ -175,6 +187,7 @@ testAgentEventHandling result = do
175
187
seenDead <- receiveChan reply
176
188
177
189
stash result $ seenAlive && seenDead
190
+ kill agentPid " test-complete" >>= awaitExit agentPid
178
191
179
192
testMxRegEvents :: Process ()
180
193
testMxRegEvents = do
@@ -222,6 +235,7 @@ testMxRegEvents = do
222
235
reg3 `shouldBe` equalTo (Just (label, p1))
223
236
224
237
mapM_ (flip kill $ " test-complete" ) [agent, p1, p2]
238
+ awaitExit agent
225
239
226
240
testMxRegMon :: LocalNode -> Process ()
227
241
testMxRegMon remoteNode = do
@@ -280,7 +294,7 @@ testMxRegMon remoteNode = do
280
294
evts `shouldContain` (Just (label1, p1))
281
295
evts `shouldContain` (Just (label2, p1))
282
296
283
- kill agent " test-complete"
297
+ kill agent " test-complete" >>= awaitExit agent
284
298
285
299
tests :: TestTransport -> IO [Test ]
286
300
tests TestTransport {.. } = do
0 commit comments