Skip to content

Commit

Permalink
run the wallet server in the same thread as the test scenario
Browse files Browse the repository at this point in the history
Before, we ran this in a separate process which, as a major downside, has been escaping any
form of coverage measurement. Having it ran in another _thread_ solves this problem
  • Loading branch information
KtorZ committed May 6, 2019
1 parent 52496a0 commit 3aecf52
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
2 changes: 2 additions & 0 deletions lib/http-bridge/cardano-wallet-http-bridge.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ test-suite integration
, http-api-data
, http-types
, process
, servant-server
, template-haskell
, text
, text-class
, time
, transformers
, warp
type:
exitcode-stdio-1.0
hs-source-dirs:
Expand Down
59 changes: 50 additions & 9 deletions lib/http-bridge/test/integration/Main.hs
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}

module Main where

import Prelude

import Cardano.Environment
( Network (..), network )
import Cardano.Launcher
( Command (..), StdStream (..), launch )
import Cardano.Wallet
( mkWalletLayer )
import Cardano.Wallet.Api
( Api )
import Cardano.Wallet.Api.Server
( server )
import Cardano.Wallet.Compatibility.HttpBridge
( HttpBridge )
import Control.Concurrent
( threadDelay )
( forkIO, threadDelay )
import Control.Concurrent.Async
( async, cancel, link )
import Control.Monad
( void )
import Data.Function
( (&) )
import Data.Proxy
( Proxy (..) )
import Data.Time
( addUTCTime, defaultTimeLocale, formatTime, getCurrentTime )
import Network.HTTP.Client
( defaultManagerSettings, newManager )
import System.Environment
( setEnv )
import Servant
( (:>), serve )
import System.IO
( IOMode (..), hClose, openFile )
import Test.Hspec
( after, afterAll, beforeAll, describe, hspec )
import Test.Integration.Framework.DSL
( Context (..), tearDown )

import qualified Cardano.Wallet.DB.MVar as MVar
import qualified Cardano.Wallet.Network.HttpBridge as HttpBridge
import qualified Cardano.Wallet.Network.HttpBridgeSpec as HttpBridge
import qualified Cardano.Wallet.Transaction.HttpBridge as HttpBridge
import qualified Cardano.WalletSpec as Wallet
import qualified Network.Wai.Handler.Warp as Warp
import qualified Test.Integration.Scenario.Wallets as Wallets

main :: IO ()
main = do
case network of
Testnet ->
return ()
_ ->
fail $ "unsupported integration environment: " <> show network
hspec $ do
describe "Cardano.WalletSpec" Wallet.spec
describe "Cardano.Wallet.Network.HttpBridge" HttpBridge.spec
Expand All @@ -52,9 +79,10 @@ main = do
, cardanoNodeSimple stateDir systemStart ("core1", "127.0.0.1:3001")
, cardanoNodeSimple stateDir systemStart ("core2", "127.0.0.1:3002")
, cardanoNodeSimple stateDir systemStart ("relay", "127.0.0.1:3100")
, cardanoWalletLauncher "1337" "8080" "local" handle
, cardanoHttpBridge "8080" "local" handle
]
link cluster
cardanoWalletServer 1337 8080
let baseURL = "http://localhost:1337/"
manager <- newManager defaultManagerSettings
threadDelay (2 * startUpDelay)
Expand All @@ -80,9 +108,22 @@ main = do
] (pure ())
NoStream

cardanoWalletLauncher serverPort bridgePort network handle = Command
"cardano-wallet-launcher"
[ "--wallet-server-port", serverPort
, "--http-bridge-port", bridgePort
] (setEnv "NETWORK" network *> threadDelay startUpDelay)
cardanoHttpBridge port template handle = Command
"cardano-http-bridge"
[ "start"
, "--template", template
, "--port", port
] (threadDelay startUpDelay)
(UseHandle handle)

-- NOTE
-- We start the wallet server in the same process such that we get
-- code coverage measures from running the scenarios on top of it!
cardanoWalletServer walletPort bridgePort = void $ forkIO $ do
threadDelay startUpDelay
db <- MVar.newDBLayer
nl <- HttpBridge.newNetworkLayer bridgePort
let tl = HttpBridge.newTransactionLayer
let wallet = mkWalletLayer @_ @HttpBridge db nl tl
let settings = Warp.defaultSettings & Warp.setPort walletPort
Warp.runSettings settings (serve (Proxy @("v2" :> Api)) (server wallet))

0 comments on commit 3aecf52

Please sign in to comment.