time-out-0.2: test/test.hs
{- This file is part of time-out.
-
- Written in 2016 by fr33domlover <fr33domlover@riseup.net>.
-
- ♡ Copying is an act of love. Please copy, reuse and share.
-
- The author(s) have dedicated all copyright and related and neighboring
- rights to this software to the public domain worldwide. This software is
- distributed without any warranty.
-
- You should have received a copy of the CC0 Public Domain Dedication along
- with this software. If not, see
- <http://creativecommons.org/publicdomain/zero/1.0/>.
-}
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Timeout
import Control.Timeout
import Data.Maybe (isJust, isNothing, catMaybes)
import Data.Time.Units (Second)
import System.Exit (exitFailure)
import System.IO
tests :: [(Int, TimeoutT IO Bool)]
tests =
[ ( 1
, liftIO $ fmap isJust $ timeout (3 :: Second) $ delay (1 :: Second)
)
, ( 2
, liftIO $ fmap isNothing $ timeout (3 :: Second) $ delay (5 :: Second)
)
, ( 3
, fmap isJust $ withTimeoutCatch $ delay (1 :: Second)
)
, ( 4
, fmap isNothing $ withTimeoutCatch $ delay (5 :: Second)
)
]
runTest :: Int -> TimeoutT IO Bool -> TimeoutT IO (Maybe Int)
runTest num test = do
result <- test
return $ if result then Nothing else Just num
runTests :: [(Int, TimeoutT IO Bool)] -> TimeoutT IO [Int]
runTests = fmap catMaybes . traverse (uncurry runTest)
main :: IO ()
main = do
fails <- runTimeoutT (runTests tests) (3 :: Second)
if null fails
then putStrLn "Success!"
else do
putStrLn $ "Failed: " ++ show fails
exitFailure