Repository navigation
Expand file tree
/
Copy pathUtils.hs
More file actions
228 lines (193 loc) · 6.51 KB
/
Copy pathUtils.hs
File metadata and controls
228 lines (193 loc) · 6.51 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE CPP #-}
-----------------------------------------------------------------------------
-- |
-- Module : Control.Distributed.Process.Tests.Internal.Utils
-- Copyright : (c) Tim Watson, Jeff Epstein 2013
-- License : BSD3 (see the file LICENSE)
--
-- Maintainer : Tim Watson
-- Stability : experimental
-- Portability : non-portable (requires concurrency)
--
-- This module provides basic building blocks for testing Cloud Haskell programs.
-----------------------------------------------------------------------------
module Control.Distributed.Process.Tests.Internal.Utils
( TestResult
-- ping !
, Ping(Ping)
, ping
, pause
, shouldContain
, shouldNotContain
, synchronisedAssertion
-- test process utilities
, TestProcessControl
, startTestProcess
, runTestProcess
, testProcessGo
, testProcessStop
, testProcessReport
, delayedAssertion
, assertComplete
, (@^?=)
-- logging
, Logger()
, newLogger
, putLogMsg
, stopLogger
-- runners
, tryRunProcess
, tryForkProcess
, noop
, stash
) where
import Control.Concurrent
( ThreadId
, myThreadId
, forkIO
)
import Control.Concurrent.STM
( TQueue
, newTQueueIO
, readTQueue
, writeTQueue
)
import Control.Concurrent.MVar
( MVar
, newEmptyMVar
, takeMVar
)
import Control.Concurrent
( throwTo
)
import Control.Concurrent.MVar
( putMVar
)
import Control.Distributed.Process hiding (finally, catch)
import Control.Distributed.Process.Node
import Control.Distributed.Process.Serializable()
import Control.Exception (AsyncException(ThreadKilled), SomeException)
import Control.Monad (forever, void)
import Control.Monad.Catch (finally, catch)
import Control.Monad.STM (atomically)
import Data.Binary
import Data.Typeable (Typeable)
import Test.Tasty.HUnit (Assertion, assertBool, assertEqual)
import GHC.Generics
import System.Timeout (timeout)
-- | A mutable cell containing a test result.
type TestResult a = MVar a
-- | A simple @Ping@ signal
data Ping = Ping
deriving (Typeable, Generic, Eq, Show)
instance Binary Ping where
ping :: ProcessId -> Process ()
ping pid = send pid Ping
-- | Control signals used to manage /test processes/
data TestProcessControl = Stop | Go | Report ProcessId
deriving (Typeable, Generic)
instance Binary TestProcessControl where
data Private = Private
deriving (Typeable, Generic)
instance Binary Private where
-- | Does exactly what it says on the tin, doing so in the @Process@ monad.
noop :: Process ()
noop = return ()
pause :: Int -> Process ()
pause delay =
void $ receiveTimeout delay [ match (\Private -> return ()) ]
synchronisedAssertion :: Eq a
=> String
-> LocalNode
-> a
-> (TestResult a -> Process ())
-> MVar ()
-> Assertion
synchronisedAssertion note localNode expected testProc lock = do
result <- newEmptyMVar
_ <- forkProcess localNode $ do
acquire lock
finally (testProc result)
(release lock)
assertComplete note result expected
where acquire lock' = liftIO $ takeMVar lock'
release lock' = liftIO $ putMVar lock' ()
stash :: TestResult a -> a -> Process ()
stash mvar x = liftIO $ putMVar mvar x
shouldContain :: (Show a, Eq a) => [a] -> a -> Process ()
shouldContain xs x = liftIO $ assertBool mempty (x `elem` xs)
shouldNotContain :: (Show a, Eq a) => [a] -> a -> Process ()
shouldNotContain xs x = liftIO $ assertBool mempty (not $ x `elem` xs)
-- | 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)
-- synchronised logging
data Logger = Logger { _tid :: ThreadId, msgs :: TQueue String }
-- | Create a new Logger.
-- Logger uses a 'TQueue' to receive and process messages on a worker thread.
newLogger :: IO Logger
newLogger = do
tid <- liftIO $ myThreadId
q <- liftIO $ newTQueueIO
_ <- forkIO $ logger q
return $ Logger tid q
where logger q' = forever $ do
msg <- atomically $ readTQueue q'
putStrLn msg
-- | Send a message to the Logger
putLogMsg :: Logger -> String -> Process ()
putLogMsg logger msg = liftIO $ atomically $ writeTQueue (msgs logger) msg
-- | Stop the worker thread for the given Logger
stopLogger :: Logger -> IO ()
stopLogger = (flip throwTo) ThreadKilled . _tid
-- | Starts a test process on the local node.
startTestProcess :: Process () -> Process ProcessId
startTestProcess proc =
spawnLocal $ do
getSelfPid >>= register "test-process"
runTestProcess proc
-- | Runs a /test process/ around the supplied @proc@, which is executed
-- whenever the outer process loop receives a 'Go' signal.
runTestProcess :: Process () -> Process ()
runTestProcess proc = do
ctl <- expect
case ctl of
Stop -> return ()
Go -> proc >> runTestProcess proc
Report p -> receiveWait [matchAny (\m -> forward m p)] >> runTestProcess proc
-- | Tell a /test process/ to continue executing
testProcessGo :: ProcessId -> Process ()
testProcessGo pid = send pid Go
-- | Tell a /test process/ to stop (i.e., 'terminate')
testProcessStop :: ProcessId -> Process ()
testProcessStop pid = send pid Stop
-- | Tell a /test process/ to send a report (message)
-- back to the calling process
testProcessReport :: ProcessId -> Process ()
testProcessReport pid = do
self <- getSelfPid
send pid $ Report self
tryRunProcess :: LocalNode -> Process () -> IO ()
tryRunProcess node p = do
tid <- liftIO myThreadId
void $ timeout (1000000 * 60 * 5 :: Int) $
runProcess node $ catch p (\e -> liftIO $ throwTo tid (e::SomeException))
tryForkProcess :: LocalNode -> Process () -> IO ProcessId
tryForkProcess node p = do
tid <- liftIO myThreadId
forkProcess node $ catch p (\e -> liftIO $ throwTo tid (e::SomeException))
(@^?=) :: (Eq a, Show a) => Process a -> a -> Process ()
test @^?= expected = test >>= \t -> liftIO $ assertEqual "" expected t