1module Serializable (testSerializable) where
4import Control.Concurrent
5import Control.Exception as E
7import Database.PostgreSQL.Simple.Transaction
9initCounter :: Connection -> IO ()
11 0 <- execute_ conn "DROP TABLE IF EXISTS testSerializableCounter;\
12 \ CREATE TABLE testSerializableCounter (n INT)"
13 1 <- execute_ conn "INSERT INTO testSerializableCounter VALUES (0)"
16getCounter :: Connection -> IO Int
18 [Only n] <- query_ conn "SELECT n FROM testSerializableCounter"
21putCounter :: Connection -> Int -> IO ()
23 1 <- execute conn "UPDATE testSerializableCounter SET n=?" (Only n)
26testSerializable :: TestEnv -> Assertion
27testSerializable TestEnv{..} =
28 withConn $ \conn2 -> do
31 attemptCounter <- newIORef (0 :: Int)
32 readyToBother <- newEmptyMVar
33 bothered <- newEmptyMVar
34 finished <- newEmptyMVar
37 withTransactionSerializable conn2 $ do
38 modifyIORef attemptCounter (+1)
40 True <- tryPutMVar readyToBother ()
42 putCounter conn2 (n+1)
45 takeMVar readyToBother
46 withTransactionSerializable conn $ do
49 True <- tryPutMVar bothered ()
53 ac <- readIORef attemptCounter
54 assertEqual "attemptCounter" 2 ac
56 ok <- E.catch (do withTransactionSerializable conn (fail "Whoops")
58 (\(_ :: IOException) -> return True)
59 assertBool "Exceptions (besides serialization failure) should be\
60 \ propagated through withTransactionSerializable"
63 -- Make sure transaction isn't dangling
64 1 <- execute_ conn "UPDATE testSerializableCounter SET n=12345"
65 0 <- execute_ conn "ROLLBACK"
66 -- This prints "NOTICE: there is no transaction in progress"
67 [Only (12345 :: Int)] <- query_ conn "SELECT n FROM testSerializableCounter"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list