on master help sign in
Randomized port numbers
commit70d89fbf5c687ee96a7374b784489a02bdb499bb authorMichael Snoyman <michael@snoyman.com> dateWed 9 Apr 2014, 07:23 (13 years ago) parentb15997f
2 files changed+16−8
warp/test/ExceptionSpec.hs+14−7 warp/warp.cabal+2−1
warp/test/ExceptionSpec.hs+14−7
@@ -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----------------------------------------------------------------
warp/warp.cabal+2−1
@@ -140,7 +140,8 @@ Test-Suite spec
140140 , old-locale
141141 , text
142142 , conduit-extra
143− , streaming-commons
143+ , streaming-commons >= 0.1.1
144+ , async
144145
145146 if (os(linux) || os(freebsd) || os(darwin)) && flag(allow-sendfilefd)
146147 Cpp-Options: -DSENDFILEFD