@@ -12,6 +12,10 @@ import Protolude hiding (get)
1212import Test.Hspec (SpecWith , describe , it )
1313import Test.Hspec.Wai
1414import Toxiproxy (withDisabled )
15+ import Prometheus (getGauge )
16+ import PostgREST.Metrics (MetricsState (poolAvailable ))
17+ import qualified PostgREST.Metrics as Metric
18+ import Test.Hspec.Expectations
1519
1620spec :: SpecWith (SpecState , Application )
1721spec = describe " Tests using Toxiproxy" $ do
@@ -32,3 +36,18 @@ spec = describe "Tests using Toxiproxy" $ do
3236 liftBaseDiscard (withDisabled specToxiProxy) $ do
3337 void $ get " /items?id=eq.5"
3438 `shouldRespondWith` 503
39+
40+ it " Must not have negative connection count" $ do
41+ SpecState {specAppState, specToxiProxy, specMetrics= Metric. MetricsState {poolAvailable}} <- getState
42+
43+ -- make sure there are no open connections
44+ liftIO $ AppState. flushPool specAppState
45+
46+ liftBaseDiscard (withDisabled specToxiProxy) $ do
47+ replicateM_ 5 $ get " /authors_only?id=eq.5"
48+ `shouldRespondWith` 503
49+
50+ -- TODO https://github.com/PostgREST/postgrest/issues/4622
51+ -- change to 0 when fixed
52+ -- HSpec does not support xfail which should be used instead
53+ liftIO $ getGauge poolAvailable >>= (`shouldBe` (- 5 :: Int )) . round
0 commit comments