Skip to content

test: Add reproducer of 4622 (negative connection count)#4855

Open
mkleczek wants to merge 3 commits intoPostgREST:mainfrom
mkleczek:test/4622-reproducer
Open

test: Add reproducer of 4622 (negative connection count)#4855
mkleczek wants to merge 3 commits intoPostgREST:mainfrom
mkleczek:test/4622-reproducer

Conversation

@mkleczek
Copy link
Copy Markdown
Collaborator

@mkleczek mkleczek commented Apr 28, 2026

DISCLAIMER:
This commit was authored entirely by a human without the assistance of LLMs.

Stacked on top of #4836

The test can be enabled (ie. the assertion changed to a proper one) in #4672 if this PR is merged into main.

Comment on lines +20 to +53
spec :: SpecWith (SpecState, Application)
spec = describe "Tests using Toxiproxy" $ do
it "Should return 503 on temporary database server unavailability" $ do
pendingWith "TODO fix"
SpecState{specAppState, specToxiProxy} <- getState

-- make sure there are no open connections
liftIO $ AppState.flushPool specAppState

liftBaseDiscard (withDisabled specToxiProxy) $ do
void $ get "/items?id=eq.5"
`shouldRespondWith` 503

void $ get "/items?id=eq.5"
`shouldRespondWith` 200

liftBaseDiscard (withDisabled specToxiProxy) $ do
void $ get "/items?id=eq.5"
`shouldRespondWith` 503

it "Must not have negative connection count" $ do
SpecState{specAppState, specToxiProxy, specMetrics=Metric.MetricsState{poolAvailable}} <- getState

-- make sure there are no open connections
liftIO $ AppState.flushPool specAppState

liftBaseDiscard (withDisabled specToxiProxy) $ do
replicateM_ 5 $ get "/authors_only?id=eq.5"
`shouldRespondWith` 503

-- TODO https://github.com/PostgREST/postgrest/issues/4622
-- change to 0 when fixed
-- HSpec does not support xfail which should be used instead
liftIO $ getGauge poolAvailable >>= (`shouldBe` (-5::Int)) . round
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The benefit of this integration is much more clear now.

Comment thread test/lib/Toxiproxy.hs
@@ -0,0 +1,347 @@
{-
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding #4836 (comment), can't we delete the code we don't use?

postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: createProxy
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: createToxic
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: deleteProxy
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: deleteToxic
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: getProxies
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: getProxy
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: getToxic
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: getToxics
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: getVersion
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: postPopulate
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: postReset
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: updateToxic
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:331:1: withToxic
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:343:1: withProxy

From https://github.com/PostgREST/postgrest/actions/runs/25049698636/job/73374301118?pr=4855

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding #4836 (comment), can't we delete the code we don't use?

postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: createProxy
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: createToxic
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: deleteProxy
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: deleteToxic
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: getProxies
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: getProxy
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: getToxic
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: getToxics
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: getVersion
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: postPopulate
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: postReset
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:282:1: updateToxic
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:331:1: withToxic
postgrest-15-inplace-observability: test/observability/Toxiproxy.hs:343:1: withProxy

From https://github.com/PostgREST/postgrest/actions/runs/25049698636/job/73374301118?pr=4855

Yeah... I've left it for now as the code is copied as-is from https://github.com/jpittis/toxiproxy-haskell and I was not sure what to do with it.
Happy to remove unused code, the only thing I am hesitant to delete is withToxic - we do not have these kind of tests yet but I expect to have them in the future.

Copy link
Copy Markdown
Member

@wolfgangwalther wolfgangwalther left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about whether this should be an hspec or IO test, in light of #4860 (comment) and #4836 (review):

It seems to me like this test would not suffer from the "brittle log checking" problem we currently have with IO tests. Instead we'd check the return of the metrics endpoint, correct?

@mkleczek
Copy link
Copy Markdown
Collaborator Author

Thinking about whether this should be an hspec or IO test, in light of #4860 (comment) and #4836 (review):

It seems to me like this test would not suffer from the "brittle log checking" problem we currently have with IO tests. Instead we'd check the return of the metrics endpoint, correct?

True - if we had Toxiproxy infra/helpers in Python we can scrap metrics endpoint and validate its output.

@mkleczek mkleczek force-pushed the test/4622-reproducer branch from c7a8cdb to f546667 Compare April 29, 2026 14:22
mkleczek added 3 commits May 3, 2026 14:10
This commit introduces toxiproxy in withTools adding the following helpers:
* withToxiproxyServer
* withToxiproxyProxy
* withToxiproxyPgProxy
@mkleczek mkleczek force-pushed the test/4622-reproducer branch from f546667 to c02231e Compare May 3, 2026 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants