Tests.hs 7.02 KB
Newer Older
1 2
module Main where

3 4
import           Control.Concurrent.MVar               (MVar, putMVar,
                                                        newEmptyMVar, takeMVar)
5 6 7 8 9
import           Control.Distributed.Process
import           Control.Distributed.Process.Node
import qualified Network.Transport as NT               (Transport)
import           Network.Transport.TCP
import           Prelude                        hiding (seq)
10 11
import           Test.Framework                        (Test, testGroup,
                                                        defaultMain)
12
import           Test.Framework.Providers.HUnit        (testCase)
13
import           Test.HUnit                            (Assertion)
14 15
import           Test.HUnit.Base                       (assertBool)

16 17
import           Bench                                 (seq, par, par_seq,
                                                        dist, dist_seq)
18 19 20 21 22 23 24
import           MasterWorker                          (__remoteTable)
import           Utils

-- Sequential Tests

testSeqShort :: TestResult String -> Process ()
testSeqShort result = do
25 26
    x <- seq gg13 11
    stash result x
27 28 29

testSeqIntermediate :: TestResult String -> Process ()
testSeqIntermediate result = do
30 31
    x <- seq gg124 157
    stash result x
32 33 34

testSeqLong :: TestResult String -> Process ()
testSeqLong result = do
35 36
    x <- seq gg1245 157
    stash result x
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
-- Parallel Tests

testParShort :: TestResult String -> Process ()
testParShort result = do
    x <- par gg13 11 2
    stash result x

testParIntermediate :: TestResult String -> Process ()
testParIntermediate result = do
    x <- par gg124 157 2
    stash result x

testParLong :: TestResult String -> Process ()
testParLong result = do
    x <- par gg1245 157 2
    stash result x

testParSeqShort :: TestResult String -> Process ()
testParSeqShort result = do
    x <- par_seq gg13 11 2
    stash result x

testParSeqIntermediate :: TestResult String -> Process ()
testParSeqIntermediate result = do
    x <- par_seq gg124 157 2
    stash result x

testParSeqLong :: TestResult String -> Process ()
testParSeqLong result = do
    x <- par_seq gg1245 157 2
    stash result x

-- Distributed Tests

testDistShort :: [NodeId] -> TestResult String -> Process ()
testDistShort nodes result = do
    x <- dist gg13 11 2 nodes
    stash result x

testDistIntermediate :: [NodeId] -> TestResult String -> Process ()
testDistIntermediate nodes result = do
    x <- dist gg124 157 2 nodes
    stash result x

testDistLong :: [NodeId] -> TestResult String -> Process ()
testDistLong nodes result = do
    x <- dist gg1245 157 2 nodes
    stash result x

testDistSeqShort :: [NodeId] -> TestResult String -> Process ()
testDistSeqShort nodes result = do
    x <- dist_seq gg13 11 2 nodes
    stash result x

testDistSeqIntermediate :: [NodeId] -> TestResult String -> Process ()
testDistSeqIntermediate nodes result = do
    x <- dist_seq gg124 157 2 nodes
    stash result x

testDistSeqLong :: [NodeId] -> TestResult String -> Process ()
testDistSeqLong nodes result = do
    x <- dist_seq gg1245 157 2 nodes
    stash result x

102 103
-- Batch the tests

104 105
tests :: [LocalNode] -> [Test]
tests [] = []
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
tests (localNode : localNodes) = [
      testGroup "Sequential Tests" [
            testCase "testSeqShort"
              (delayedAssertion "short" localNode "{size,10}" testSeqShort)
          , testCase "testSeqIntermediate"
              (delayedAssertion "intermediate" localNode "{size,133}" testSeqIntermediate)
          , testCase "testSeqLong"
              (delayedAssertion "long" localNode "{size,134}" testSeqLong)
        ]
    , testGroup "Parallel Tests" [
            testCase "testParSeqShort"
              (delayedAssertion "short" localNode "{size,10}" testParSeqShort)
          , testCase "testParSeqIntermediate"
              (delayedAssertion "intermediate" localNode "{size,133}" testParSeqIntermediate)
          , testCase "testParSeqLong"
              (delayedAssertion "long" localNode "{size,134}" testParSeqLong)
          , testCase "testParShort"
              (delayedAssertion "short" localNode "{size,10}" testParShort)
          , testCase "testParIntermediate"
              (delayedAssertion "intermediate" localNode "{size,133}" testParIntermediate)
          , testCase "testParLong"
              (delayedAssertion "long" localNode "{size,134}" testParLong)
        ]
    , testGroup "Distributed Tests" [
            testCase "testDistSeqShort"
              (delayedAssertion "short" localNode "{size,10}" $
                testDistSeqShort (map localNodeId localNodes))
          , testCase "testDistSeqIntermediate"
              (delayedAssertion "intermediate" localNode "{size,133}" $
                testDistSeqIntermediate (map localNodeId localNodes))
          , testCase "testDistSeqLong"
              (delayedAssertion "long" localNode "{size,134}" $
                testDistSeqLong (map localNodeId localNodes))
          , testCase "testDistShort"
              (delayedAssertion "short" localNode "{size,10}" $
                testDistShort (map localNodeId localNodes))
          , testCase "testDistIntermediate"
              (delayedAssertion "intermediate" localNode "{size,133}" $
                testDistIntermediate (map localNodeId localNodes))
          , testCase "testDistLong"
              (delayedAssertion "long" localNode "{size,134}" $
                testDistLong (map localNodeId localNodes))
       ]
149 150 151 152 153 154
  ]

-- Run the tests

orbitTests :: NT.Transport -> IO [Test]
orbitTests transport = do
155 156 157 158
    localNode  <- newLocalNode transport rtable
    localNode2 <- newLocalNode transport rtable
    localNode3 <- newLocalNode transport rtable
    let testData = tests [localNode, localNode2, localNode3]
159
    return testData
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  where rtable :: RemoteTable
        rtable = MasterWorker.__remoteTable initRemoteTable

main :: IO ()
main = testMain $ orbitTests

-- Auxiliary functions
-------------------------------------------------------------------

-- | A mutable cell containing a test result.
type TestResult a = MVar a

-- | Stashes a value in our 'TestResult' using @putMVar@
stash :: TestResult a -> a -> Process ()
stash mvar x = liftIO $ putMVar mvar x

-- | Run the supplied @testProc@ using an @MVar@ to collect and assert
-- against its result. Uses the supplied @note@ if the assertion fails.
delayedAssertion :: (Eq a) => String -> LocalNode -> a ->
                    (TestResult a -> Process ()) -> Assertion
delayedAssertion note localNode expected testProc = do
181 182 183
    result <- newEmptyMVar
    _ <- forkProcess localNode $ testProc result
    assertComplete note result expected
184 185 186 187

-- | Takes the value of @mv@ (using @takeMVar@) and asserts that it matches @a@
assertComplete :: (Eq a) => String -> MVar a -> a -> IO ()
assertComplete msg mv a = do
188 189
    b <- takeMVar mv
    assertBool msg (a == b)
190 191 192 193

-- | Given a @builder@ function, make and run a test suite on a single transport
testMain :: (NT.Transport -> IO [Test]) -> IO ()
testMain builder = do
194 195 196 197
    Right (transport, _) <-
      createTransportExposeInternals "127.0.0.1" "10501" defaultTCPParameters
    testData <- builder transport
    defaultMain testData