1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
{-# LANGUAGE CPP #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE PatternGuards #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
import Test.Hspec
import Test.QuickCheck.Arbitrary ()
import Control.Exception.Lifted hiding (throwTo)
import Data.IORef
import Data.Typeable
import Control.Monad.IO.Class
import Control.Concurrent (throwTo, threadDelay, forkIO)
import Control.Exception.Enclosed
#ifdef HLINT
{-# ANN main ("HLint: ignore Redundant do"::String) #-}
#endif
main :: IO ()
main = hspec $ do
describe "any exceptions" $ do
it "catchAny" $ do
failed <- newIORef 0
tid <- forkIO $ do
catchAny
(threadDelay 20000)
(const $ writeIORef failed 1)
writeIORef failed 2
threadDelay 10000
throwTo tid DummyException
threadDelay 50000
didFail <- readIORef failed
liftIO $ didFail `shouldBe` (0 :: Int)
it "tryAny" $ do
failed <- newIORef False
tid <- forkIO $ do
_ <- tryAny $ threadDelay 20000
writeIORef failed True
threadDelay 10000
throwTo tid DummyException
threadDelay 50000
didFail <- readIORef failed
liftIO $ didFail `shouldBe` False
it "tryAnyDeep" $ do
eres <- tryAnyDeep $ return $ throw DummyException
case eres of
Left e
| Just DummyException <- fromException e -> return ()
| otherwise -> error "Expected a DummyException"
Right () -> error "Expected an exception" :: IO ()
data DummyException = DummyException
deriving (Show, Typeable)
instance Exception DummyException
|