Gate CallService by handler flags on the Bus
This commit is contained in:
+32
-8
@@ -6,6 +6,7 @@ import AFRP (Request (..), Event (..))
|
||||
import Control.Concurrent.STM
|
||||
( atomically
|
||||
, dupTChan
|
||||
, isEmptyTChan
|
||||
, readTChan
|
||||
, writeTChan
|
||||
)
|
||||
@@ -15,9 +16,11 @@ import Data.Time (UTCTime (..), utc)
|
||||
import Data.UUID (nil)
|
||||
import HomeAssistant.Controller (HASSEff (..), Service (..), Target(..))
|
||||
import HomeAssistant.Runtime.Bus
|
||||
import HomeAssistant.Runtime.Flags (loadFlags, setEnabled)
|
||||
import HomeAssistant.Runtime.Metrics (registerAppMetrics)
|
||||
import Katip (Namespace (Namespace), runKatipContextT, Severity (..))
|
||||
import qualified System.Metrics as Metrics
|
||||
import System.IO.Temp (withTempDirectory)
|
||||
import Test.Hspec
|
||||
|
||||
spec :: Spec
|
||||
@@ -40,6 +43,15 @@ spec = describe "Bus" $ do
|
||||
(_, svc') <- atomically $ readTChan (busOutbound bus)
|
||||
svc' `shouldBe` svc
|
||||
|
||||
it "channelHassEval drops CallService when the handler is disabled" $
|
||||
withTestFlagsBus $ \bus -> do
|
||||
setEnabled (busFlags bus) "test-handler" False
|
||||
let req = Request (UTCTime (toEnum 0) 0) utc nil "test-handler"
|
||||
svc = Service "light" "turn_on" Nothing [EntityId "light.test"]
|
||||
runKatipContextT (busLogEnv bus) () (Namespace ["test"]) $
|
||||
channelHassEval bus (CallService req svc)
|
||||
atomically (isEmptyTChan (busOutbound bus)) `shouldReturn` True
|
||||
|
||||
|
||||
it "generates unique sequential call ids" $ do
|
||||
gen <- mkCallIdGen 0
|
||||
@@ -51,16 +63,28 @@ spec = describe "Bus" $ do
|
||||
it "increments the trigger and service counters" $ do
|
||||
store <- Metrics.newStore
|
||||
m <- registerAppMetrics store
|
||||
withBus InfoS m $ \bus -> do
|
||||
recordInbound bus
|
||||
recordInbound bus
|
||||
recordOutbound bus
|
||||
sample <- Metrics.sampleAll store
|
||||
HM.lookup "hass.trigger.in" sample `shouldBe` Just (Metrics.Counter 2)
|
||||
HM.lookup "hass.service.out" sample `shouldBe` Just (Metrics.Counter 1)
|
||||
withTempDirectory "/tmp" "bus-spec" $ \dir -> do
|
||||
flags <- loadFlags dir []
|
||||
withBus InfoS m flags $ \bus -> do
|
||||
recordInbound bus
|
||||
recordInbound bus
|
||||
recordOutbound bus
|
||||
sample <- Metrics.sampleAll store
|
||||
HM.lookup "hass.trigger.in" sample `shouldBe` Just (Metrics.Counter 2)
|
||||
HM.lookup "hass.service.out" sample `shouldBe` Just (Metrics.Counter 1)
|
||||
|
||||
withTestBus :: (Bus -> IO a) -> IO a
|
||||
withTestBus action = do
|
||||
store <- Metrics.newStore
|
||||
m <- registerAppMetrics store
|
||||
withBus InfoS m action
|
||||
withTempDirectory "/tmp" "bus-spec" $ \dir -> do
|
||||
flags <- loadFlags dir []
|
||||
withBus InfoS m flags action
|
||||
|
||||
withTestFlagsBus :: (Bus -> IO a) -> IO a
|
||||
withTestFlagsBus action = do
|
||||
store <- Metrics.newStore
|
||||
m <- registerAppMetrics store
|
||||
withTempDirectory "/tmp" "bus-spec" $ \dir -> do
|
||||
flags <- loadFlags dir ["test-handler"]
|
||||
withBus InfoS m flags action
|
||||
|
||||
Reference in New Issue
Block a user