-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tests.hs
196 lines (165 loc) · 7.18 KB
/
Tests.hs
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
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
102
103
104
105
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
module Main where
import Control.Concurrent.MVar (MVar, putMVar,
newEmptyMVar, takeMVar)
import Control.Distributed.Process
import Control.Distributed.Process.Node
import qualified Network.Transport as NT (Transport)
import Network.Transport.TCP
import Prelude hiding (seq)
import Test.Framework (Test, testGroup,
defaultMain)
import Test.Framework.Providers.HUnit (testCase)
import Test.HUnit (Assertion)
import Test.HUnit.Base (assertBool)
import Bench (seq, par, dist, getAnswer)
import MasterWorker (__remoteTable)
import Utils
-- Sequential Tests
testSeqShort :: TestResult String -> Process ()
testSeqShort result = do
x <- seq gg13 11
stash result (getAnswer x)
testSeqIntermediate :: TestResult String -> Process ()
testSeqIntermediate result = do
x <- seq gg124 157
stash result (getAnswer x)
testSeqLong :: TestResult String -> Process ()
testSeqLong result = do
x <- seq gg1245 157
stash result (getAnswer x)
-- Parallel Tests
testParShort :: TestResult String -> Process ()
testParShort result = do
x <- par True gg13 11 2
stash result (getAnswer x)
testParIntermediate :: TestResult String -> Process ()
testParIntermediate result = do
x <- par True gg124 157 2
stash result (getAnswer x)
testParLong :: TestResult String -> Process ()
testParLong result = do
x <- par True gg1245 157 2
stash result (getAnswer x)
testParSeqShort :: TestResult String -> Process ()
testParSeqShort result = do
x <- par False gg13 11 2
stash result (getAnswer x)
testParSeqIntermediate :: TestResult String -> Process ()
testParSeqIntermediate result = do
x <- par False gg124 157 2
stash result (getAnswer x)
testParSeqLong :: TestResult String -> Process ()
testParSeqLong result = do
x <- par False gg1245 157 2
stash result (getAnswer x)
-- Distributed Tests
testDistShort :: [NodeId] -> TestResult String -> Process ()
testDistShort nodes result = do
x <- dist True gg13 11 2 nodes
stash result (getAnswer x)
testDistIntermediate :: [NodeId] -> TestResult String -> Process ()
testDistIntermediate nodes result = do
x <- dist True gg124 157 2 nodes
stash result (getAnswer x)
testDistLong :: [NodeId] -> TestResult String -> Process ()
testDistLong nodes result = do
x <- dist True gg1245 157 2 nodes
stash result (getAnswer x)
testDistSeqShort :: [NodeId] -> TestResult String -> Process ()
testDistSeqShort nodes result = do
x <- dist False gg13 11 2 nodes
stash result (getAnswer x)
testDistSeqIntermediate :: [NodeId] -> TestResult String -> Process ()
testDistSeqIntermediate nodes result = do
x <- dist False gg124 157 2 nodes
stash result (getAnswer x)
testDistSeqLong :: [NodeId] -> TestResult String -> Process ()
testDistSeqLong nodes result = do
x <- dist False gg1245 157 2 nodes
stash result (getAnswer x)
-- Batch the tests
tests :: [LocalNode] -> [Test]
tests [] = []
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))
]
]
-- Run the tests
orbitTests :: NT.Transport -> IO [Test]
orbitTests transport = do
localNode <- newLocalNode transport rtable
localNode2 <- newLocalNode transport rtable
localNode3 <- newLocalNode transport rtable
let testData = tests [localNode, localNode2, localNode3]
return testData
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
result <- newEmptyMVar
_ <- forkProcess localNode $ testProc result
assertComplete note result expected
-- | 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
b <- takeMVar mv
assertBool msg (a == b)
-- | Given a @builder@ function, make and run a test suite on a single transport
testMain :: (NT.Transport -> IO [Test]) -> IO ()
testMain builder = do
Right (transport, _) <-
createTransportExposeInternals "127.0.0.1" "10501" defaultTCPParameters
testData <- builder transport
defaultMain testData