-- | Coverage for 'Pqi.loClose': closing an open large object, after which
-- reads through the stale descriptor fail.
module Pqi.Conformance.Operation.LoClose
  ( spec,
  )
where

import qualified Pqi
import Pqi.Conformance.Harness
import Pqi.Conformance.Prelude
import Pqi.Conformance.Scenario (inTransaction)
import System.IO (IOMode (..))
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
"loClose" do
    String
-> (ByteString -> IO ()) -> SpecWith (Arg (ByteString -> IO ()))
forall a.
(HasCallStack, Example a) =>
String -> a -> SpecWith (Arg a)
it String
"closes the descriptor and invalidates further reads" \ByteString
conninfo ->
      Adapter
-> ByteString
-> (Connection -> IO (Maybe (Maybe (Maybe ()), Maybe ByteString)))
-> IO ()
forall a.
(Eq a, Show a, HasCallStack) =>
Adapter -> ByteString -> (Connection -> IO a) -> IO ()
differential Adapter
adapter ByteString
conninfo \Connection
connection ->
        Connection
-> IO (Maybe (Maybe (Maybe ()), Maybe ByteString))
-> IO (Maybe (Maybe (Maybe ()), Maybe ByteString))
forall a. Connection -> IO a -> IO a
inTransaction Connection
connection do
          oid <- Connection -> IO (Maybe Word32)
Pqi.loCreat Connection
connection
          outcome <- for oid \Word32
o -> do
            fd <- Connection -> Word32 -> IOMode -> IO (Maybe Int32)
Pqi.loOpen Connection
connection Word32
o IOMode
ReadMode
            closed <- for fd (Pqi.loClose connection)
            readAfterClose <- join <$> for fd \Int32
f -> Connection -> Int32 -> Int -> IO (Maybe ByteString)
Pqi.loRead Connection
connection Int32
f Int
10
            pure (closed, readAfterClose)
          traverse_ (Pqi.loUnlink connection) oid
          pure outcome