@@ -27,7 +27,8 @@ import Control.Concurrent.Async
27
27
import Control.Concurrent
28
28
import Control.Exception
29
29
import qualified Data.ByteString.Lazy.Char8 as BL8 ( hPutStrLn )
30
- import Network.Socket
30
+ import Network.Simple.TCP hiding (send )
31
+ import Network.Socket (socketToHandle )
31
32
import System.IO
32
33
import Data.String.Conversions
33
34
import Test.Hspec
@@ -122,14 +123,19 @@ mockServerTalk CommandConfigurationDone = do
122
123
testPort :: Int
123
124
testPort = 8001
124
125
126
+ -- | Sample host shared amongst client and server
127
+ --
128
+ testHost :: String
129
+ testHost = " localhost"
130
+
125
131
-- | Runs server in a thread, 'withAsync' ensures cleanup.
126
132
--
127
133
withServer :: IO () -> IO ()
128
134
withServer test = withAsync server (const test)
129
135
where
130
136
server = runDAPServer config mockServerTalk
131
137
config = ServerConfig
132
- { host = " localhost "
138
+ { host = testHost
133
139
, port = testPort
134
140
, serverCapabilities = defaultCapabilities
135
141
, debugLogging = False
@@ -138,26 +144,17 @@ withServer test = withAsync server (const test)
138
144
-- | Spawns a new mock client that connects to the mock server.
139
145
--
140
146
withNewClient :: (Handle -> IO () ) -> IO ()
141
- withNewClient continue = withSocketsDo $ do
142
- [info] <- getAddrInfo (Just addrInfo) Nothing (Just (show testPort))
143
- socket <- openSocket info
144
- connect socket (addrAddress info) `catch` exceptionHandler
145
- handle <- socketToHandle socket ReadWriteMode
146
- hSetNewlineMode handle NewlineMode { inputNL = CRLF , outputNL = CRLF }
147
- continue handle `finally` hClose handle
148
- where
149
- exceptionHandler :: SomeException -> IO ()
150
- exceptionHandler _ = do
151
- threadDelay 100
152
- putStrLn " Retrying connection..."
153
- withNewClient continue
154
-
155
- addrInfo :: AddrInfo
156
- addrInfo
157
- = defaultHints
158
- { addrSocketType = Stream
159
- , addrFamily = AF_INET
160
- }
147
+ withNewClient continue = flip catch exceptionHandler $
148
+ connect testHost (show testPort) $ \ (socket, _) -> do
149
+ handle <- socketToHandle socket ReadWriteMode
150
+ hSetNewlineMode handle NewlineMode { inputNL = CRLF , outputNL = CRLF }
151
+ continue handle `finally` hClose handle
152
+ where
153
+ exceptionHandler :: SomeException -> IO ()
154
+ exceptionHandler _ = do
155
+ threadDelay 100
156
+ putStrLn " Retrying connection..."
157
+ withNewClient continue
161
158
162
159
-- | Helper to send JSON payloads to the server
163
160
--
0 commit comments