tower-hs-0.2.0.0: test/Tower/Middleware/TimeoutSpec.hs
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NumericUnderscores #-}
module Tower.Middleware.TimeoutSpec (spec) where
import Control.Concurrent (threadDelay)
import Test.Hspec
import Tower.Service
import Tower.Error
import Tower.Error.Testing ()
import Data.Function ((&))
import Tower.Middleware.Timeout
spec :: Spec
spec = describe "Timeout middleware" $ do
it "allows fast requests through" $ do
let svc :: Service () String
svc = Service $ \_ -> pure (Right "fast")
timed = svc & withTimeout 1000
result <- runService timed ()
result `shouldBe` Right "fast"
it "times out slow requests" $ do
let svc :: Service () String
svc = Service $ \_ -> do
threadDelay 500_000 -- 500ms
pure (Right "slow")
timed = svc & withTimeout 100 -- 100ms timeout
result <- runService timed ()
result `shouldBe` Left TimeoutError
it "preserves errors from inner service" $ do
let svc :: Service () String
svc = Service $ \_ -> pure (Left (CustomError "inner error"))
timed = svc & withTimeout 1000
result <- runService timed ()
result `shouldBe` Left (CustomError "inner error")