-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
lib/local-cluster/test/unit/Cardano/Wallet/Launch/Cluster/Monitoring/Http/APISpec.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
module Cardano.Wallet.Launch.Cluster.Monitoring.Http.APISpec | ||
( spec | ||
) | ||
where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.Launch.Cluster.Monitoring.Http.API | ||
( ApiT (..) | ||
) | ||
import Cardano.Wallet.Launch.Cluster.Monitoring.Phase | ||
( History (..) | ||
, Phase (..) | ||
) | ||
import Control.Monitoring.Tracing | ||
( MonitorState (..) | ||
) | ||
import Data.Aeson | ||
( FromJSON (..) | ||
, Result (..) | ||
, ToJSON (..) | ||
, fromJSON | ||
) | ||
import Data.Time | ||
( Day (ModifiedJulianDay) | ||
, UTCTime (UTCTime) | ||
, secondsToDiffTime | ||
) | ||
import Test.Hspec | ||
( Spec | ||
, describe | ||
, it | ||
, shouldBe | ||
) | ||
import Test.QuickCheck | ||
( Arbitrary (..) | ||
, Gen | ||
, elements | ||
, forAll | ||
, listOf | ||
, oneof | ||
) | ||
|
||
import qualified Data.Map as Map | ||
|
||
jsonRoundtrip :: (ToJSON a, FromJSON a, Eq a, Show a) => a -> IO () | ||
jsonRoundtrip a = fromJSON (toJSON a) `shouldBe` Success a | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "observe end-point json instances" | ||
$ it " roundtrips" $ forAll genObserveApiType jsonRoundtrip | ||
|
||
genObserveApiType :: Gen (ApiT (History, MonitorState)) | ||
genObserveApiType = do | ||
history' <- | ||
History . Map.fromList <$> listOf ((,) <$> genUTCTime <*> genPhase) | ||
state <- oneof [pure Wait, pure Step, pure Run] | ||
pure $ ApiT (history', state) | ||
|
||
genUTCTime :: Gen UTCTime | ||
genUTCTime = do | ||
day <- ModifiedJulianDay <$> arbitrary | ||
seconds <- secondsToDiffTime . (`mod` (3600 * 24)) <$> arbitrary | ||
pure $ UTCTime day seconds | ||
|
||
genPhase :: Gen Phase | ||
genPhase = | ||
elements | ||
[ RetrievingFunds | ||
, Metadata | ||
, Genesis | ||
, Pool0 | ||
, Funding | ||
, Pools | ||
, Relay | ||
, Cluster Nothing | ||
] |
120 changes: 120 additions & 0 deletions
120
lib/local-cluster/test/unit/Cardano/Wallet/Launch/Cluster/Monitoring/MonitorSpec.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
module Cardano.Wallet.Launch.Cluster.Monitoring.MonitorSpec | ||
( spec | ||
) | ||
where | ||
|
||
import Prelude | ||
|
||
import Cardano.Wallet.Launch.Cluster.Monitoring.Http.Client | ||
( Query (..) | ||
, RunQuery (..) | ||
) | ||
import Cardano.Wallet.Launch.Cluster.Monitoring.Monitor | ||
( MonitorConfiguration (..) | ||
, withMonitoring | ||
) | ||
import Cardano.Wallet.Launch.Cluster.Monitoring.Phase | ||
( History (..) | ||
, Phase (..) | ||
) | ||
import Control.Monad | ||
( unless | ||
) | ||
import Control.Monad.Cont | ||
( evalContT | ||
) | ||
import Control.Monad.Fix | ||
( fix | ||
) | ||
import Control.Monad.IO.Class | ||
( liftIO | ||
) | ||
import Control.Monitoring.Tracing | ||
( MonitorState (..) | ||
) | ||
import Control.Tracer | ||
( Tracer | ||
, nullTracer | ||
, traceWith | ||
) | ||
import Data.Foldable | ||
( toList | ||
) | ||
import Test.Hspec | ||
( Spec | ||
, describe | ||
, it | ||
, shouldBe | ||
) | ||
import UnliftIO.Async | ||
( async | ||
, wait | ||
) | ||
|
||
testMonitoring | ||
:: MonitorState | ||
-> (Tracer IO Phase -> RunQuery IO -> IO ()) | ||
-> IO () | ||
testMonitoring w f = | ||
evalContT $ do | ||
(tracer, query) <- | ||
withMonitoring nullTracer | ||
$ MonitorConfiguration Nothing w | ||
liftIO $ f tracer query | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "withMonitoring" $ do | ||
it "can start" $ do | ||
testMonitoring Step $ \_ _ -> pure () | ||
it "can query" $ do | ||
testMonitoring Step $ \_ (RunQuery query) -> do | ||
result <- query ReadyQ | ||
result `shouldBe` False | ||
it "can trace" $ do | ||
testMonitoring Run $ \tracer _ -> do | ||
traceWith tracer RetrievingFunds | ||
it "can report readiness" $ do | ||
testMonitoring Run $ \tracer (RunQuery query) -> do | ||
traceWith tracer (Cluster Nothing) | ||
result <- query ReadyQ | ||
result `shouldBe` True | ||
it "can step the tracer thread" $ do | ||
testMonitoring Step $ \tracer (RunQuery query) -> do | ||
tracer' <- async $ do | ||
traceWith tracer (Cluster Nothing) | ||
fix $ \loop -> do | ||
result <- query ReadyQ | ||
unless result $ query StepQ >> loop | ||
wait tracer' | ||
it "can report the phase history" $ do | ||
testMonitoring Run $ \tracer (RunQuery query) -> do | ||
traceWith tracer RetrievingFunds | ||
traceWith tracer Metadata | ||
traceWith tracer Genesis | ||
traceWith tracer Pool0 | ||
traceWith tracer Funding | ||
traceWith tracer Pools | ||
traceWith tracer Relay | ||
traceWith tracer (Cluster Nothing) | ||
(History phases, state) <- query ObserveQ | ||
toList phases | ||
`shouldBe` [ RetrievingFunds | ||
, Metadata | ||
, Genesis | ||
, Pool0 | ||
, Funding | ||
, Pools | ||
, Relay | ||
, Cluster Nothing | ||
] | ||
state `shouldBe` Run | ||
it "can switch from step to run" $ do | ||
testMonitoring Step $ \tracer (RunQuery query) -> do | ||
tracer' <- async $ do | ||
traceWith tracer RetrievingFunds | ||
state <- query SwitchQ | ||
state `shouldBe` Run | ||
wait tracer' | ||
(History phases, _state) <- query ObserveQ | ||
toList phases `shouldBe` [RetrievingFunds] |