Make flag-decision tests fail fast and assert the outbound metric

This commit is contained in:
2026-09-30 08:57:40 +03:00
parent 526710ef71
commit aee34f4e14
2 changed files with 13 additions and 9 deletions
+5 -3
View File
@@ -21,6 +21,7 @@ import HomeAssistant.Runtime.Metrics (registerAppMetrics)
import Katip (Namespace (Namespace), runKatipContextT, Severity (..)) import Katip (Namespace (Namespace), runKatipContextT, Severity (..))
import qualified System.Metrics as Metrics import qualified System.Metrics as Metrics
import System.IO.Temp (withTempDirectory) import System.IO.Temp (withTempDirectory)
import System.Timeout (timeout)
import Test.Hspec import Test.Hspec
spec :: Spec spec :: Spec
@@ -50,9 +51,10 @@ spec = describe "Bus" $ do
svc = Service "light" "turn_on" Nothing [EntityId "light.test"] svc = Service "light" "turn_on" Nothing [EntityId "light.test"]
runKatipContextT (busLogEnv bus) () (Namespace ["test"]) $ runKatipContextT (busLogEnv bus) () (Namespace ["test"]) $
channelHassEval bus (CallService req svc) channelHassEval bus (CallService req svc)
(_, svc') <- atomically $ readTChan (busOutbound bus) result <- timeout 1000000 $ atomically $ readTChan (busOutbound bus)
svc' `shouldBe` svc case result of
Nothing -> expectationFailure "expected a write to the outbound channel"
Just (_, svc') -> svc' `shouldBe` svc
it "generates unique sequential call ids" $ do it "generates unique sequential call ids" $ do
gen <- mkCallIdGen 0 gen <- mkCallIdGen 0
+8 -6
View File
@@ -3,9 +3,8 @@
module ConnectionSpec (spec) where module ConnectionSpec (spec) where
import AFRP (Request(..)) import AFRP (Request(..))
import Control.Monad.IO.Class (liftIO)
import Data.Aeson (object, (.=)) import Data.Aeson (object, (.=))
import Data.Foldable (forM_) import qualified Data.HashMap.Strict as HM
import Data.Maybe (fromJust) import Data.Maybe (fromJust)
import Data.Text (Text) import Data.Text (Text)
import Data.Time (UTCTime (..), utc) import Data.Time (UTCTime (..), utc)
@@ -19,6 +18,7 @@ import HomeAssistant.Runtime.Connection (dispatchService, encodeService, isTrigg
import Katip (Namespace (Namespace), Severity (InfoS), runKatipContextT) import Katip (Namespace (Namespace), Severity (InfoS), runKatipContextT)
import System.IO.Temp (withTempDirectory) import System.IO.Temp (withTempDirectory)
import qualified System.Metrics as Metrics import qualified System.Metrics as Metrics
import System.Timeout (timeout)
import Test.Hspec import Test.Hspec
spec :: Spec spec :: Spec
@@ -63,10 +63,12 @@ spec = do
flags <- loadFlags dir ["test"] flags <- loadFlags dir ["test"]
setEnabled flags "test" False setEnabled flags "test" False
withBus InfoS m flags $ \bus -> do withBus InfoS m flags $ \bus -> do
runKatipContextT (busLogEnv bus) () (Namespace ["connection"]) $ timeout 1000000 (do
dispatchService limiter bus (req 1) (lightOn [EntityId "light.test"]) runKatipContextT (busLogEnv bus) () (Namespace ["connection"]) $
generateCallId (busGen bus) `shouldReturn` 1 dispatchService limiter bus (req 1) (lightOn [EntityId "light.test"])
generateCallId (busGen bus)) `shouldReturn` Just 1
sample <- Metrics.sampleAll store
HM.lookup "hass.service.out" sample `shouldBe` Just (Metrics.Counter 0)
req :: Int -> Request req :: Int -> Request
req n = Request (UTCTime (toEnum 0) (fromIntegral (0 :: Int))) utc req n = Request (UTCTime (toEnum 0) (fromIntegral (0 :: Int))) utc