Skip to content

Commit

Permalink
sandwich: fix compatibility with base < 4.14.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjm committed Nov 8, 2024
1 parent 860dc43 commit ee34074
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions sandwich/src/Test/Sandwich/Waits.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE RankNTypes #-}

Expand All @@ -22,12 +23,15 @@ import Data.String.Interpolate
import Data.Time
import Data.Typeable
import GHC.Stack
import System.Timeout (Timeout)
import Test.Sandwich
import UnliftIO.Exception
import UnliftIO.Retry
import UnliftIO.Timeout

#if MIN_VERSION_base(4,14,0)
import System.Timeout (Timeout)
#endif


-- | Keep trying an action up to a timeout while it fails with a 'FailureReason'.
-- Use exponential backoff, with delays capped at 1 second.
Expand Down Expand Up @@ -60,11 +64,15 @@ waitUntil' policy timeInSeconds action = do
if | (diffUTCTime now startTime) > thresh -> return DontRetry
| otherwise -> return ConsultPolicy

-- We can only catch the timeout for base >= 4.14.0.0, since before that the Timeout exception wasn't exported
rethrowTimeoutExceptionWithCallStack :: (HasCallStack) => m a -> m a
rethrowTimeoutExceptionWithCallStack = handleSyncOrAsync $ \(e@(SomeException inner)) ->
if | Just (_ :: Timeout) <- fromExceptionUnwrap e -> do
throwIO $ Reason (Just (popCallStack callStack)) "Timeout in waitUntil"
| Just (SyncExceptionWrapper (cast -> Just (SomeException (cast -> Just (SomeAsyncException (cast -> Just (_ :: Timeout))))))) <- cast inner -> do
throwIO $ Reason (Just (popCallStack callStack)) "Timeout in waitUntil"
| otherwise -> do
throwIO e
if
#if !MIN_VERSION_base(4,13,0)
| Just (_ :: Timeout) <- fromExceptionUnwrap e -> do
throwIO $ Reason (Just (popCallStack callStack)) "Timeout in waitUntil"
| Just (SyncExceptionWrapper (cast -> Just (SomeException (cast -> Just (SomeAsyncException (cast -> Just (_ :: Timeout))))))) <- cast inner -> do
throwIO $ Reason (Just (popCallStack callStack)) "Timeout in waitUntil"
#endif
| otherwise -> do
throwIO e

0 comments on commit ee34074

Please sign in to comment.