Skip to content

Commit

Permalink
tests: remove duplicate isPortOpen
Browse files Browse the repository at this point in the history
There was a sticky package interdependency that meant that isPortOpen
needed to be copy&pasted into two packages. One of the copies missed a
bug fix. Since the package interdependency is no longer there, delete
the broken copy.
  • Loading branch information
rvl committed Dec 27, 2019
1 parent cbe5a4a commit 2dea92e
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 49 deletions.
3 changes: 3 additions & 0 deletions lib/jormungandr/cardano-wallet-jormungandr.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ test-suite integration
, network
, persistent
, process
, random-shuffle
, retry
, safe
, servant
Expand All @@ -233,6 +234,7 @@ test-suite integration
, text-class
, time
, transformers
, unliftio
, warp
, yaml
build-tools:
Expand All @@ -258,6 +260,7 @@ test-suite integration
Test.Integration.Jormungandr.Scenario.CLI.StakePools
Test.Integration.Jormungandr.Scenario.CLI.Transactions
Test.Integration.Jormungandr.Scenario.CLI.Mnemonics
Test.Utils.Ports

benchmark latency
default-language:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,20 @@
module Test.Utils.Ports
( randomUnusedTCPPorts
, findPort
, isPortOpen
, simpleSockAddr
) where

import Prelude

import Cardano.Wallet.Network.Ports
( isPortOpen
, simpleSockAddr
)
import Control.Monad
( filterM )
import Data.List
( sort )
import Data.Word
( Word8 )
import Foreign.C.Error
( Errno (..), eCONNREFUSED )
import GHC.IO.Exception
( IOException (..) )
import Network.Socket
( Family (AF_INET)
, PortNumber
, SockAddr (SockAddrInet)
, SocketType (Stream)
, close'
, connect
, socket
, tupleToHostAddress
)
import System.Random.Shuffle
( shuffleM )
import UnliftIO.Exception
( bracket, throwIO, try )

-- | Get a list of random TCPv4 ports that currently do not have any servers
-- listening on them. It may return less than the requested number of ports.
Expand All @@ -59,27 +43,3 @@ randomUnusedTCPPorts count = do
-- called.
findPort :: IO Int
findPort = head <$> randomUnusedTCPPorts 1

-- | Checks whether @connect()@ to a given TCPv4 `SockAddr` succeeds or
-- returns `eCONNREFUSED`.
--
-- Rethrows connection exceptions in all other cases (e.g. when the host
-- is unroutable).
isPortOpen :: SockAddr -> IO Bool
isPortOpen sockAddr = do
bracket (socket AF_INET Stream 6 {- TCP -}) close' $ \sock -> do
res <- try $ connect sock sockAddr
case res of
Right () -> return True
Left e ->
if (Errno <$> ioe_errno e) == Just eCONNREFUSED
then return False
else throwIO e


-- | Creates a `SockAttr` from host IP and port number.
--
-- Example:
-- > simpleSockAddr (127,0,0,1) 8000
simpleSockAddr :: (Word8, Word8, Word8, Word8) -> PortNumber -> SockAddr
simpleSockAddr addr port = SockAddrInet port (tupleToHostAddress addr)
3 changes: 0 additions & 3 deletions lib/test-utils/cardano-wallet-test-utils.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,16 @@ library
, iohk-monitoring
, network
, QuickCheck
, random-shuffle
, stm
, template-haskell
, time
, unliftio
, wai-app-static
, warp
hs-source-dirs:
src
exposed-modules:
Test.Hspec.Extra
Test.Utils.Paths
Test.Utils.Ports
Test.Utils.StaticServer
Test.Utils.Time
Test.Utils.Trace
Expand Down
2 changes: 2 additions & 0 deletions nix/.stack.nix/cardano-wallet-jormungandr.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions nix/.stack.nix/cardano-wallet-test-utils.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2dea92e

Please sign in to comment.