-
-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add some basic tests for the cleanup machinery in Delayed
- Loading branch information
Showing
3 changed files
with
55 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
servant-server/test/Servant/Server/Internal/RoutingApplicationSpec.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module Servant.Server.Internal.RoutingApplicationSpec (spec) where | ||
|
||
import Control.Exception hiding (Handler) | ||
import Control.Monad.IO.Class | ||
import Servant.Server | ||
import Servant.Server.Internal.RoutingApplication | ||
import System.Directory | ||
import Test.Hspec | ||
|
||
ok :: IO (RouteResult ()) | ||
ok = return (Route ()) | ||
|
||
delayed :: DelayedIO () -> RouteResult (Handler ()) -> Delayed () (Handler ()) | ||
delayed body srv = Delayed | ||
{ capturesD = \() -> DelayedIO $ \_req _cl -> ok | ||
, methodD = DelayedIO $ \_req_ _cl -> ok | ||
, authD = DelayedIO $ \_req _cl -> ok | ||
, bodyD = do | ||
liftIO (writeFile "delayed.test" "hia") | ||
addCleanup (removeFile "delayed.test" >> putStrLn "file removed") | ||
body | ||
, serverD = \() () _body _req -> srv | ||
} | ||
|
||
simpleRun :: Delayed () (Handler ()) | ||
-> IO () | ||
simpleRun d = fmap (either ignoreE id) . try $ | ||
runAction d () undefined (\_ -> return ()) (\_ -> FailFatal err500) | ||
|
||
where ignoreE :: SomeException -> () | ||
ignoreE = const () | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "Delayed" $ do | ||
it "actually runs clean up actions" $ do | ||
_ <- simpleRun $ delayed (return ()) (Route $ return ()) | ||
fileStillThere <- doesFileExist "delayed.test" | ||
fileStillThere `shouldBe` False | ||
it "even with exceptions in serverD" $ do | ||
_ <- simpleRun $ delayed (return ()) (Route $ throw DivideByZero) | ||
fileStillThere <- doesFileExist "delayed.test" | ||
fileStillThere `shouldBe` False | ||
it "even with routing failure in bodyD" $ do | ||
_ <- simpleRun $ delayed (delayedFailFatal err500) (Route $ return ()) | ||
fileStillThere <- doesFileExist "delayed.test" | ||
fileStillThere `shouldBe` False | ||
it "even with exceptions in bodyD" $ do | ||
_ <- simpleRun $ delayed (liftIO $ throwIO DivideByZero) (Route $ return ()) | ||
fileStillThere <- doesFileExist "delayed.test" | ||
fileStillThere `shouldBe` False |