-- | Reachability and recovery coverage for-- 'Hasql.Pool.ConnectionDiscards'.---- The verdicts themselves are pure functions of a SQLSTATE and are pinned by-- the @unit-tests@ suite, which needs no server. What only a live server can-- establish is the two things asserted here:---- * that the code in question actually reaches the predicate as a-- 'Errors.SessionUseError', rather than arriving as a-- 'Errors.ConnectionUseError' the pool discards without consulting the-- module at all, and---- * that the discard restores service - the residue really was-- connection-local, and a fresh connection runs the same statement-- successfully.---- Every test therefore runs against a pool of size 1, so "the next use" is-- guaranteed to be either the same connection or its replacement.module Specs.ConnectionDiscardsSpec whereimport Data.IORef qualified as IORefimport Data.Text qualified as Textimport Hasql.Decoders qualified as Decodersimport Hasql.Encoders qualified as Encodersimport Hasql.Errors qualified as Errorsimport Hasql.Poolimport Hasql.Pool.Observation qualified as Observationimport Hasql.Session qualified as Sessionimport Hasql.Statement qualified as Statementimport Helpers.Scripts qualified as Scriptsimport Helpers.Sessions qualified as Sessionsimport Preludeimport Test.Hspecspec :: SpecWith Scripts.ScopeParamsspec = do -- XX000. The server notices the retired type OID while replanning the -- prepared statement and raises @cache lookup failed for type NNNN@, which -- repeats on every subsequent execution over that connection. it "Discards a connection whose prepared statement references a recreated type" \scopeParams -> do typeName <- Text.replace "-" "_" <$> Scripts.generateName "cached_type_" onSingleConnectionPool scopeParams \pool -> do use pool (Session.script (createTypeSql typeName)) `shouldReturn` Right () use pool (roundtripEnum typeName "ok") `shouldReturn` Right "ok" use pool (Session.script (recreateTypeSql typeName)) `shouldReturn` Right () shouldFailWithSessionError (use pool (roundtripEnum typeName "ok")) use pool (roundtripEnum typeName "ok") `shouldReturn` Right "ok" -- 0A000, @cached plan must not change result type@. The prepared plan for -- @select *@ was built against one column; adding a second one invalidates -- it permanently for this connection. it "Discards a connection whose prepared plan outlived the shape of its result" \scopeParams -> do tableName <- Text.replace "-" "_" <$> Scripts.generateName "cached_plan_" onSingleConnectionPool scopeParams \pool -> do use pool (Session.script ("create table " <> quoteIdentifier tableName <> " (a int8)")) `shouldReturn` Right () use pool (selectStar tableName) `shouldReturn` Right () use pool (Session.script ("alter table " <> quoteIdentifier tableName <> " add column b int8")) `shouldReturn` Right () shouldFailWithSessionError (use pool (selectStar tableName)) -- The replacement connection replans against the new shape, so the -- one-column decoder now genuinely mismatches. That it fails -- \*differently* is the point: the plan-cache residue is gone. use pool (selectTwoColumns tableName) `shouldReturn` Right () -- 26000, @prepared statement "..." does not exist@. Hasql's statement -- cache still lists the name; the server no longer does, and nothing -- evicts the stale entry. it "Discards a connection whose statement cache outlived the server's prepared statements" \scopeParams -> onSingleConnectionPool scopeParams \pool -> do use pool Sessions.selectOne `shouldReturn` Right 1 use pool (Session.script "deallocate all") `shouldReturn` Right () shouldFailWithSessionError (use pool Sessions.selectOne) use pool Sessions.selectOne `shouldReturn` Right 1 -- 25P02. Note the sequencing: the statement that aborts the transaction -- fails with a syntax error, which is correctly *retained* - the residue -- only becomes visible on the following use, which is the one that -- triggers the discard. it "Discards a connection left parked in an aborted transaction" \scopeParams -> onSingleConnectionPool scopeParams \pool -> do shouldFailWithSessionError (use pool (Session.script "begin" >> Sessions.badQuery)) shouldFailWithSessionError (use pool Sessions.selectOne) use pool Sessions.selectOne `shouldReturn` Right 1 -- 57P01. Terminating the backend underneath a session is announced with a -- genuine FATAL error result, so hasql reports it as a -- 'Errors.ConnectionUseError' carrying an 'Errors.ServerConnectionError' -- with the SQLSTATE intact - never as a server error reaching -- 'ConnectionDiscards.serverError'. The @57P@ rule is therefore dead for -- this code, and is kept only because the rest of the class has not been -- shown to behave the same way. The pool must discard the connection and -- establish a replacement, which the size-1 pool makes observable. Its -- termination observation must preserve the structured server error too. it "Recovers from a backend terminated underneath it and observes its error" \scopeParams -> do observations <- IORef.newIORef [] let observe observation = IORef.atomicModifyIORef' observations \recorded -> (observation : recorded, ()) Scripts.onAutotaggedPoolWithObservationHandler 1 10 1_800 1_800 observe scopeParams \_ pool -> do use pool Sessions.selectOne `shouldReturn` Right 1 terminated <- use pool terminateSelf terminated `shouldSatisfy` \case Left (SessionUsageError (Errors.ConnectionUseError connectionError)) -> Errors.toSqlState connectionError == Just "57P01" _ -> False use pool Sessions.selectOne `shouldReturn` Right 1 recorded <- IORef.readIORef observations recorded `shouldSatisfy` any hasStructuredTerminationErrorhasStructuredTerminationError :: Observation.Observation -> BoolhasStructuredTerminationError = \case Observation.ConnectionObservation _ (Observation.TerminatedConnectionStatus (Observation.NetworkErrorConnectionTerminationReason (Errors.ServerConnectionError serverError))) -> Errors.toSqlState serverError == Just "57P01" _ -> FalseonSingleConnectionPool :: Scripts.ScopeParams -> (Pool -> IO ()) -> IO ()onSingleConnectionPool scopeParams cont = Scripts.onAutotaggedPool 1 10 1_800 1_800 scopeParams (const cont)shouldFailWithSessionError :: (Show a) => IO (Either UsageError a) -> ExpectationshouldFailWithSessionError action = do res <- action shouldSatisfy res \case Left (SessionUsageError _) -> True _ -> FalsequoteIdentifier :: Text -> TextquoteIdentifier identifier = "\"" <> Text.replace "\"" "\"\"" identifier <> "\""createTypeSql :: Text -> TextcreateTypeSql typeName = "create type " <> quoteIdentifier typeName <> " as enum ('sad', 'ok', 'happy')"recreateTypeSql :: Text -> TextrecreateTypeSql typeName = "drop type " <> quoteIdentifier typeName <> "; create type " <> quoteIdentifier typeName <> " as enum ('sad', 'ok', 'happy')"roundtripEnum :: Text -> Text -> Session.Session TextroundtripEnum typeName value = Session.statement value statement where statement = Statement.preparable ("select $1 :: " <> quoteIdentifier typeName) (Encoders.param (Encoders.nonNullable (Encoders.enum Nothing typeName id))) (Decoders.singleRow (Decoders.column (Decoders.nonNullable (Decoders.enum Nothing typeName Just))))selectStar :: Text -> Session.Session ()selectStar tableName = Session.statement () statement where statement = Statement.preparable ("select * from " <> quoteIdentifier tableName) Encoders.noParams (Decoders.rowList (Decoders.column (Decoders.nonNullable Decoders.int8)) $> ())selectTwoColumns :: Text -> Session.Session ()selectTwoColumns tableName = Session.statement () statement where statement = Statement.preparable ("select * from " <> quoteIdentifier tableName) Encoders.noParams ( Decoders.rowList ( (,) <$> Decoders.column (Decoders.nonNullable Decoders.int8) <*> Decoders.column (Decoders.nonNullable Decoders.int8) ) $> () )terminateSelf :: Session.Session ()terminateSelf = Session.script "select pg_terminate_backend(pg_backend_pid())"