2024-04-01 18:46:33 +00:00
|
|
|
module Test.Effect.Postgres.Pool where
|
|
|
|
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
import Data.Traversable (traverse)
|
|
|
|
import Effect.Aff (finally, forkAff, joinFiber)
|
|
|
|
import Effect.Aff.Postgres.Client as Client
|
|
|
|
import Effect.Aff.Postgres.Pool as Pool
|
|
|
|
import Effect.Class (liftEffect)
|
2024-06-25 18:53:09 +00:00
|
|
|
import Effect.Postgres.Error.Except as X
|
2024-04-01 20:20:59 +00:00
|
|
|
import Test.Common (config, onceAff, withPool)
|
2024-04-01 18:46:33 +00:00
|
|
|
import Test.Spec (Spec, around, describe, it)
|
|
|
|
import Test.Spec.Assertions (expectError, shouldEqual)
|
|
|
|
|
|
|
|
foreign import refEq :: forall a. a -> a -> Boolean
|
|
|
|
|
|
|
|
spec :: Spec Unit
|
|
|
|
spec = describe "Pool" do
|
|
|
|
it "make" do
|
|
|
|
cfg <- liftEffect config
|
|
|
|
void $ liftEffect $ Pool.make cfg
|
|
|
|
around withPool do
|
|
|
|
it "idleCount, totalCount" \p -> do
|
2024-06-25 18:53:09 +00:00
|
|
|
a <- X.run $ Pool.connect p
|
|
|
|
b <- X.run $ Pool.connect p
|
|
|
|
c <- X.run $ Pool.connect p
|
|
|
|
liftEffect $ X.run $ Pool.release p a
|
|
|
|
liftEffect $ X.run $ Pool.release p b
|
|
|
|
finally (liftEffect $ X.run $ Pool.release p c) do
|
2024-04-01 18:46:33 +00:00
|
|
|
Pool.clientIdleCount p `shouldEqual` 2
|
|
|
|
Pool.clientCount p `shouldEqual` 3
|
|
|
|
Pool.clientIdleCount p `shouldEqual` 3
|
|
|
|
Pool.clientCount p `shouldEqual` 3
|
|
|
|
it "waitingCount" \p -> do
|
2024-06-25 18:53:09 +00:00
|
|
|
a <- X.run $ Pool.connect p
|
|
|
|
b <- X.run $ Pool.connect p
|
|
|
|
c <- X.run $ Pool.connect p
|
|
|
|
dFiber <- forkAff $ X.run $ Pool.connect p
|
2024-04-01 18:46:33 +00:00
|
|
|
let
|
|
|
|
rel =
|
|
|
|
do
|
2024-06-25 18:53:09 +00:00
|
|
|
void $ liftEffect $ X.run $ traverse (Pool.release p) [ a, b, c ]
|
2024-04-01 18:46:33 +00:00
|
|
|
d <- joinFiber dFiber
|
2024-06-25 18:53:09 +00:00
|
|
|
liftEffect $ X.run $ Pool.release p d
|
2024-04-01 18:46:33 +00:00
|
|
|
finally rel $ Pool.clientWaitingCount p `shouldEqual` 1
|
|
|
|
describe "events" do
|
|
|
|
it "connect" \p -> do
|
|
|
|
expect <- forkAff $ void $ onceAff Pool.connectE p
|
2024-06-25 18:53:09 +00:00
|
|
|
c <- X.run $ Pool.connect p
|
|
|
|
finally (liftEffect $ X.run $ Pool.release p c) $ joinFiber expect
|
2024-04-01 18:46:33 +00:00
|
|
|
it "acquire" \p -> do
|
2024-06-25 18:53:09 +00:00
|
|
|
c <- X.run$ Pool.connect p
|
|
|
|
liftEffect $ X.run$ Pool.release p c
|
2024-04-01 18:46:33 +00:00
|
|
|
expect <- forkAff do
|
|
|
|
c'' <- onceAff Pool.acquireE p
|
|
|
|
refEq c c'' `shouldEqual` true
|
2024-06-25 18:53:09 +00:00
|
|
|
c' <- X.run $ Pool.connect p
|
|
|
|
finally (liftEffect $ X.run$ Pool.release p c') $ joinFiber expect
|
2024-04-01 18:46:33 +00:00
|
|
|
it "release" \p -> do
|
2024-06-25 18:53:09 +00:00
|
|
|
c <- X.run $ Pool.connect p
|
2024-04-01 18:46:33 +00:00
|
|
|
expect <- forkAff do
|
|
|
|
c' <- onceAff Pool.releaseE p
|
|
|
|
refEq c c' `shouldEqual` true
|
2024-06-25 18:53:09 +00:00
|
|
|
liftEffect $ X.run $ Pool.release p c
|
2024-04-01 18:46:33 +00:00
|
|
|
joinFiber expect
|
|
|
|
it "remove" \p -> do
|
2024-06-25 18:53:09 +00:00
|
|
|
c <- X.run $ Pool.connect p
|
2024-04-01 18:46:33 +00:00
|
|
|
expect <- forkAff do
|
|
|
|
c' <- onceAff Pool.removeE p
|
|
|
|
refEq c c' `shouldEqual` true
|
2024-06-25 18:53:09 +00:00
|
|
|
liftEffect $ X.run $ Pool.destroy p c
|
2024-04-01 18:46:33 +00:00
|
|
|
joinFiber expect
|
|
|
|
it "connect" \p -> do
|
2024-06-25 18:53:09 +00:00
|
|
|
c <- X.run $ Pool.connect p
|
|
|
|
let rel = liftEffect $ X.run $ Pool.release p c
|
|
|
|
finally rel $ shouldEqual 1 =<< X.run (Client.query "select 1" c)
|
2024-04-01 18:46:33 +00:00
|
|
|
describe "destroy" do
|
|
|
|
it "throws on query after destroy" \p -> do
|
2024-06-25 18:53:09 +00:00
|
|
|
c <- X.run $ Pool.connect p
|
|
|
|
liftEffect $ X.run $ Pool.destroy p c
|
|
|
|
expectError $ X.run $ Client.exec "select 1" c
|
2024-04-01 18:46:33 +00:00
|
|
|
it "different client yielded after destroy" \p -> do
|
2024-06-25 18:53:09 +00:00
|
|
|
a <- X.run $ Pool.connect p
|
|
|
|
liftEffect $ X.run $ Pool.destroy p a
|
|
|
|
b <- X.run $ Pool.connect p
|
|
|
|
liftEffect $ X.run $ Pool.destroy p b
|
2024-04-01 18:46:33 +00:00
|
|
|
refEq a b `shouldEqual` false
|
|
|
|
describe "release" do
|
|
|
|
it "allows reuse" \p -> do
|
2024-06-25 18:53:09 +00:00
|
|
|
a <- X.run $ Pool.connect p
|
|
|
|
liftEffect $ X.run $ Pool.release p a
|
|
|
|
b <- X.run $ Pool.connect p
|
|
|
|
liftEffect $ X.run $ Pool.release p b
|
2024-04-01 18:46:33 +00:00
|
|
|
refEq a b `shouldEqual` true
|
|
|
|
it "throws when invoked twice" \p -> do
|
2024-06-25 18:53:09 +00:00
|
|
|
c <- X.run $ Pool.connect p
|
|
|
|
liftEffect $ X.run $ Pool.release p c
|
|
|
|
expectError $ liftEffect $ X.run $ Pool.release p c
|