Skip to content

Commit e435869

Browse files
carbolymernewhoggy
authored andcommitted
Add test for asyncRegister_
1 parent ed8060a commit e435869

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

test/Hedgehog/Extras/Test/TestWatchdogSpec.hs

+48
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE NumericUnderscores #-}
22
{-# LANGUAGE LambdaCase #-}
3+
34
module Hedgehog.Extras.Test.TestWatchdogSpec where
45

56
import Control.Concurrent
@@ -104,6 +105,53 @@ hprop_check_watchdog_kills_hanged_thread_with_its_children = H.propertyOnce $ do
104105
tailPid <- liftIO $ H.readMVar procHandle >>= P.getPid
105106
tailPid === Nothing
106107

108+
hprop_check_asyncRegister_finishes_with_test :: Property
109+
hprop_check_asyncRegister_finishes_with_test = H.propertyOnce $ do
110+
childTid <- H.newEmptyMVar
111+
grandChildTid1 <- H.newEmptyMVar
112+
grandChildTid2 <- H.newEmptyMVar
113+
childTripwire <- H.makeTripwire
114+
grandChildTripwire1 <- H.makeTripwire
115+
grandChildTripwire2 <- H.makeTripwire
116+
117+
-- test that asyncRegister_ gets killed when the main thread finishes
118+
(result, _) <- spawnTestT $ do
119+
liftIO $ myThreadId >>= H.putMVar childTid
120+
121+
H.asyncRegister_ $ do
122+
liftIO $ myThreadId >>= H.putMVar grandChildTid1
123+
threadDelay 10_000_000
124+
H.trip_ grandChildTripwire1
125+
126+
H.asyncRegister_ $ do
127+
liftIO $ myThreadId >>= H.putMVar grandChildTid2
128+
threadDelay 10_000_000
129+
H.trip_ grandChildTripwire2
130+
131+
H.trip_ childTripwire
132+
133+
result === Right ()
134+
-- double check that main thread finished successfully
135+
H.assertTripped childTripwire
136+
H.assertNotTripped grandChildTripwire1
137+
H.assertNotTripped grandChildTripwire2
138+
139+
-- Give OS 5 seconds to do the process cleanup
140+
deadline <- D.addUTCTime 5 <$> liftIO D.getCurrentTime
141+
142+
H.byDeadlineM 0.2 deadline "childStatus" $ do
143+
childStatus <- liftIO $ H.readMVar childTid >>= threadStatus
144+
childStatus === ThreadFinished
145+
146+
H.byDeadlineM 0.2 deadline "grandChildStatus1" $ do
147+
grandChildStatus1 <- liftIO $ H.readMVar grandChildTid1 >>= threadStatus
148+
grandChildStatus1 === ThreadFinished
149+
150+
H.byDeadlineM 0.2 deadline "grandChildStatus2" $ do
151+
grandChildStatus2 <- liftIO $ H.readMVar grandChildTid2 >>= threadStatus
152+
grandChildStatus2 === ThreadFinished
153+
154+
107155
assertWatchdogExceptionWasRaised :: HasCallStack
108156
=> H.MonadTest m
109157
=> MonadFail m

0 commit comments

Comments
 (0)