git.iko.soy/typeable/postgresql-simple
on master help sign in
test: Add Notify
commit385ce1a4a8f4ad7723c6ce4fc323983e091dad42 authorJoey Adams <joeyadams3.14159@gmail.com> dateThu 17 May 2012, 23:27 (14 years ago) parent6e72a71
3 files changed+60−4
postgresql-simple.cabal+1 test/Main.hs+16−4 test/Notify.hs+43
postgresql-simple.cabal+1
@@ -71,6 +71,7 @@ test-suite test
7171 main-is: Main.hs
7272 other-modules:
7373 Bytea
74+ Notify
7475
7576 ghc-options: -Wall -fno-warn-name-shadowing -fno-warn-unused-do-bind
7677
test/Main.hs+16−4
@@ -9,15 +9,27 @@ import System.IO
99import Test.HUnit
1010
1111import Bytea
12+import Notify
13+
14+-- | Action for connecting to the database that will be used for testing.
15+--
16+-- Note that some tests, such as Notify, use multiple connections, and assume
17+-- that 'testConnect' connects to the same database every time it is called.
18+testConnect :: IO Connection
19+testConnect = connectPostgreSQL ""
20+
21+withConn :: (Connection -> IO a) -> IO a
22+withConn = bracket testConnect close
1223
1324tests :: Connection -> [Test]
1425tests conn =
15− [ TestLabel "Bytea" $ testBytea conn
26+ [ TestLabel "Bytea" $ testBytea conn
27+ , TestLabel "Notify" $ testNotify conn withConn
1628 ]
1729
1830main :: IO ()
1931main = do
2032 mapM_ (`hSetBuffering` LineBuffering) [stdout, stderr]
21− bracket (connectPostgreSQL "") close $ \conn -> do
22− Counts{cases, tried, errors, failures} <- runTestTT $ TestList $ tests conn
23− when (cases /= tried || errors /= 0 || failures /= 0) $ exitFailure
33+ Counts{cases, tried, errors, failures} <-
34+ withConn $ runTestTT . TestList . tests
35+ when (cases /= tried || errors /= 0 || failures /= 0) $ exitFailure
test/Notify.hsnew file+43
@@ -0,0 +1,43 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Notify (testNotify) where
3+
4+import Control.Applicative
5+import Control.Concurrent
6+import Control.Monad
7+import Data.Function
8+import Data.List
9+import Database.PostgreSQL.Simple
10+import Database.PostgreSQL.Simple.Notification
11+import Test.HUnit
12+
13+import qualified Data.ByteString as B
14+
15+-- TODO: Test with payload, but only for PostgreSQL >= 9.0
16+-- (when that feature was introduced).
17+
18+testNotify :: Connection -> ((Connection -> IO ()) -> IO ()) -> Test
19+testNotify conn withConn =
20+ TestCase $
21+ withConn $ \conn2 -> do
22+ execute_ conn "LISTEN foo"
23+ execute_ conn "LISTEN bar"
24+
25+ results_mv <- newEmptyMVar
26+ forkIO $ replicateM 2 (getNotification conn)
27+ >>= putMVar results_mv
28+
29+ threadDelay 100000
30+
31+ execute_ conn2 "NOTIFY foo"
32+ execute_ conn2 "NOTIFY bar"
33+
34+ [n1, n2] <- sortBy (compare `on` notificationChannel)
35+ <$> takeMVar results_mv
36+
37+ assertEqual "n1" "bar" (notificationChannel n1)
38+ assertEqual "n2" "foo" (notificationChannel n2)
39+
40+ -- Other sanity checks
41+ assertEqual "Server PIDs match" (notificationPid n1) (notificationPid n2)
42+ assertBool "notificationData is empty" $
43+ all (B.null . notificationData) [n1, n2]