Skip to content

Commit

Permalink
move content of 'Test.Utils.Ports' to 'Cardano.Wallet.Network.Ports'
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Jun 8, 2020
1 parent 269d273 commit 5a263bc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 49 deletions.
1 change: 1 addition & 0 deletions lib/core/cardano-wallet-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ library
, persistent-template
, process
, random
, random-shuffle
, retry
, safe
, scientific
Expand Down
26 changes: 26 additions & 0 deletions lib/core/src/Cardano/Wallet/Network/Ports.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ module Cardano.Wallet.Network.Ports
-- * Helpers
, waitForPort
, unsafePortNumber
, findPort
, randomUnusedTCPPorts
) where

import Prelude

import Control.Monad
( filterM )
import Control.Monad.IO.Class
( liftIO )
import Control.Retry
( RetryPolicyM, retrying )
import Data.List
( isInfixOf )
import Data.List
( sort )
import Data.Streaming.Network
( bindRandomPortTCP )
import Data.Word
Expand All @@ -54,6 +60,8 @@ import Network.Socket
, socket
, tupleToHostAddress
)
import System.Random.Shuffle
( shuffleM )
import UnliftIO.Exception
( bracket, throwIO, try )

Expand Down Expand Up @@ -113,3 +121,21 @@ unsafePortNumber = \case
SockAddrInet p _ -> p
SockAddrInet6 p _ _ _ -> p
SockAddrUnix _ -> error "unsafePortNumber: no port for unix sockets."

-- | 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.
--
-- Note that this method of allocating ports is subject to race
-- conditions. Production code should use better methods such as passing a
-- listening socket to the child process.
randomUnusedTCPPorts :: Int -> IO [Int]
randomUnusedTCPPorts count = do
usablePorts <- shuffleM [1024..49151]
sort <$> filterM unused (take count usablePorts)
where
unused = fmap not . isPortOpen . simpleSockAddr (127,0,0,1) . fromIntegral

-- | Returen a single TCP port that was unused at the time this function was
-- called.
findPort :: IO Int
findPort = head <$> randomUnusedTCPPorts 1
2 changes: 0 additions & 2 deletions lib/jormungandr/cardano-wallet-jormungandr.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ test-suite jormungandr-integration
, monad-control
, persistent
, process
, random-shuffle
, retry
, safe
, servant
Expand Down Expand Up @@ -281,7 +280,6 @@ test-suite jormungandr-integration
Test.Integration.Jormungandr.Scenario.CLI.StakePools
Test.Integration.Jormungandr.Scenario.CLI.Transactions
Test.Integration.Jormungandr.Scenario.CLI.Port
Test.Utils.Ports

benchmark latency
default-language:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ import Cardano.Wallet.Network
)
import Cardano.Wallet.Network.BlockHeaders
( emptyBlockHeaders )
import Cardano.Wallet.Network.Ports
( randomUnusedTCPPorts )
import Cardano.Wallet.Primitive.AddressDerivation
( NetworkDiscriminant (..), Passphrase (..) )
import Cardano.Wallet.Primitive.Types
Expand Down Expand Up @@ -122,8 +124,6 @@ import Test.Hspec
)
import Test.QuickCheck
( Arbitrary (..), generate, vector )
import Test.Utils.Ports
( randomUnusedTCPPorts )

import qualified Cardano.Wallet.Jormungandr.Api.Client as Jormungandr
import qualified Data.ByteString as BS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import Cardano.Launcher
( Command (..), StdStream (..), withBackendProcess )
import Cardano.Wallet.Api.Types
( ApiWallet )
import Cardano.Wallet.Network.Ports
( findPort )
import Cardano.Wallet.Primitive.Types
( SyncProgress (..) )
import Control.Exception
Expand Down Expand Up @@ -68,8 +70,6 @@ import Test.Integration.Framework.DSL
)
import Test.Utils.Paths
( getTestData )
import Test.Utils.Ports
( findPort )

import qualified Data.Text as T
import qualified Data.Text.IO as TIO
Expand Down
43 changes: 0 additions & 43 deletions lib/jormungandr/test/integration/Test/Utils/Ports.hs

This file was deleted.

0 comments on commit 5a263bc

Please sign in to comment.