1
1
module Servant.Server.Internal.RoutingApplicationSpec (spec ) where
2
2
3
+ import Prelude ()
4
+ import Prelude.Compat
5
+
3
6
import Control.Exception hiding (Handler )
4
7
import Control.Monad.IO.Class
8
+ import Data.Maybe (isJust )
9
+ import Data.IORef
5
10
import Servant.Server
6
11
import Servant.Server.Internal.RoutingApplication
7
- import System.Directory
8
12
import Test.Hspec
9
13
14
+ import System.IO.Unsafe (unsafePerformIO )
15
+
10
16
ok :: IO (RouteResult () )
11
17
ok = return (Route () )
12
18
19
+ -- Let's not write to the filesystem
20
+ delayedTestRef :: IORef (Maybe String )
21
+ delayedTestRef = unsafePerformIO $ newIORef Nothing
22
+
13
23
delayed :: DelayedIO () -> RouteResult (Handler () ) -> Delayed () (Handler () )
14
24
delayed body srv = Delayed
15
25
{ capturesD = \ () -> DelayedIO $ \ _req _cl -> ok
16
26
, methodD = DelayedIO $ \ _req_ _cl -> ok
17
27
, authD = DelayedIO $ \ _req _cl -> ok
18
28
, bodyD = do
19
- liftIO (writeFile " delayed.test " " hia " )
20
- addCleanup (removeFile " delayed.test " >> putStrLn " file removed " )
29
+ liftIO (writeIORef delayedTestRef ( Just " hia " ) >> putStrLn " garbage created " )
30
+ addCleanup (writeIORef delayedTestRef Nothing >> putStrLn " garbage collected " )
21
31
body
22
32
, serverD = \ () () _body _req -> srv
23
33
}
@@ -35,17 +45,17 @@ spec = do
35
45
describe " Delayed" $ do
36
46
it " actually runs clean up actions" $ do
37
47
_ <- simpleRun $ delayed (return () ) (Route $ return () )
38
- fileStillThere <- doesFileExist " delayed.test "
39
- fileStillThere `shouldBe` False
48
+ cleanUpDone <- isJust <$> readIORef delayedTestRef
49
+ cleanUpDone `shouldBe` False
40
50
it " even with exceptions in serverD" $ do
41
51
_ <- simpleRun $ delayed (return () ) (Route $ throw DivideByZero )
42
- fileStillThere <- doesFileExist " delayed.test "
43
- fileStillThere `shouldBe` False
52
+ cleanUpDone <- isJust <$> readIORef delayedTestRef
53
+ cleanUpDone `shouldBe` False
44
54
it " even with routing failure in bodyD" $ do
45
55
_ <- simpleRun $ delayed (delayedFailFatal err500) (Route $ return () )
46
- fileStillThere <- doesFileExist " delayed.test "
47
- fileStillThere `shouldBe` False
56
+ cleanUpDone <- isJust <$> readIORef delayedTestRef
57
+ cleanUpDone `shouldBe` False
48
58
it " even with exceptions in bodyD" $ do
49
59
_ <- simpleRun $ delayed (liftIO $ throwIO DivideByZero ) (Route $ return () )
50
- fileStillThere <- doesFileExist " delayed.test "
51
- fileStillThere `shouldBe` False
60
+ cleanUpDone <- isJust <$> readIORef delayedTestRef
61
+ cleanUpDone `shouldBe` False
0 commit comments