Skip to content

Commit

Permalink
Merge #1683
Browse files Browse the repository at this point in the history
1683: Assert that the specified port is open by nodes in the chairman cluster r=newhoggy a=newhoggy



Co-authored-by: John Ky <john.ky@iohk.io>
  • Loading branch information
iohk-bors[bot] and newhoggy committed Aug 13, 2020
2 parents ed1c93a + 94df030 commit 35bb9ad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 7 additions & 1 deletion cardano-node/test/Test/Cardano/Node/Chairman.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import qualified Hedgehog as H
import qualified System.IO as IO
import qualified System.Process as IO
import qualified Test.Common.Base as H
import qualified Test.Common.Network as IO
import qualified Test.Common.Process as H

prop_spawnOneNode :: Property
prop_spawnOneNode = H.propertyOnce . H.workspace "temp/chairman" $ \tempDir -> do
let nodeCount = 3
base <- H.noteShowM H.getProjectBase
baseConfig <- H.noteShow $ base <> "/configuration/chairman/defaults/simpleview"
currentTime <- H.noteShowIO DTC.getCurrentTime
Expand All @@ -43,7 +45,7 @@ prop_spawnOneNode = H.propertyOnce . H.workspace "temp/chairman" $ \tempDir -> d
]

-- Launch cluster of three nodes
forM_ [0..2] $ \i -> do
forM_ [0..nodeCount - 1] $ \i -> do
si <- H.noteShow $ show @Int i
dbDir <- H.noteShow $ tempDir <> "/db/node-" <> si
socketDir <- H.noteShow $ tempDir <> "/socket"
Expand Down Expand Up @@ -82,6 +84,10 @@ prop_spawnOneNode = H.propertyOnce . H.workspace "temp/chairman" $ \tempDir -> d

return (hIn, hProcess)

deadline <- H.noteShowIO $ DTC.addUTCTime 180 <$> DTC.getCurrentTime -- 60 seconds from now

forM_ [0..nodeCount - 1] $ \i -> H.assertByDeadlineIO deadline $ IO.isPortOpen (3000 + i)

H.threadDelay 10000000

tests :: IO Bool
Expand Down
22 changes: 21 additions & 1 deletion cardano-node/test/Test/Common/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ module Test.Common.Base
, noteShowM
, noteShowIO
, noteTempFile
, assertByDeadlineIO
, Integration
) where

import Control.Monad
import Control.Monad.IO.Class (liftIO)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Morph (hoist)
import Control.Monad.Trans.Resource (ResourceT, runResourceT)
import Data.Bool
Expand All @@ -26,8 +27,10 @@ import Data.Function (($), (.))
import Data.Int
import Data.Maybe (Maybe (..), listToMaybe, maybe)
import Data.Monoid (Monoid (..))
import Data.Ord
import Data.Semigroup (Semigroup (..))
import Data.String (String)
import Data.Time.Clock (UTCTime)
import Data.Tuple
import GHC.Stack (CallStack, HasCallStack, callStack, getCallStack)
import Hedgehog (MonadTest)
Expand All @@ -37,6 +40,7 @@ import System.IO (FilePath, IO)
import Text.Show

import qualified Control.Concurrent as IO
import qualified Data.Time.Clock as DTC
import qualified GHC.Stack as GHC
import qualified Hedgehog as H
import qualified Hedgehog.Internal.Property as H
Expand Down Expand Up @@ -119,3 +123,19 @@ noteTempFile tempDir filePath = GHC.withFrozenCallStack $ do
let relPath = tempDir <> "/" <> filePath
H.annotate relPath
return relPath

-- | Run the operation 'f' once a second until it returns 'True' or the deadline expires.
--
-- Expiration of the deadline results in an assertion failure
assertByDeadlineIO :: (MonadIO m, HasCallStack) => UTCTime -> IO Bool -> H.PropertyT m ()
assertByDeadlineIO deadline f = GHC.withFrozenCallStack $ do
success <- liftIO f
unless success $ do
currentTime <- liftIO DTC.getCurrentTime
if currentTime < deadline
then do
liftIO $ IO.threadDelay 1000000
assertByDeadlineIO deadline f
else do
H.annotateShow currentTime
failWithCustom GHC.callStack Nothing "Condition not met by deadline"

0 comments on commit 35bb9ad

Please sign in to comment.