Skip to content

Commit

Permalink
Stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Dec 2, 2020
1 parent 328827c commit f34ce0e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 55 deletions.
1 change: 1 addition & 0 deletions plugins/backend-monitoring/lobemo-backend-monitoring.cabal
Expand Up @@ -53,6 +53,7 @@ test-suite tests
containers,
contra-tracer,
directory,
exceptions,
filepath,
hedgehog >= 1.0,
iohk-monitoring,
Expand Down
102 changes: 47 additions & 55 deletions plugins/backend-monitoring/test/Cardano/BM/Test/Monitoring.lhs
Expand Up @@ -2,7 +2,9 @@

%if style == newcode
\begin{code}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeApplications #-}
module Cardano.BM.Test.Monitoring (
tests
Expand All @@ -26,10 +28,14 @@ import Cardano.BM.Data.Severity
import Cardano.BM.Data.SubTrace
import Cardano.BM.Plugin
import Cardano.BM.Setup
import Control.Monad.Catch (MonadCatch, catch, SomeException)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Cardano.BM.Trace
import GHC.Stack (withFrozenCallStack)
import Test.Tasty
import Test.Tasty.HUnit
import Hedgehog (MonadTest)
import qualified Hedgehog as H
import qualified Test.Tasty.Hedgehog as H
Expand Down Expand Up @@ -183,10 +189,7 @@ actionsTests = testGroup "Actions tests" [
testSetGlobalMinimalSeverity
,
#endif
testCase
"test AlterSeverity"
testAlterSeverity
, H.testProperty "test AlterSeverity" testAlterSeverityH
H.testProperty "test AlterSeverity" testAlterSeverityH
]
\end{code}

Expand Down Expand Up @@ -257,61 +260,48 @@ testSetGlobalMinimalSeverity = do
currentGlobalSeverity == targetGlobalSeverity
#endif
testAlterSeverity :: Assertion
testAlterSeverity = do
let initialSeverity = Warning
targetSeverity = Debug
c <- CM.empty
CM.setSubTrace c "complex" (Just Neutral)
CM.setSeverity c "complex.monitoring" (Just Debug)
CM.setSeverity c "complex.monitoring.monitMe" (Just initialSeverity)
CM.setDefaultBackends c [KatipBK, MonitoringBK]
CM.setSetupBackends c [KatipBK, MonitoringBK]
CM.setBackends c "complex.monitoring.monitMe" (Just [MonitoringBK])
CM.setMonitors c $ HM.fromList
[ ( "complex.monitoring"
, ( Nothing
, Compare "monitMe" (GE, OpMeasurable 10)
, [AlterSeverity "complex.monitoring.monitMe" targetSeverity]
)
)
]
tr' <- startupTraceWithPlugin c "complex"
let tr = appendName "monitoring" tr'
meta <- mkLOMeta Warning Public
traceNamedObject tr (meta, LogValue "monitMe" (PureI 100))
-- procMonitoring <- monitoringThr tr'
-- _ <- Async.waitCatch procMonitoring
threadDelay 10000 -- 10 ms
Just currentSeverity <- CM.inspectSeverity c "complex.monitoring.monitMe"
assertBool ("Severity didn't change! " ++ show currentSeverity) $ targetSeverity == currentSeverity
propertyOnce :: H.PropertyT IO () -> H.Property
propertyOnce = H.withTests 100 . H.withShrinks 0 . H.property
-- tryAll :: MonadCatch m => m a -> m (Either SomeException a)
-- tryAll m =
-- catch (fmap Right m) $ \exception ->
-- case fromException exception :: Maybe AsyncException of
-- Nothing ->
-- pure $ Left exception
-- Just async ->
-- throwM async
evalMoo :: (MonadCatch m, MonadTest m, MonadIO m, HasCallStack) => IO a -> m a
evalMoo f = withFrozenCallStack $ catch (liftIO f) $ \h -> do
H.annotate $ show @SomeException h
H.failure
-- tryAll :: MonadCatch m => m a -> m (Either SomeException a)
-- tryAll m =
-- catch (fmap Right m) $ \exception ->
-- case fromException exception :: Maybe AsyncException of
-- Nothing ->
-- pure $ Left exception
-- Just async ->
-- throwM async
testAlterSeverityH :: H.Property
testAlterSeverityH = propertyOnce $ do
let initialSeverity = Warning
targetSeverity = Debug
c <- H.evalIO CM.empty
c <- evalMoo CM.empty
H.evalIO $ CM.setSubTrace c "complex" (Just Neutral)
H.evalIO $ CM.setSeverity c "complex.monitoring" (Just Debug)
H.evalIO $ CM.setSeverity c "complex.monitoring.monitMe" (Just initialSeverity)
H.evalIO $ CM.setDefaultBackends c [KatipBK, MonitoringBK]
H.evalIO $ CM.setSetupBackends c [KatipBK, MonitoringBK]
evalMoo $ CM.setSubTrace c "complex" (Just Neutral)
evalMoo $ CM.setSeverity c "complex.monitoring" (Just Debug)
evalMoo $ CM.setSeverity c "complex.monitoring.monitMe" (Just initialSeverity)
evalMoo $ CM.setDefaultBackends c [KatipBK, MonitoringBK]
evalMoo $ CM.setSetupBackends c [KatipBK, MonitoringBK]
H.evalIO $ CM.setBackends c "complex.monitoring.monitMe" (Just [MonitoringBK])
evalMoo $ CM.setBackends c "complex.monitoring.monitMe" (Just [MonitoringBK])
H.evalIO . CM.setMonitors c $ HM.fromList
evalMoo $ CM.setMonitors c $ HM.fromList
[ ( "complex.monitoring"
, ( Nothing
, Compare "monitMe" (GE, OpMeasurable 10)
Expand All @@ -320,17 +310,19 @@ testAlterSeverityH = propertyOnce $ do
)
]
tr' <- H.evalIO $ startupTraceWithPlugin c "complex"
tr' <- evalMoo $ startupTraceWithPlugin c "complex"
let tr = appendName "monitoring" tr'
meta <- H.evalIO $ mkLOMeta Warning Public
H.evalIO $ traceNamedObject tr (meta, LogValue "monitMe" (PureI 100))
let !tr = appendName "monitoring" tr'
meta <- evalMoo $ mkLOMeta Warning Public
evalMoo $ traceNamedObject tr (meta, LogValue "monitMe" (PureI 100))
-- procMonitoring <- monitoringThr tr'
-- _ <- Async.waitCatch procMonitoring
H.evalIO $ threadDelay 10000 -- 10 ms
Just currentSeverity <- H.evalIO $ CM.inspectSeverity c "complex.monitoring.monitMe"
H.evalIO $ assertBool ("Severity didn't change! " ++ show currentSeverity) $ targetSeverity == currentSeverity
-- evalMoo $ threadDelay 10000 -- 10 ms
-- Just currentSeverity <- evalMoo $ CM.inspectSeverity c "complex.monitoring.monitMe"
-- targetSeverity H.=== currentSeverity
return ()
-- evalMoo $ assertBool ("Severity didn't change! " ++ show currentSeverity) $ targetSeverity == currentSeverity
\end{code}

0 comments on commit f34ce0e

Please sign in to comment.