Skip to content

Commit

Permalink
Add regression test for socket connect function
Browse files Browse the repository at this point in the history
A recent regression occurred where errors were not being thrown from
`connect`. This is easily avoidable with two simple tests to check
success and error.
  • Loading branch information
eborden committed Jan 21, 2019
1 parent 7a36049 commit afebb25
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions tests/SimpleSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module SimpleSpec (main, spec) where

import Control.Concurrent (ThreadId, forkIO, myThreadId)
import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar, readMVar)
import Control.Concurrent.MVar (MVar, newEmptyMVar, putMVar, takeMVar, readMVar)
import qualified Control.Exception as E
import Control.Monad
import Data.ByteString (ByteString)
Expand Down Expand Up @@ -111,6 +111,18 @@ spec = do

client sock = send sock testMsg
tcpTest client server
describe "connect" $ do
let
hints = defaultHints { addrSocketType = Stream }
connect' serverPort = do
addr:_ <- getAddrInfo (Just hints) (Just serverAddr) (Just $ show serverPort)
sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
connect sock (addrAddress addr)
return sock
it "fails to connect and throws an IOException" $ do
connect' (8080 :: Int) `shouldThrow` anyIOException
it "successfully connects to a socket with no exception" $ do
tcpTestUsingClient return return $ readMVar >=> connect'

describe "UserTimeout" $ do
it "can be set" $ do
Expand Down Expand Up @@ -244,8 +256,7 @@ unixTest clientAct serverAct = do
-- closed after the actions have run.
tcpTest :: (Socket -> IO a) -> (Socket -> IO b) -> IO ()
tcpTest clientAct serverAct = do
portVar <- newEmptyMVar
test (clientSetup portVar) clientAct (serverSetup portVar) server
tcpTestUsingClient serverAct clientAct clientSetup
where
clientSetup portVar = do
let hints = defaultHints { addrSocketType = Stream }
Expand All @@ -260,6 +271,12 @@ tcpTest clientAct serverAct = do
connect sock $ addrAddress addr
return sock

tcpTestUsingClient
:: (Socket -> IO a) -> (Socket -> IO b) -> (MVar PortNumber -> IO Socket) -> IO ()
tcpTestUsingClient serverAct clientAct clientSetup = do
portVar <- newEmptyMVar
test (clientSetup portVar) clientAct (serverSetup portVar) server
where
serverSetup portVar = do
let hints = defaultHints {
addrFlags = [AI_PASSIVE]
Expand Down

0 comments on commit afebb25

Please sign in to comment.