2
2
3
3
module TestLib.Util where
4
4
5
- import Control.Monad.Catch (MonadMask , MonadThrow )
6
5
import Control.Monad.IO.Unlift
7
- import Control.Retry
8
6
import Data.Aeson (Value )
9
7
import Data.String.Interpolate
10
8
import Data.Text as T
11
- import Data.Time
12
- import Data.Typeable
13
- import GHC.Stack
14
9
import System.FilePath
15
- import System.Timeout (Timeout )
16
10
import Test.Sandwich
17
11
import UnliftIO.Directory
18
- import UnliftIO.Exception
19
- import UnliftIO.Timeout
20
12
21
13
#if MIN_VERSION_aeson(2,0,0)
22
14
import qualified Data.Aeson.Key as A
@@ -27,10 +19,10 @@ import qualified Data.HashMap.Strict as HM
27
19
#endif
28
20
29
21
30
- findFirstParentMatching :: (MonadIO m , MonadThrow m ) => (FilePath -> m Bool ) -> m FilePath
22
+ findFirstParentMatching :: (MonadIO m ) => (FilePath -> m Bool ) -> m FilePath
31
23
findFirstParentMatching cb = getCurrentDirectory >>= findFirstParentMatching' cb
32
24
33
- findFirstParentMatching' :: (MonadIO m , MonadThrow m ) => (FilePath -> m Bool ) -> FilePath -> m FilePath
25
+ findFirstParentMatching' :: (MonadIO m ) => (FilePath -> m Bool ) -> FilePath -> m FilePath
34
26
findFirstParentMatching' cb startingAt = cb startingAt >>= \ case
35
27
True -> return startingAt
36
28
False -> case takeDirectory startingAt of
@@ -52,40 +44,3 @@ aesonFromList xs = HM.fromList [(A.fromText k, v) | (k, v) <- xs]
52
44
aesonFromList :: (Eq k , Hashable k ) => [(Text , Value )] -> HM. HashMap A. Key v
53
45
aesonFromList = HM. fromList
54
46
#endif
55
-
56
- -- waitUntil :: forall m a. (HasCallStack, MonadIO m, MonadMask m, MonadThrow m) => Double -> m a -> m a
57
- -- waitUntil timeInSeconds action = do
58
- -- let policy = limitRetriesByCumulativeDelay (round (timeInSeconds * 1_000_000.0)) $ capDelay 200_000 $ exponentialBackoff 1_000
59
- -- recoverAll policy $ const action
60
-
61
- waitUntil :: forall m a . (HasCallStack , MonadIO m , MonadMask m , MonadThrow m , MonadUnliftIO m ) => Double -> m a -> m a
62
- waitUntil timeInSeconds action = do
63
- startTime <- liftIO getCurrentTime
64
-
65
- recoveringDynamic policy [handleFailureReasonException startTime] $ \ _status ->
66
- rethrowTimeoutExceptionWithCallStack $
67
- timeout (round (timeInSeconds * 1_000_000 )) action >>= \ case
68
- Nothing -> throwIO $ userError [i |Action timed out in waitUntil|]
69
- Just x -> return x
70
-
71
- where
72
- policy = capDelay 1_000_000 $ exponentialBackoff 1_000
73
-
74
- handleFailureReasonException startTime _status = Handler $ \ (_ :: SomeException ) ->
75
- retryUnlessTimedOut startTime
76
-
77
- retryUnlessTimedOut :: UTCTime -> m RetryAction
78
- retryUnlessTimedOut startTime = do
79
- now <- liftIO getCurrentTime
80
- let thresh = secondsToNominalDiffTime (realToFrac timeInSeconds)
81
- if | (diffUTCTime now startTime) > thresh -> return DontRetry
82
- | otherwise -> return ConsultPolicy
83
-
84
- rethrowTimeoutExceptionWithCallStack :: (HasCallStack ) => m a -> m a
85
- rethrowTimeoutExceptionWithCallStack = handleSyncOrAsync $ \ (e@ (SomeException inner)) ->
86
- if | Just (_ :: Timeout ) <- fromExceptionUnwrap e -> do
87
- throwIO $ userError " Timeout in waitUntil"
88
- | Just (SyncExceptionWrapper (cast -> Just (SomeException (cast -> Just (SomeAsyncException (cast -> Just (_ :: Timeout ))))))) <- cast inner -> do
89
- throwIO $ userError " Timeout in waitUntil"
90
- | otherwise -> do
91
- throwIO e
0 commit comments