forked from facebookincubator/hsthrift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExceptionTest.hs
48 lines (40 loc) · 1.15 KB
/
ExceptionTest.hs
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
-- Copyright (c) Facebook, Inc. and its affiliates.
module ExceptionTest where
import Control.Concurrent.MVar
import Control.Exception
import Control.Monad.Trans.Class
import Test.HUnit
import TestChannel
import TestRunner
import Exception.Types
import Thrift.Monad
runDangerousCode :: IO ()
runDangerousCode = throw $ X "just because"
exceptionTest :: Test
exceptionTest = TestLabel "exception test" $ TestCase $
runDangerousCode `catch` \X{} -> return ()
data Event = Acquire | Release | RunBody | Unreachable | Catch
deriving (Eq, Show)
bracketTest :: Test
bracketTest = TestLabel "bracket test" $ TestCase $ do
mvar <- newMVar []
let logEvent ev = modifyMVar_ mvar (return . (ev:))
channel <- TestChannel <$> newEmptyMVar
runThrift
(bracketThrift_
(lift $ logEvent Acquire)
(lift $ logEvent Release)
(lift $ do
logEvent RunBody
runDangerousCode
logEvent Unreachable))
channel
`catch` (\ X{} -> logEvent Catch)
eventlog <- readMVar mvar
assertEqual "eventlog"
[Acquire, RunBody, Release, Catch]
(reverse eventlog)
main :: IO ()
main = testRunner $ TestList
[ exceptionTest
, bracketTest ]