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