git.iko.soy/typeable/postgresql-simple
on master help sign in
41 lines 1.2 kB history raw
1module Notify (testNotify) where
3import Common
5import Control.Applicative
6import Control.Concurrent
7import Control.Monad
8import Data.Function
9import Data.List
10import Database.PostgreSQL.Simple.Notification
12import qualified Data.ByteString as B
14-- TODO: Test with payload, but only for PostgreSQL >= 9.0
15-- (when that feature was introduced).
17testNotify :: TestEnv -> Assertion
18testNotify TestEnv{..} =
19 withConn $ \conn2 -> do
20 execute_ conn "LISTEN foo"
21 execute_ conn "LISTEN bar"
23 results_mv <- newEmptyMVar
24 forkIO $ replicateM 2 (getNotification conn)
25 >>= putMVar results_mv
27 threadDelay 100000
29 execute_ conn2 "NOTIFY foo"
30 execute_ conn2 "NOTIFY bar"
32 [n1, n2] <- sortBy (compare `on` notificationChannel)
33 <$> takeMVar results_mv
35 assertEqual "n1" "bar" (notificationChannel n1)
36 assertEqual "n2" "foo" (notificationChannel n2)
38 -- Other sanity checks
39 assertEqual "Server PIDs match" (notificationPid n1) (notificationPid n2)
40 assertBool "notificationData is empty" $
41 all (B.null . notificationData) [n1, n2]