Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PORT_ tests for Jörmungandr #506

Merged
merged 3 commits into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions lib/core/test/integration/Test/Integration/Framework/Request.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ module Test.Integration.Framework.Request

import Prelude

import Control.Concurrent.Async
( Async )
import Control.Monad.Catch
( Exception (..), MonadCatch (..), throwM )
import Control.Monad.IO.Class
Expand All @@ -39,8 +37,6 @@ import Data.Proxy
( Proxy (..) )
import Data.Text
( Text )
import Database.Persist.Sqlite
( SqlBackend )
import GHC.Generics
( Generic )
import Network.HTTP.Client
Expand All @@ -66,8 +62,6 @@ import Network.Wai.Handler.Warp
( Port )
import Numeric.Natural
( Natural )
import System.IO
( Handle )
import Test.Integration.Faucet
( Faucet )

Expand All @@ -79,24 +73,19 @@ import qualified Network.HTTP.Types.Status as HTTP

-- | Running Context for our integration test
data Context t = Context
{ _cluster
:: Async ()
-- ^ A handle to the running cluster / chain producer
{ _cleanup
:: IO ()
-- ^ An action to clean up open processes after tests
, _manager
:: (Text, Manager)
-- ^ The underlying BaseUrl and Manager used by the Wallet Client
, _port
:: Port
-- ^ Server TCP port
, _logs
:: Handle
-- ^ A file 'Handle' to the launcher log output
, _faucet
:: Faucet
-- ^ A 'Faucet' handle in to have access to funded wallets in
-- integration tests.
, _db :: SqlBackend
-- ^ A database connection handle
, _feeEstimator :: TxDescription -> (Natural, Natural)
-- ^ A fee estimator for the integration tests
, _target
Expand Down
1 change: 0 additions & 1 deletion lib/http-bridge/cardano-wallet-http-bridge.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ test-suite integration
, http-types
, iohk-monitoring
, persistent
, persistent-sqlite
, process
, retry
, template-haskell
Expand Down
15 changes: 6 additions & 9 deletions lib/http-bridge/test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ main = do
$ describe "with random port" $ do
PortCLI.specCommon @t
PortCLI.specWithRandomPort @t defaultPort
beforeAll startCluster $
afterAll killCluster $ after tearDown $ do
beforeAll startCluster $ afterAll _cleanup $ after tearDown $ do
describe "Wallets API endpoint tests" (Wallets.spec @t)
describe "Transactions API endpoint tests" (Transactions.spec @t)
describe "Addresses API endpoint tests" (Addresses.spec @t)
Expand Down Expand Up @@ -208,13 +207,11 @@ main = do
manager <- (baseURL,) <$> newManager defaultManagerSettings
faucet <- putStrLn "Creating money out of thin air..." *> initFaucet nl
let estimator = mkFeeEstimator byronFeePolicy
return $ Context cluster manager port handle faucet db estimator Proxy

killCluster :: Context t -> IO ()
killCluster ctx = do
cancel (_cluster ctx)
hClose (_logs ctx)
close' (_db ctx)
let cleanup = do
cancel cluster
hClose handle
close' db
return $ Context cleanup manager port faucet estimator Proxy

killServer :: (HasType ThreadId s, HasType SqlBackend s) => s -> IO ()
killServer ctx = do
Expand Down
1 change: 0 additions & 1 deletion lib/jormungandr/cardano-wallet-jormungandr.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ test-suite integration
, iohk-monitoring
, memory
, persistent
, persistent-sqlite
, process
, QuickCheck
, retry
Expand Down
57 changes: 39 additions & 18 deletions lib/jormungandr/test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ import Cardano.Wallet.Primitive.Types
import Cardano.Wallet.Unsafe
( unsafeFromHex, unsafeRunExceptT )
import Control.Concurrent
( forkIO )
( threadDelay )
import Control.Concurrent.Async
( async, cancel )
( Async, async, cancel )
import Control.Concurrent.MVar
( newEmptyMVar, putMVar, takeMVar )
import Control.Monad
Expand All @@ -56,6 +56,8 @@ import Data.Coerce
( coerce )
import Data.Function
( (&) )
import Data.Maybe
( fromMaybe )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
Expand Down Expand Up @@ -94,6 +96,7 @@ import qualified Test.Integration.Scenario.API.Wallets as Wallets
import qualified Test.Integration.Scenario.CLI.Addresses as AddressesCLI
import qualified Test.Integration.Scenario.CLI.Miscellaneous as MiscellaneousCLI
import qualified Test.Integration.Scenario.CLI.Mnemonics as MnemonicsCLI
import qualified Test.Integration.Scenario.CLI.Port as PortCLI
import qualified Test.Integration.Scenario.CLI.Transactions as TransactionsCLI
import qualified Test.Integration.Scenario.CLI.Wallets as WalletsCLI

Expand All @@ -106,7 +109,8 @@ main = hspec $ do
describe "Cardano.Wallet.NetworkSpec" Network.spec
describe "Mnemonics CLI tests" (MnemonicsCLI.spec @t)
describe "Miscellaneous CLI tests" (MiscellaneousCLI.spec @t)
beforeAll start $ afterAll cleanup $ after tearDown $ do
describe "Ports CLI (negative) tests" (PortCLI.specNegative @t)
beforeAll (start Nothing) $ afterAll _cleanup $ after tearDown $ do
-- API e2e Testing
describe "Addresses API endpoint tests" Addresses.spec
describe "Transactions API endpoint tests" Transactions.spec
Expand All @@ -116,9 +120,21 @@ main = hspec $ do
describe "Server CLI tests" (ServerCLI.spec @t)
describe "Transactions CLI tests" (TransactionsCLI.spec @t)
describe "Wallets CLI tests" (WalletsCLI.spec @t)
describe "Ports CLI (default) tests" $ do
PortCLI.specCommon @t
PortCLI.specWithDefaultPort @t
let explicitPort = Just $ ListenOnPort defaultPort
beforeAll (start explicitPort) $ afterAll _cleanup $ after tearDown $ do
describe "Ports CLI (explicit) tests" $ do
PortCLI.specCommon @t
let randomPort = Just ListenOnRandomPort
beforeAll (start randomPort) $ afterAll _cleanup $ after tearDown $ do
describe "Ports CLI (random) tests" $ do
PortCLI.specCommon @t
PortCLI.specWithRandomPort @t defaultPort
where
start :: IO (Context (Jormungandr 'Testnet))
start = do
start :: Maybe Listen -> IO (Context (Jormungandr 'Testnet))
start listen = do
let dir = "./test/data/jormungandr"
removePathForcibly "/tmp/cardano-wallet-jormungandr"
logs <- openFile "/tmp/jormungandr" WriteMode
Expand All @@ -130,18 +146,18 @@ main = hspec $ do
] (return ())
(UseHandle logs)
handle <- async $ void $ launch [jormungandrLauncher]
(port, feePolicy, db) <- cardanoWalletServer
(handle', port, feePolicy, db) <- cardanoWalletServer listen
let baseUrl = "http://localhost:" <> T.pack (show port) <> "/"
manager <- (baseUrl,) <$> newManager defaultManagerSettings
faucet <- initFaucet
let estimator = mkFeeEstimator feePolicy
return $ Context handle manager port logs faucet db estimator Proxy

cleanup :: Context t -> IO ()
cleanup ctx = do
cancel (_cluster ctx)
hClose (_logs ctx)
close' (_db ctx)
let cleanup = do
cancel handle
cancel handle'
hClose logs
close' db
threadDelay oneSecond
return $ Context cleanup manager port faucet estimator Proxy

-- | Initialize logging at the specified minimum 'Severity' level.
initTracer :: Severity -> Text -> IO (Trace IO Text)
Expand All @@ -155,22 +171,23 @@ initTracer minSeverity cmd = do
-- code coverage measures from running the scenarios on top of it!
cardanoWalletServer
:: forall network. (network ~ Jormungandr 'Testnet)
=> IO (Int, FeePolicy, SqlBackend)
cardanoWalletServer = do
=> Maybe Listen
-> IO (Async (), Int, FeePolicy, SqlBackend)
cardanoWalletServer mlisten = do
logConfig <- CM.empty
tracer <- initTracer Info "serve"
(nl, block0, feePolicy) <- newNetworkLayer jormungandrUrl block0H
(conn, db) <- Sqlite.newDBLayer @_ @network logConfig tracer Nothing
mvar <- newEmptyMVar
void $ forkIO $ do
handle <- async $ do
let tl = Jormungandr.newTransactionLayer block0H
wallet <- newWalletLayer tracer block0 feePolicy db nl tl
let listen = ListenOnRandomPort
let listen = fromMaybe (ListenOnPort defaultPort) mlisten
Server.withListeningSocket listen $ \(port, socket) -> do
let settings = Warp.defaultSettings
& setBeforeMainLoop (putMVar mvar port)
Server.start settings tracer socket wallet
(,feePolicy,conn) <$> takeMVar mvar
(handle,,feePolicy,conn) <$> takeMVar mvar
where
jormungandrUrl :: BaseUrl
jormungandrUrl = BaseUrl Http "localhost" 8080 "/api"
Expand Down Expand Up @@ -220,3 +237,7 @@ mkFeeEstimator policy (TxDescription nInps nOuts) =
-- | One second in micro-seconds
oneSecond :: Int
oneSecond = 1000000

-- | Default port for the wallet server
defaultPort :: Int
defaultPort = 8090
3 changes: 1 addition & 2 deletions nix/.stack.nix/cardano-wallet-http-bridge.nix

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

3 changes: 1 addition & 2 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.