@@ -0,0 +1,149 @@
1+{-# LANGUAGE TemplateHaskell, DeriveDataTypeable #-}
2+{-# OPTIONS_GHC -Wall #-}
3+import Control.Distributed.Process
4+import Control.Distributed.Process.Closure
14+import Test.Framework (defaultMain, testGroup)
15+import Test.Framework.Providers.HUnit
20+-- 1. 2 matchChans, receive on each one
21+-- 2. matchChan/matchIf, receive on each one
22+-- 3. matchIf/matchChan, receive on each one
23+-- 4. matchIf/matchChan/matchIf, receive on each one
25+recTest1 :: ReceivePort ()
27+ -> ReceivePort String -> ReceivePort String
29+recTest1 wait sync r1 r2 = do
33+ [ matchChan r1 $ \s -> return ("received1 " ++ s)
34+ , matchChan r2 $ \s -> return ("received2 " ++ s)
38+recTest2 :: ReceivePort ()
40+ -> ReceivePort String -> ReceivePort String
42+recTest2 wait sync r1 r2 = do
46+ [ matchChan r1 $ \s -> return ("received1 " ++ s)
47+ , matchIf (== "foo") $ \s -> return ("received2 " ++ s)
51+recTest3 :: ReceivePort ()
53+ -> ReceivePort String -> ReceivePort String
55+recTest3 wait sync r1 r2 = do
59+ [ matchIf (== "foo") $ \s -> return ("received1 " ++ s)
60+ , matchChan r1 $ \s -> return ("received2 " ++ s)
64+recTest4 :: ReceivePort ()
66+ -> ReceivePort String -> ReceivePort String
68+recTest4 wait sync r1 r2 = do
72+ [ matchIf (== "foo") $ \s -> return ("received1 " ++ s)
73+ , matchChan r1 $ \s -> return ("received2 " ++ s)
74+ , matchIf (== "bar") $ \s -> return ("received3 " ++ s)
79+master :: [NodeId] -> Process ()
82+ (waits,waitr) <- newChan
83+ (syncs,syncr) <- newChan
86+ r <- receiveChan syncr
87+ liftIO $ print (r,expect, r == expect)
88+ liftIO $ r @?= expect
90+ liftIO $ putStrLn "---- Test 1 ----"
93+ p <- spawnLocal (recTest1 waitr syncs r1 r2)
95+ sendChan s1 "a" >> go "received1 a"
96+ sendChan s2 "b" >> go "received2 b"
97+ sendChan s1 "a" >> sendChan s2 "b" >> go "received1 a"
102+ liftIO $ putStrLn "\n---- Test 2 ----"
105+ p <- spawnLocal (recTest2 waitr syncs r1 r2)
107+ sendChan s1 "a" >> go "received1 a"
108+ send p "foo" >> go "received2 foo"
109+ sendChan s1 "a" >> send p "foo" >> go "received1 a"
110+ sendChan s1 "a" >> send p "bar" >> go "received1 a"
115+ liftIO $ putStrLn "\n---- Test 3 ----"
118+ p <- spawnLocal (recTest3 waitr syncs r1 r2)
120+ sendChan s1 "a" >> go "received2 a"
121+ send p "foo" >> go "received1 foo"
122+ sendChan s1 "a" >> send p "foo" >> go "received1 foo"
123+ sendChan s1 "a" >> send p "bar" >> go "received2 a"
128+ liftIO $ putStrLn "\n---- Test 4 ----"
131+ p <- spawnLocal (recTest4 waitr syncs r1 r2)
133+ sendChan s1 "a" >> go "received2 a"
134+ send p "foo" >> go "received1 foo"
135+ send p "bar" >> go "received3 bar"
136+ sendChan s1 "a" >> send p "foo" >> go "received1 foo"
137+ send p "bar" >> go "received2 a"
138+ send p "foo" >> go "received1 foo" >> go "received3 bar"
145+-- NB. test doesn't work, because failure exceptions don't get
149+main = defaultMain [ testCase "all" $ distribMain master id ]