|
1 | 1 | {-# LANGUAGE NumericUnderscores #-}
|
2 | 2 | {-# LANGUAGE LambdaCase #-}
|
| 3 | + |
3 | 4 | module Hedgehog.Extras.Test.TestWatchdogSpec where
|
4 | 5 |
|
5 | 6 | import Control.Concurrent
|
@@ -104,6 +105,53 @@ hprop_check_watchdog_kills_hanged_thread_with_its_children = H.propertyOnce $ do
|
104 | 105 | tailPid <- liftIO $ H.readMVar procHandle >>= P.getPid
|
105 | 106 | tailPid === Nothing
|
106 | 107 |
|
| 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 | + |
107 | 155 | assertWatchdogExceptionWasRaised :: HasCallStack
|
108 | 156 | => H.MonadTest m
|
109 | 157 | => MonadFail m
|
|
0 commit comments