From 5a263bc2f6db213ca9a222968525607b0a45e117 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Fri, 5 Jun 2020 17:35:24 +0200 Subject: [PATCH] move content of 'Test.Utils.Ports' to 'Cardano.Wallet.Network.Ports' --- lib/core/cardano-wallet-core.cabal | 1 + lib/core/src/Cardano/Wallet/Network/Ports.hs | 26 +++++++++++ .../cardano-wallet-jormungandr.cabal | 2 - .../Cardano/Wallet/Jormungandr/NetworkSpec.hs | 4 +- .../Jormungandr/Scenario/CLI/Launcher.hs | 4 +- .../test/integration/Test/Utils/Ports.hs | 43 ------------------- 6 files changed, 31 insertions(+), 49 deletions(-) delete mode 100644 lib/jormungandr/test/integration/Test/Utils/Ports.hs diff --git a/lib/core/cardano-wallet-core.cabal b/lib/core/cardano-wallet-core.cabal index dbe50c6dfa3..47ac5eb155f 100644 --- a/lib/core/cardano-wallet-core.cabal +++ b/lib/core/cardano-wallet-core.cabal @@ -75,6 +75,7 @@ library , persistent-template , process , random + , random-shuffle , retry , safe , scientific diff --git a/lib/core/src/Cardano/Wallet/Network/Ports.hs b/lib/core/src/Cardano/Wallet/Network/Ports.hs index d4b637dbb08..bcd5d254734 100644 --- a/lib/core/src/Cardano/Wallet/Network/Ports.hs +++ b/lib/core/src/Cardano/Wallet/Network/Ports.hs @@ -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 @@ -54,6 +60,8 @@ import Network.Socket , socket , tupleToHostAddress ) +import System.Random.Shuffle + ( shuffleM ) import UnliftIO.Exception ( bracket, throwIO, try ) @@ -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 diff --git a/lib/jormungandr/cardano-wallet-jormungandr.cabal b/lib/jormungandr/cardano-wallet-jormungandr.cabal index 405dcbb26c0..8df6ce11f52 100644 --- a/lib/jormungandr/cardano-wallet-jormungandr.cabal +++ b/lib/jormungandr/cardano-wallet-jormungandr.cabal @@ -245,7 +245,6 @@ test-suite jormungandr-integration , monad-control , persistent , process - , random-shuffle , retry , safe , servant @@ -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: diff --git a/lib/jormungandr/test/integration/Cardano/Wallet/Jormungandr/NetworkSpec.hs b/lib/jormungandr/test/integration/Cardano/Wallet/Jormungandr/NetworkSpec.hs index 174f1fe3fd7..7dd1f7b6f11 100644 --- a/lib/jormungandr/test/integration/Cardano/Wallet/Jormungandr/NetworkSpec.hs +++ b/lib/jormungandr/test/integration/Cardano/Wallet/Jormungandr/NetworkSpec.hs @@ -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 @@ -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 diff --git a/lib/jormungandr/test/integration/Test/Integration/Jormungandr/Scenario/CLI/Launcher.hs b/lib/jormungandr/test/integration/Test/Integration/Jormungandr/Scenario/CLI/Launcher.hs index 47cd207aecf..dcdfa0d6001 100644 --- a/lib/jormungandr/test/integration/Test/Integration/Jormungandr/Scenario/CLI/Launcher.hs +++ b/lib/jormungandr/test/integration/Test/Integration/Jormungandr/Scenario/CLI/Launcher.hs @@ -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 @@ -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 diff --git a/lib/jormungandr/test/integration/Test/Utils/Ports.hs b/lib/jormungandr/test/integration/Test/Utils/Ports.hs deleted file mode 100644 index deade049f03..00000000000 --- a/lib/jormungandr/test/integration/Test/Utils/Ports.hs +++ /dev/null @@ -1,43 +0,0 @@ --- | --- Copyright: © 2018-2020 IOHK --- License: Apache-2.0 --- --- Provides functions for checking if TCP ports are available to listen --- on. These can be used to start servers for testing when there may be multiple --- test suites running in parallel. --- --- Includes code from nh2: https://stackoverflow.com/a/57022572 - -module Test.Utils.Ports - ( randomUnusedTCPPorts - , findPort - ) where - -import Prelude - -import Cardano.Wallet.Network.Ports - ( isPortOpen, simpleSockAddr ) -import Control.Monad - ( filterM ) -import Data.List - ( sort ) -import System.Random.Shuffle - ( shuffleM ) - --- | 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