packages feed

http-env-gen-0.0.0.0: test/Network/HTTP/Env/FailureBudgetSpec.hs

{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE TypeApplications #-}

module Network.HTTP.Env.FailureBudgetSpec (spec) where

import Control.Monad (replicateM_)
import qualified Data.Sequence as Seq
import Data.Word (Word64)
import Network.HTTP.Env.FailureBudget
import Network.HTTP.Env.FailureBudget.Gen ()
import Network.HTTP.Env.Numeric (intToWord)
import Test.Syd
import Test.Syd.Validity

spec :: Spec
spec = do
  let t0 :: Word64
      t0 = 1_000_000_000_000

  let seconds :: Word64 -> Word64
      seconds n = n * 1_000_000_000

  describe "GenValid" $ do
    genValidSpec @FailureBudgetSettings
    genValidSpec @FailureBudget
    genValidSpec @FailureBudgetStatus
    genValidSpec @FailureBudgetReport

  describe "defaultFailureBudgetSettings" $ do
    it "is valid" $
      shouldBeValid defaultFailureBudgetSettings
    it "allows ten failures an hour" $
      defaultFailureBudgetSettings
        `shouldBe` FailureBudgetSettings
          { failureBudgetSettingsFailures = 10,
            failureBudgetSettingsWindow = 3_600
          }

  describe "Validity FailureBudgetSettings" $ do
    it "accepts the smallest settings that make sense" $
      shouldBeValid
        FailureBudgetSettings
          { failureBudgetSettingsFailures = 1,
            failureBudgetSettingsWindow = 0.000000000001
          }
    it "refuses a budget of no failures" $
      shouldBeInvalid defaultFailureBudgetSettings {failureBudgetSettingsFailures = 0}
    it "refuses a window of zero" $
      shouldBeInvalid defaultFailureBudgetSettings {failureBudgetSettingsWindow = 0}

  let settings =
        FailureBudgetSettings
          { failureBudgetSettingsFailures = 3,
            failureBudgetSettingsWindow = 60
          }

  let empty = emptyFailureBudget settings
  let holding :: [Word64] -> FailureBudget
      holding stamps = empty {failureBudgetFailures = Seq.fromList stamps}

  describe "emptyFailureBudget" $
    it "resolves the settings and holds no failure" $
      emptyFailureBudget settings
        `shouldBe` FailureBudget
          { failureBudgetLimit = 3,
            failureBudgetWindowNanos = seconds 60,
            failureBudgetFailures = Seq.empty
          }

  describe "recordFailure" $ do
    it "records a failure in an empty budget" $
      recordFailure t0 empty `shouldBe` holding [t0]
    it "records the newest failure first" $
      recordFailure (t0 + seconds 1) (holding [t0])
        `shouldBe` holding [t0 + seconds 1, t0]
    it "forgets the oldest failure once it keeps as many as it allows" $
      recordFailure (t0 + seconds 3) (holding [t0 + seconds 2, t0 + seconds 1, t0])
        `shouldBe` holding [t0 + seconds 3, t0 + seconds 2, t0 + seconds 1]
    it "produces a valid budget" $
      forAllValid $ \now ->
        forAllValid $ \budget ->
          shouldBeValid (recordFailure now budget)

  describe "failuresWithin" $ do
    it "counts a failure within a window too long to count in nanoseconds" $
      let budget = recordFailure 1_000_000_000 (emptyFailureBudget defaultFailureBudgetSettings {failureBudgetSettingsWindow = 18446744073.709551616})
       in failuresWithin 2_000_000_000 budget `shouldBe` 1
    it "counts nothing in an empty budget" $
      failuresWithin t0 empty `shouldBe` 0
    it "counts a failure just before the edge of the window" $
      failuresWithin (t0 + seconds 60 - 1) (holding [t0]) `shouldBe` 1
    it "does not count a failure exactly at the edge of the window" $
      failuresWithin (t0 + seconds 60) (holding [t0]) `shouldBe` 0
    it "counts a failure stamped later than the time it is given" $
      failuresWithin t0 (holding [t0 + seconds 1]) `shouldBe` 1
    it "counts only the failures within the window" $
      failuresWithin (t0 + seconds 90) (holding [t0 + seconds 80, t0 + seconds 40, t0])
        `shouldBe` 2
    it "counts at most the budget's limit" $
      forAllValid $ \now ->
        forAllValid $ \budget ->
          failuresWithin now budget `shouldSatisfy` (<= failureBudgetLimit budget)

  describe "failureBudgetReport" $ do
    let recordAll :: [Word64] -> FailureBudget
        recordAll = foldl' (flip recordFailure) empty
    it "is within budget after a single failure" $
      failureBudgetReport t0 (recordAll [t0])
        `shouldBe` FailureBudgetReport
          { failureBudgetReportFailures = 1,
            failureBudgetReportStatus = FailureBudgetStatusWithin
          }
    it "is within budget one failure short of it" $
      failureBudgetReport (t0 + seconds 2) (recordAll [t0, t0 + seconds 1])
        `shouldBe` FailureBudgetReport
          { failureBudgetReportFailures = 2,
            failureBudgetReportStatus = FailureBudgetStatusWithin
          }
    it "is exhausted once the budget's worth of failures falls within the window" $
      failureBudgetReport (t0 + seconds 2) (recordAll [t0, t0 + seconds 1, t0 + seconds 2])
        `shouldBe` FailureBudgetReport
          { failureBudgetReportFailures = 3,
            failureBudgetReportStatus = FailureBudgetStatusExhausted
          }
    it "is within budget when the same failures are spread wider than the window" $
      failureBudgetReport (t0 + seconds 60) (recordAll [t0, t0 + seconds 30, t0 + seconds 60])
        `shouldBe` FailureBudgetReport
          { failureBudgetReportFailures = 2,
            failureBudgetReportStatus = FailureBudgetStatusWithin
          }
    it "is exhausted by the newest failures however many came before" $
      failureBudgetReport (t0 + seconds 102) (recordAll (map (\n -> t0 + seconds n) [0 .. 102]))
        `shouldBe` FailureBudgetReport
          { failureBudgetReportFailures = 3,
            failureBudgetReportStatus = FailureBudgetStatusExhausted
          }
    it "is within budget again once the failures age out" $
      failureBudgetReport (t0 + seconds 62) (recordAll [t0, t0 + seconds 1, t0 + seconds 2])
        `shouldBe` FailureBudgetReport
          { failureBudgetReportFailures = 0,
            failureBudgetReportStatus = FailureBudgetStatusWithin
          }

  describe "Validity FailureBudget" $ do
    it "refuses a budget that keeps more failures than it allows" $
      shouldBeInvalid (holding [t0, t0, t0, t0])
    it "accepts a budget that keeps as many failures as it allows" $
      shouldBeValid (holding [t0, t0, t0])
    it "refuses a budget that allows no failure" $
      shouldBeInvalid empty {failureBudgetLimit = 0}
    it "accepts the largest limit a sequence can count" $
      shouldBeValid empty {failureBudgetLimit = intToWord maxBound}
    it "refuses a limit larger than a sequence can count" $
      shouldBeInvalid empty {failureBudgetLimit = intToWord maxBound + 1}

  describe "FailureBudgetEnv" $ do
    it "reports nothing before any failure" $ do
      env <- newFailureBudget settings
      peekFailureBudget env
        `shouldReturn` FailureBudgetReport
          { failureBudgetReportFailures = 0,
            failureBudgetReportStatus = FailureBudgetStatusWithin
          }
    it "reports the failures recorded into it" $ do
      env <- newFailureBudget settings
      replicateM_ 3 (recordFailureNow env)
      peekFailureBudget env
        `shouldReturn` FailureBudgetReport
          { failureBudgetReportFailures = 3,
            failureBudgetReportStatus = FailureBudgetStatusExhausted
          }