-- | Coverage for 'Pqi.isBusy': reporting whether a 'Pqi.getResult'
-- would block, settling to not-busy once the result has arrived.
module Pqi.Conformance.Operation.IsBusy
  ( spec,
  )
where

import qualified Pqi
import Pqi.Conformance.Harness
import Pqi.Conformance.Prelude
import Pqi.Conformance.Scenario (drainResults)
import Test.Hspec

spec :: Pqi.Adapter -> SpecWith ByteString
spec :: Adapter -> SpecWith ByteString
spec Adapter
adapter =
  String -> SpecWith ByteString -> SpecWith ByteString
forall a. HasCallStack => String -> SpecWith a -> SpecWith a
describe String
"isBusy" do
    String
-> (ByteString -> IO ()) -> SpecWith (Arg (ByteString -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"settles to not-busy after the result arrives" \ByteString
conninfo ->
      Adapter -> ByteString -> (Connection -> IO Bool) -> IO ()
forall a.
(Eq a, Show a, HasCallStack) =>
Adapter -> ByteString -> (Connection -> IO a) -> IO ()
differential Adapter
adapter ByteString
conninfo \Connection
connection -> do
        _ <- Connection -> ByteString -> IO Bool
Pqi.sendQuery Connection
connection ByteString
"select 42"
        let settle (Int
0 :: Int) = Connection -> IO Bool
Pqi.isBusy Connection
connection
            settle Int
n = do
              _ <- Connection -> IO Bool
Pqi.consumeInput Connection
connection
              busy <- Pqi.isBusy connection
              if busy then threadDelay 1000 >> settle (n - 1) else pure busy
        stillBusy <- settle 10000
        _ <- drainResults connection
        pure stillBusy