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 -> Test
27testSerializable TestEnv{..} =
29 withConn $ \conn2 -> do
32 attemptCounter <- newIORef (0 :: Int)
33 readyToBother <- newEmptyMVar
34 bothered <- newEmptyMVar
35 finished <- newEmptyMVar
38 withTransactionSerializable conn2 $ do
39 modifyIORef attemptCounter (+1)
41 True <- tryPutMVar readyToBother ()
43 putCounter conn2 (n+1)
46 takeMVar readyToBother
47 withTransactionSerializable conn $ do
50 True <- tryPutMVar bothered ()
54 ac <- readIORef attemptCounter
55 assertEqual "attemptCounter" 2 ac
57 ok <- E.catch (do withTransactionSerializable conn (fail "Whoops")
59 (\(_ :: IOException) -> return True)
60 assertBool "Exceptions (besides serialization failure) should be\
61 \ propagated through withTransactionSerializable"
64 -- Make sure transaction isn't dangling
65 1 <- execute_ conn "UPDATE testSerializableCounter SET n=12345"
66 0 <- execute_ conn "ROLLBACK"
67 -- This prints "NOTICE: there is no transaction in progress"
68 [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