on master help sign in
Reuse bindRandomPortTCP #671
c548f55Michael Snoyman
warp/ChangeLog.md+2−1
@@ -1,7 +1,8 @@
1## 3.2.16
1## 3.2.16
2
2
3* In `testWithApplication`, don't `throwTo` ignorable exceptions
3* In `testWithApplication`, don't `throwTo` ignorable exceptions
4− [#671](https://github.com/yesodweb/wai/issues/671)
4+ [#671](https://github.com/yesodweb/wai/issues/671), and
5+ reuse `bindRandomPortTCP`
5
6
6## 3.2.15
7## 3.2.15
7
8
warp/Network/Wai/Handler/Warp/WithApplication.hs+3−12
@@ -1,4 +1,4 @@
1−
1+{-# LANGUAGE OverloadedStrings #-}
2module Network.Wai.Handler.Warp.WithApplication (
2module Network.Wai.Handler.Warp.WithApplication (
3 withApplication,
3 withApplication,
4 withApplicationSettings,
4 withApplicationSettings,
@@ -12,6 +12,7 @@ import Control.Concurrent
12import Control.Concurrent.Async
12import Control.Concurrent.Async
13import Control.Exception
13import Control.Exception
14import Control.Monad (when)
14import Control.Monad (when)
15+import Data.Streaming.Network (bindRandomPortTCP)
15import Network.Socket
16import Network.Socket
16import Network.Wai
17import Network.Wai
17import Network.Wai.Handler.Warp.Run
18import Network.Wai.Handler.Warp.Run
@@ -96,17 +97,7 @@ mkWaiter = do
96--
97--
97-- @since 3.2.4
98-- @since 3.2.4
98openFreePort :: IO (Port, Socket)
99openFreePort :: IO (Port, Socket)
99−openFreePort = do
100+openFreePort = bindRandomPortTCP "127.0.0.1"
100− let hints = defaultHints {
101− addrFlags = [AI_PASSIVE]
102− , addrSocketType = Stream
103− }
104− addr:_ <- getAddrInfo (Just hints) (Just "127.0.0.1") Nothing
105− s <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
106− bind s $ addrAddress addr
107− listen s 1
108− port <- socketPort s
109− return (fromIntegral port, s)
110
101
111-- | Like 'openFreePort' but closes the socket before exiting.
102-- | Like 'openFreePort' but closes the socket before exiting.
112withFreePort :: ((Port, Socket) -> IO a) -> IO a
103withFreePort :: ((Port, Socket) -> IO a) -> IO a