|
1 | 1 | {-# LANGUAGE NumericUnderscores #-}
|
2 | 2 | module Hedgehog.Extras.Test.TestWatchdogSpec where
|
3 | 3 |
|
| 4 | +import Control.Concurrent |
| 5 | +import Control.Monad |
| 6 | +import Control.Monad.IO.Class |
| 7 | +import Control.Monad.Trans.Except (runExceptT) |
| 8 | +import Control.Monad.Trans.Resource (ResourceT, runResourceT) |
| 9 | +import Control.Monad.Trans.Writer.Lazy (runWriterT) |
4 | 10 | import Data.Function
|
5 | 11 | import Data.Semigroup
|
6 |
| -import Hedgehog (Property) |
| 12 | +import GHC.Conc |
| 13 | +import GHC.Stack |
| 14 | +import Hedgehog (Property, (===)) |
7 | 15 | import qualified Hedgehog as H
|
8 | 16 | import qualified Hedgehog.Extras as H
|
9 | 17 | import qualified Hedgehog.Extras.Stock.IO.Network.Port as IO
|
10 |
| -import Hedgehog.Extras.Test.TestWatchdog |
11 | 18 | import qualified Hedgehog.Extras.Test.Concurrent as H
|
| 19 | +import Hedgehog.Extras.Test.TestWatchdog |
12 | 20 | import qualified Hedgehog.Internal.Property as H
|
13 |
| -import Control.Monad.Trans.Writer.Lazy (runWriterT) |
14 |
| -import Control.Monad.Trans.Except (runExceptT) |
15 | 21 | import qualified Network.Socket as N
|
| 22 | +import Prelude |
16 | 23 | import Text.Show
|
17 |
| -import Control.Monad |
18 |
| -import Prelude |
19 |
| -import Control.Monad.IO.Class |
20 |
| -import GHC.Stack |
21 |
| -import Control.Concurrent |
22 |
| -import GHC.Conc |
| 24 | +import qualified System.Process as P |
23 | 25 |
|
24 | 26 | hprop_check_watchdog_kills_hanged_thread :: Property
|
25 | 27 | hprop_check_watchdog_kills_hanged_thread = H.propertyOnce $ do
|
26 | 28 | let watchdogCfg = WatchdogConfig 1
|
27 |
| - childTid <- liftIO $ newEmptyMVar |
28 |
| - (res, log') <- runTestT $ runWithWatchdog_ watchdogCfg $ do |
29 |
| - liftIO $ myThreadId >>= putMVar childTid |
30 |
| - void . forever $ H.threadDelay 1_000_000 |
31 |
| - H.success |
| 29 | + childTid <- H.newEmptyMVar |
| 30 | + tripwire <- H.makeTripwire |
| 31 | + |
| 32 | + _ <- spawnTestT $ runWithWatchdog_ watchdogCfg $ do |
| 33 | + liftIO $ myThreadId >>= H.putMVar childTid |
| 34 | + -- simulate thread hang |
| 35 | + void $ H.threadDelay 3_000_000 |
| 36 | + H.triggerTripwire tripwire |
| 37 | + |
| 38 | + -- make sure that we accidentally didn't trigger the tripwire |
| 39 | + H.checkTripwire tripwire |
| 40 | + childStatus <- liftIO $ H.readMVar childTid >>= threadStatus |
| 41 | + childStatus === ThreadFinished |
| 42 | + |
| 43 | +-- TODO make only unix test case |
| 44 | +hprop_check_watchdog_kills_hanged_thread_with_its_children :: Property |
| 45 | +hprop_check_watchdog_kills_hanged_thread_with_its_children = H.propertyOnce $ do |
| 46 | + let watchdogCfg = WatchdogConfig 1 |
| 47 | + childTid <- H.newEmptyMVar |
| 48 | + grandChildTid1 <- H.newEmptyMVar |
| 49 | + grandChildTid2 <- H.newEmptyMVar |
| 50 | + procHandle <- H.newEmptyMVar |
| 51 | + childTripwire <- H.makeTripwire |
| 52 | + grandChildTripwire <- H.makeTripwire |
| 53 | + |
| 54 | + _ <- spawnTestT $ runWithWatchdog_ watchdogCfg $ do |
| 55 | + liftIO $ myThreadId >>= H.putMVar childTid |
| 56 | + |
| 57 | + (_, _, _, h, _) <- H.createProcess $ P.shell "tail -f /dev/null" |
| 58 | + H.putMVar procHandle h |
| 59 | + |
| 60 | + H.asyncRegister_ $ do |
| 61 | + liftIO $ myThreadId >>= H.putMVar grandChildTid1 |
| 62 | + threadDelay 3_000_000 |
| 63 | + H.triggerTripwire grandChildTripwire |
| 64 | + |
| 65 | + H.asyncRegister_ $ do |
| 66 | + liftIO $ myThreadId >>= H.putMVar grandChildTid2 |
| 67 | + threadDelay 3_000_000 |
| 68 | + H.triggerTripwire grandChildTripwire |
| 69 | + |
| 70 | + void $ H.threadDelay 3_000_000 |
| 71 | + H.triggerTripwire childTripwire |
| 72 | + |
| 73 | + -- wait for OS to kill the spawned process |
| 74 | + t <- liftIO getUtcTime |
| 75 | + H.byDeadlineM |
32 | 76 | H.threadDelay 1_000_000
|
33 |
| - H.noteShowM_ . liftIO $ (readMVar childTid >>= threadStatus) |
| 77 | + -- make sure that we accidentally didn't trigger the tripwire |
| 78 | + H.checkTripwire childTripwire |
| 79 | + H.checkTripwire grandChildTripwire |
| 80 | + |
| 81 | + childStatus <- liftIO $ H.readMVar childTid >>= threadStatus |
| 82 | + childStatus === ThreadFinished |
| 83 | + |
| 84 | + grandChildStatus1 <- liftIO $ H.readMVar grandChildTid1 >>= threadStatus |
| 85 | + grandChildStatus1 === ThreadFinished |
| 86 | + |
| 87 | + grandChildStatus2 <- liftIO $ H.readMVar grandChildTid1 >>= threadStatus |
| 88 | + grandChildStatus2 === ThreadFinished |
| 89 | + |
| 90 | + -- check that tail process got killed |
| 91 | + tailPid <- liftIO $ H.readMVar procHandle >>= P.getPid |
| 92 | + tailPid === Nothing |
34 | 93 | H.failure
|
35 | 94 |
|
36 |
| -runTestT :: HasCallStack |
| 95 | +spawnTestT :: HasCallStack |
37 | 96 | => H.MonadTest m
|
38 | 97 | => MonadIO m
|
39 | 98 | => Show a
|
40 |
| - => H.TestT IO a |
| 99 | + => H.TestT (ResourceT IO) a |
41 | 100 | -> m (Either H.Failure a, H.Journal)
|
42 |
| -runTestT testt = withFrozenCallStack $ do |
43 |
| - (res, log') <- H.evalIO . runWriterT . runExceptT . H.unTest $ testt |
| 101 | +spawnTestT testt = withFrozenCallStack $ do |
| 102 | + (res, log') <- H.evalIO $ |
| 103 | + H.withAsync |
| 104 | + (runResourceT . runWriterT . runExceptT $ H.unTest testt) |
| 105 | + H.wait |
44 | 106 | H.noteShow_ res
|
45 | 107 | H.noteShow_ log'
|
46 | 108 | pure (res, log')
|
|
0 commit comments