Skip to content

Commit

Permalink
bind: tests for the new bind behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
infinity0 committed May 22, 2020
1 parent fb6ce53 commit 342cd9f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions network.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ test-suite spec
directory,
HUnit,
network,
temporary,
hspec >= 2.6

test-suite doctests
Expand Down
31 changes: 31 additions & 0 deletions tests/Network/SocketSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Network.Socket
import Network.Socket.ByteString
import Network.Test.Common
import System.Mem (performGC)
import System.IO.Error (tryIOError, isAlreadyInUseError)
import System.IO.Temp (withSystemTempDirectory)

import Test.Hspec

Expand Down Expand Up @@ -63,6 +65,35 @@ spec = do
sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
bind sock (addrAddress addr) `shouldThrow` anyIOException

it "successfully binds to a unix socket, twice" $ do
withSystemTempDirectory "haskell-network" $ \path -> do
let sfile = path <> "/socket-file"
let addr = SockAddrUnix sfile
when (isSupportedSockAddr addr) $ do
sock0 <- socket AF_UNIX Stream defaultProtocol
bind sock0 addr
listen sock0 1

sock1 <- socket AF_UNIX Stream defaultProtocol
tryIOError (bind sock1 addr) >>= \o -> case o of
Right () -> error "bind should have failed but succeeded"
Left e -> if isAlreadyInUseError e then pure () else ioError e

close sock0

-- Unix systems tend to leave the file existing, which is
-- why our `bind` does its workaround. however if any
-- system in the future does fix this issue, we don't want
-- this test to fail, since that would defeat the purpose
-- of our workaround. but you can uncomment the below lines
-- if you want to play with this on your own system.
--import System.Directory (doesPathExist)
--ex <- doesPathExist sfile
--unless ex $ error "socket file was deleted unexpectedly"

sock2 <- socket AF_UNIX Stream defaultProtocol
bind sock2 addr

describe "UserTimeout" $ do
it "can be set" $ do
when (isSupportedSocketOption UserTimeout) $ do
Expand Down

0 comments on commit 342cd9f

Please sign in to comment.