@@ -13,13 +13,21 @@ import Network.Wai.Internal (Request(..))
1313import Network.Wai.Handler.Warp
1414import System.IO.Unsafe (unsafePerformIO)
1515import Test.Hspec
16+import Control.Exception
17+import qualified Data.Streaming.Network as N
18+import Control.Concurrent.Async (withAsync)
19+import Network.Socket (sClose)
1620
1721main :: IO ()
1822main = hspec spec
1923
20−testServer :: IO ()
21−testServer =
22− run 2345 testApp
24+withTestServer :: (Int -> IO a) -> IO a
25+withTestServer inner = bracket
26+ (N.bindRandomPortTCP "*4")
27+ (sClose . snd)
28+ $ \(port, lsocket) -> do
29+ withAsync (runSettingsSocket defaultSettings lsocket testApp)
30+ $ \_ -> inner port
2331
2432testApp :: Application
2533testApp (Network.Wai.Internal.Request {pathInfo = [x]})
@@ -38,8 +46,7 @@ testApp _ =
3846 return $ responseLBS ok200 [] "foo"
3947
4048spec :: Spec
41−spec = unsafePerformIO $ (forkIO testServer >> threadDelay 100000 >>) $ return $
42− describe "responds even if there is an exception" $ do
49+spec = describe "responds even if there is an exception" $ do
4350 {- Disabling these tests. We can consider forcing evaluation in Warp.
4451 it "statusError" $ do
4552 sc <- rspCode <$> sendGET "http://localhost:2345/statusError"
@@ -54,8 +61,8 @@ spec = unsafePerformIO $ (forkIO testServer >> threadDelay 100000 >>) $ return $
5461 sc <- rspCode <$> sendGET "http://localhost:2345/bodyError"
5562 sc `shouldBe` (5,0,0)
5663 -}
57− it "ioException" $ do
58− sc <- rspCode <$> sendGET "http://localhost:2345/ioException"
64+ it "ioException" $ withTestServer $ \port -> do
65+ sc <- rspCode <$> sendGET (concat $ ["http://localhost:", show port, "/ioException"])
5966 sc `shouldBe` (5,0,0)
6067
6168----------------------------------------------------------------