Skip to content

Replace thread delay with active checking of aggregator #208

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

Merged
1 commit merged into from
May 23, 2022
Merged
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
26 changes: 21 additions & 5 deletions mithril-test-lab/mithril-end-to-end/test/Test/EndToEndSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import CardanoCluster
import CardanoNode (ChainTip (..), RunningNode (..), cliQueryTip)
import Control.Monad.Class.MonadSTM (modifyTVar, newTVarIO, readTVarIO)
import Control.Monad.Class.MonadSay (MonadSay, say)
import Control.Monad.Class.MonadTimer (timeout)
import Control.Tracer (Tracer (Tracer))
import Data.Aeson (Value, eitherDecode)
import qualified Data.Text as Text
Expand All @@ -27,7 +28,14 @@ import Mithril.Aggregator
)
import Mithril.Client (runClient)
import Mithril.Signer (Signer, withSigner)
import Network.HTTP.Simple (getResponseBody, getResponseStatusCode, httpJSON, httpLBS, parseRequest)
import Network.HTTP.Simple
( HttpException,
getResponseBody,
getResponseStatusCode,
httpJSON,
httpLBS,
parseRequest,
)
import System.Directory (listDirectory)
import System.FilePath (takeDirectory, takeExtension, (</>))
import Test.Hydra.Prelude
Expand All @@ -53,7 +61,7 @@ spec =
waitForDBToBePopulated (takeDirectory nodeSocket)
-- Start aggregator service on some random port
withAggregator (takeDirectory nodeSocket) (contramap AggregatorLog tr) $ \aggregator@Aggregator {aggregatorPort} -> do
threadDelay 2
waitForAggregator aggregatorPort
withSigner tmp (contramap SignerLog tr) aggregatorPort node $ \signer -> do
digest <- assertNodeIsProducingSnapshot tr node aggregatorPort
assertSignerIsSigningSnapshot signer aggregatorPort digest
Expand All @@ -63,6 +71,16 @@ spec =
newtype Snapshots = Snapshots [Value]
deriving newtype (FromJSON)

waitForAggregator :: Int -> IO ()
waitForAggregator port = do
req <- parseRequest $ "http://localhost:" <> show port <> "/aggregator/certificate-pending"
timeout 5 (queryAggregator req) >>= \case
Nothing -> failure "Timeout waiting for aggregator to be up"
_ -> pure ()
where
queryAggregator req =
void (httpLBS req) `catch` \(_ :: HttpException) -> threadDelay 0.1 >> queryAggregator req

assertSignerIsSigningSnapshot :: Signer -> Int -> Text -> IO ()
assertSignerIsSigningSnapshot _signer aggregatorPort digest = go 10
where
Expand Down Expand Up @@ -98,9 +116,7 @@ assertNodeIsProducingSnapshot _tracer _cardanoNode aggregatorPort = go 10
204 -> threadDelay 1 >> go (n -1)
200 -> do
CertificatePending {beacon} <- getResponseBody <$> httpJSON request
let digest = digestOf beacon
putStrLn $ "Got beacon : " <> show beacon <> ", computed digest : " <> show digest
pure $ digest
pure $ digestOf beacon
other -> failure $ "unexpected status code: " <> show other

assertNetworkIsProducingBlock :: Tracer IO ClusterLog -> RunningCluster -> IO ()
Expand Down