module Integration.Sharing.Connection.UseSpec (spec) whereimport Control.Concurrentimport Control.Exceptionimport Data.Eitherimport Data.IORefimport Hasql.Connection qualified as Connectionimport Hasql.Decoders qualified as Decodersimport Hasql.Encoders qualified as Encodersimport Hasql.Errors qualified as Errorsimport Hasql.Session qualified as Sessionimport Hasql.Statement qualified as Statementimport Helpers.Dsls.Execution qualified as Executionimport Helpers.Scripts qualified as Scriptsimport Helpers.Statements.SelectProvidedInt8 qualified as Statements.SelectProvidedInt8import Preludeimport Test.Hspecspec :: SpecWith Scripts.ScopeParamsspec = do describe "Transactions" do it "Do not cause \"in progress after error\"" \config -> do Scripts.onPreparableConnection config \connection -> do let sumStatement = Statement.preparable "select ($1 + $2)" ( mconcat [ fst >$< Encoders.param (Encoders.nonNullable Encoders.int8), snd >$< Encoders.param (Encoders.nonNullable Encoders.int8) ] ) (Decoders.singleRow (Decoders.column (Decoders.nonNullable Decoders.int8))) result <- Connection.use connection do Session.script "." result `shouldSatisfy` isLeft result <- Connection.use connection do Session.script "begin;" s <- Session.statement (1 :: Int64, 2 :: Int64) sumStatement Session.script "end;" return s result `shouldBe` Right (3 :: Int64) describe "Release" do it "Is idempotent on a connection that never failed" \config -> do -- 'release' must not touch a connection it has already finished: -- libpq forbids calling anything, including a second PQfinish, on a -- handle after PQfinish. Every other idempotency example gets there -- via a prior failure or exception; this is the plain path, with -- nothing having gone wrong first. Scripts.onPreparableConnection config \connection -> do _ <- Connection.use connection (Session.script "select 1") Connection.release connection Connection.release connection it "Makes `use` report the connection as gone rather than reconnecting" \config -> do -- Releasing is one of the three ways a handle goes spent (the others -- being a session that finishes the connection, and an exception). No -- other example reaches "spent by `release`" directly. Scripts.onPreparableConnection config \connection -> do Connection.release connection result <- Connection.use connection (Session.script "select 1") case result of Left (Errors.ConnectionUseError _) -> pure () _ -> expectationFailure ("Unexpected result: " <> show result) -- Releasing an already-spent connection remains a no-op. Connection.release connection describe "Concurrency" do it "handles concurrent connections properly" \config -> do Scripts.onPreparableConnection config \connection1 -> do Scripts.onPreparableConnection config \connection2 -> do let selectSleep = Statement.preparable "select pg_sleep($1)" (Encoders.param (Encoders.nonNullable Encoders.float8)) Decoders.noResult beginVar <- newEmptyMVar finishVar <- newEmptyMVar _ <- forkIO do putMVar beginVar () _ <- Connection.use connection1 (Session.statement (0.2 :: Double) selectSleep) void (tryPutMVar finishVar False) _ <- forkIO do takeMVar beginVar _ <- Connection.use connection2 (Session.statement (0.1 :: Double) selectSleep) void (tryPutMVar finishVar True) -- The second connection should finish first (True) result <- takeMVar finishVar result `shouldBe` True it "Reports the connection as gone to every other thread rather than deadlocking" \config -> Scripts.onPreparableConnection config \connection -> do -- The exception path of `use` empties the MVar before it touches the -- connection and fills it back in with `Nothing`, so a thread waiting -- on the MVar wakes up to a handle that says the connection is gone. -- What it must never do is wake up to the connection itself while the -- interrupted thread is still finishing it, or not wake up at all - -- which is what happened before commit 62ebef2, where the state went -- back into the MVar before the connection was dealt with. -- -- So this pins the two halves that survive the connection no longer -- being repaired: every result another thread sees is either the -- statement's own or the verdict that the connection is gone, and no -- thread hangs. -- Results collected by the thread using the connection alongside the -- one that interrupts it. resultsRef <- newIORef [] startBarrier <- newEmptyMVar doneBarrier <- newEmptyMVar -- Thread A: interrupts a session that is in a transaction, so the -- connection is left in a non-idle state. _ <- forkIO do takeMVar startBarrier replicateM_ 10 do _ <- try @SomeException do Connection.use connection do Session.script "BEGIN" liftIO (throwIO (userError "Intentional exception")) threadDelay 1000 putMVar doneBarrier () -- Thread B: keeps using the connection throughout. _ <- forkIO do takeMVar startBarrier replicateM_ 20 do result <- Connection.use connection (Execution.sessionByParams (Statements.SelectProvidedInt8.SelectProvidedInt8 42)) atomicModifyIORef' resultsRef (\results -> (result : results, ())) threadDelay 500 putMVar doneBarrier () putMVar startBarrier () putMVar startBarrier () completion <- timeout (5 * 1000000) do takeMVar doneBarrier takeMVar doneBarrier case completion of Nothing -> expectationFailure "Timed out waiting for the threads to complete, which means the connection deadlocked." Just () -> do results <- readIORef resultsRef let unexpected = filter ( \case Right 42 -> False Left (Errors.ConnectionUseError _) -> False _ -> True ) results unexpected `shouldBe` [] -- Once gone, gone: the handle does not come back to life. finalResult <- Connection.use connection (Execution.sessionByParams (Statements.SelectProvidedInt8.SelectProvidedInt8 99)) case finalResult of Left (Errors.ConnectionUseError _) -> pure () _ -> expectationFailure ("Unexpected final result: " <> show finalResult)