@@ -19,7 +19,7 @@ import Control.Concurrent.MVar
19import Control.Monad (replicateM_, replicateM, forever)
19import Control.Monad (replicateM_, replicateM, forever)
20import Control.Exception (SomeException, throwIO)
20import Control.Exception (SomeException, throwIO)
21import qualified Control.Exception as Ex (catch)
21import qualified Control.Exception as Ex (catch)
22−import Control.Applicative ((<$>), (<*>))
22+import Control.Applicative ((<$>), (<*>), pure, (<|>))
23import qualified Network.Transport as NT (Transport, closeEndPoint)
23import qualified Network.Transport as NT (Transport, closeEndPoint)
24import Network.Socket (sClose)
24import Network.Socket (sClose)
25import Network.Transport.TCP
25import Network.Transport.TCP
@@ -762,22 +762,82 @@ testReceiveChanTimeout transport = do
763 takeMVar done
763 takeMVar done
765+-- | Test Functor, Applicative, Alternative and Monad instances for ReceiveChan
766+testReceiveChanFeatures :: NT.Transport -> Assertion
767+testReceiveChanFeatures transport = do
768+ done <- newEmptyMVar
771+ localNode <- newLocalNode transport initRemoteTable
772+ runProcess localNode $ do
773+ (spInt, rpInt) <- newChan :: Process (SendPort Int, ReceivePort Int)
774+ (spBool, rpBool) <- newChan :: Process (SendPort Bool, ReceivePort Bool)
776+ -- Test Functor instance
779+ sendChan spBool False
781+ rp1 <- mergePortsBiased [even <$> rpInt, rpBool]
783+ True <- receiveChan rp1
784+ False <- receiveChan rp1
786+ -- Test Applicative instance
791+ let rp2 = pure (+) <*> rpInt <*> rpInt
793+ 7 <- receiveChan rp2
795+ -- Test Alternative instance
798+ sendChan spBool True
800+ let rp3 = (even <$> rpInt) <|> rpBool
802+ False <- receiveChan rp3
803+ True <- receiveChan rp3
805+ -- Test Monad instance
807+ sendChan spBool True
808+ sendChan spBool False
811+ let rp4 :: ReceivePort Int
812+ rp4 = do b <- rpBool
817+ 5 <- receiveChan rp4
818+ 7 <- receiveChan rp4
820+ liftIO $ putMVar done ()
765tests :: (NT.Transport, TransportInternals) -> [Test]
824tests :: (NT.Transport, TransportInternals) -> [Test]
766tests (transport, transportInternals) = [
825tests (transport, transportInternals) = [
767 testGroup "Basic features" [
826 testGroup "Basic features" [
768− testCase "Ping" (testPing transport)
827+ testCase "Ping" (testPing transport)
769− , testCase "Math" (testMath transport)
828+ , testCase "Math" (testMath transport)
770− , testCase "Timeout" (testTimeout transport)
829+ , testCase "Timeout" (testTimeout transport)
771− , testCase "Timeout0" (testTimeout0 transport)
830+ , testCase "Timeout0" (testTimeout0 transport)
772− , testCase "SendToTerminated" (testSendToTerminated transport)
831+ , testCase "SendToTerminated" (testSendToTerminated transport)
773− , testCase "TypedChannnels" (testTypedChannels transport)
832+ , testCase "TypedChannnels" (testTypedChannels transport)
774− , testCase "MergeChannels" (testMergeChannels transport)
833+ , testCase "MergeChannels" (testMergeChannels transport)
775− , testCase "Terminate" (testTerminate transport)
834+ , testCase "Terminate" (testTerminate transport)
776− , testCase "Registry" (testRegistry transport)
835+ , testCase "Registry" (testRegistry transport)
777− , testCase "RemoteRegistry" (testRemoteRegistry transport)
836+ , testCase "RemoteRegistry" (testRemoteRegistry transport)
778− , testCase "SpawnLocal" (testSpawnLocal transport)
837+ , testCase "SpawnLocal" (testSpawnLocal transport)
779− , testCase "MatchAny" (testMatchAny transport)
838+ , testCase "MatchAny" (testMatchAny transport)
780− , testCase "ReceiveChanTimeout" (testReceiveChanTimeout transport)
839+ , testCase "ReceiveChanTimeout" (testReceiveChanTimeout transport)
840+ , testCase "ReceiveChanFeatures" (testReceiveChanFeatures transport)
782 , testGroup "Monitoring and Linking" [
842 , testGroup "Monitoring and Linking" [
783 -- Monitoring processes
843 -- Monitoring processes