22 lines
890 B
Haskell
22 lines
890 B
Haskell
module BackoffProp (spec) where
|
|
|
|
import Data.Maybe (listToMaybe)
|
|
import Data.Time (NominalDiffTime)
|
|
import qualified Hedgehog.Gen as Gen
|
|
import qualified Hedgehog.Range as Range
|
|
import HomeAssistant.Runtime.Supervisor (Backoff (..), backoffDelay)
|
|
import Test.Hspec (Spec, describe, it)
|
|
import Test.Hspec.Hedgehog (hedgehog, forAll, (===))
|
|
|
|
spec :: Spec
|
|
spec = describe "backoffDelay" $
|
|
it "doubles from base, clamped at cap" $ hedgehog $ do
|
|
baseD <- forAll $ Gen.double (Range.constant 0.0001 10)
|
|
ratio <- forAll $ Gen.double (Range.constant 1 100)
|
|
let base = realToFrac baseD :: NominalDiffTime
|
|
cap = realToFrac (baseD * ratio) :: NominalDiffTime
|
|
backoff = Backoff base cap 1
|
|
delays = map (backoffDelay backoff) [1 .. 100 :: Int]
|
|
listToMaybe delays === Just (min cap base)
|
|
mapM_ (\(a, b) -> b === min cap (a * 2)) (zip delays (drop 1 delays))
|