git.iko.soy/typeable/postgresql-simple
on f5deaee help sign in
42 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 -> Test
18testNotify TestEnv{..} =
19 TestCase $
20 withConn $ \conn2 -> do
21 execute_ conn "LISTEN foo"
22 execute_ conn "LISTEN bar"
24 results_mv <- newEmptyMVar
25 forkIO $ replicateM 2 (getNotification conn)
26 >>= putMVar results_mv
28 threadDelay 100000
30 execute_ conn2 "NOTIFY foo"
31 execute_ conn2 "NOTIFY bar"
33 [n1, n2] <- sortBy (compare `on` notificationChannel)
34 <$> takeMVar results_mv
36 assertEqual "n1" "bar" (notificationChannel n1)
37 assertEqual "n2" "foo" (notificationChannel n2)
39 -- Other sanity checks
40 assertEqual "Server PIDs match" (notificationPid n1) (notificationPid n2)
41 assertBool "notificationData is empty" $
42 all (B.null . notificationData) [n1, n2]