packages feed

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

{-# LANGUAGE ScopedTypeVariables #-}

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

import Control.Concurrent.TokenLimiter.Concurrent
  ( Count,
    TokenLimitConfig (..),
    TokenLimiter (..),
  )
import Network.HTTP.Env.Pacing
import Test.Syd
import Test.Syd.Validity
import UnliftIO.Async (replicateConcurrently)

spec :: Spec
spec = do
  describe "requestsPerHourConfig" $ do
    it "affords exactly the configured number of requests an hour" $
      forAllValid $ \(requestsPerHour :: Count) -> do
        tokenLimitConfigTokensPerSecond (requestsPerHourConfig requestsPerHour)
          `shouldBe` max 1 requestsPerHour
        requestTokenCost `shouldBe` 3600

    it "never builds a bucket that cannot refill" $
      tokenLimitConfigTokensPerSecond (requestsPerHourConfig 0)
        `shouldBe` 1

    it "starts full, so a quiet key can answer a burst at once" $
      forAllValid $ \(requestsPerHour :: Count) -> do
        let config = requestsPerHourConfig requestsPerHour
        tokenLimitConfigInitialTokens config
          `shouldBe` tokenLimitConfigMaxTokens config

    it "holds a burst of fifty requests" $
      forAllValid $ \(requestsPerHour :: Count) ->
        tokenLimitConfigMaxTokens (requestsPerHourConfig requestsPerHour)
          `shouldBe` 180000

  describe "keyedLimiter" $ do
    it "gives the same key the same limiter" $
      forAllValid $ \(key :: Int) -> do
        limiters <- newKeyedLimiters 4500
        first <- keyedLimiter limiters key
        second <- keyedLimiter limiters key
        (first == second) `shouldBe` True

    it "gives different keys different limiters" $
      forAllValid $ \(key :: Int) -> do
        limiters <- newKeyedLimiters 4500
        first <- keyedLimiter limiters key
        second <- keyedLimiter limiters (key + 1)
        (first == second) `shouldBe` False

    it "keeps every key's limiter as the others are used" $
      forAllValid $ \(key :: Int) -> do
        limiters <- newKeyedLimiters 4500
        one <- keyedLimiter limiters key
        other <- keyedLimiter limiters (key + 1)
        oneAgain <- keyedLimiter limiters key
        otherAgain <- keyedLimiter limiters (key + 1)
        (one == oneAgain, other == otherAgain) `shouldBe` (True, True)

    it "gives concurrent callers on one key the same limiter" $
      forAllValid $ \(key :: Int) -> do
        limiters <- newKeyedLimiters 4500
        gotten <- replicateConcurrently 16 (keyedLimiter limiters key)
        case gotten of
          [] -> expectationFailure "asked for no limiters"
          firstLimiter : rest -> all (== firstLimiter) rest `shouldBe` True

  describe "repaceRequestLimiter" $ do
    it "paces at the new rate" $ do
      limiter <- newRequestLimiter 4500
      repaceRequestLimiter limiter 9000
      tokenLimiter <- requestLimiterTokenLimiter limiter
      tokenLimitConfigTokensPerSecond (tokenLimiterConfig tokenLimiter) `shouldBe` 9000

    it "keeps the same bucket when the rate is the one it already paces at" $ do
      limiter <- newRequestLimiter 9000
      beforehand <- requestLimiterTokenLimiter limiter
      repaceRequestLimiter limiter 9000
      afterwards <- requestLimiterTokenLimiter limiter
      (tokenLimiterLastServiced beforehand == tokenLimiterLastServiced afterwards) `shouldBe` True

  describe "observeRequestsPerHour" $ do
    it "paces a key at what the dependency says it is allowed" $
      forAllValid $ \(key :: Int) ->
        forAllValid $ \(generated :: Count) -> do
          let allowed = 1 + min generated 99999
          limiters <- newKeyedLimiters 4500
          observeRequestsPerHour limiters key allowed
          tokenLimiter <- keyedLimiter limiters key >>= requestLimiterTokenLimiter
          tokenLimitConfigTokensPerSecond (tokenLimiterConfig tokenLimiter) `shouldBe` allowed

    it "repaces a limiter that was already handed out" $
      forAllValid $ \(key :: Int) -> do
        limiters <- newKeyedLimiters 4500
        limiter <- keyedLimiter limiters key
        observeRequestsPerHour limiters key 9000
        tokenLimiter <- requestLimiterTokenLimiter limiter
        tokenLimitConfigTokensPerSecond (tokenLimiterConfig tokenLimiter) `shouldBe` 9000

    it "paces no key but the one the dependency answered about" $
      forAllValid $ \(key :: Int) -> do
        limiters <- newKeyedLimiters 4500
        observeRequestsPerHour limiters key 12500
        tokenLimiter <- keyedLimiter limiters (key + 1) >>= requestLimiterTokenLimiter
        tokenLimitConfigTokensPerSecond (tokenLimiterConfig tokenLimiter) `shouldBe` 4500