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
|
module Main(main) where
import Network.EBus.Layer2 as L2
-- main = do
-- -- * Select binary mode (True) or text mode (False) on a open handle. (See also openBinaryFile.)
-- hSetBinaryMode stdin True
-- -- * run
-- -- Run an iteratee until it finishes, and return either the final value (if it succeeded) or the error (if it failed).
-- -- * run_
-- -- Like run, except errors are converted to exceptions and thrown. Primarily useful for small scripts or other simple cases.
-- maybePacket <- run( enumSource $$ runParser )
-- case maybePacket of
-- Right result -> print result
-- Left error -> print error
-- maybePacket <- run( enumSource $$ runParser )
-- case maybePacket of
-- Right result -> print result
-- Left error -> print error
-- enumSource :: Enumerator ByteString IO a
-- enumSource = enumHandle 1 stdin
-- runParser :: Iteratee ByteString IO EbusPacket
-- runParser = do
-- p <- iterParser parser
-- return p
import Test.Framework
import Test.Framework.Providers.HUnit
main =
defaultMain [
testGroup "SomeModule" [ testCase "foo" foooo]
]
foooo :: IO()
foooo = do
print "OK"
-- import Control.Concurrent (forkOS)
-- import Control.Concurrent.MVar (MVar, newEmptyMVar, readMVar, putMVar)
-- import Control.Exception (finally)
-- import Control.Monad.Trans (liftIO)
-- import qualified Data.Binary as B
-- import qualified System.ZMQ as ZMQ
-- import Control.Monad.BinaryProtocol.ZMQ
-- (BinaryProtocol, runProtocol, send, receive, flush)
-- main :: IO ()
-- main = defaultMain tests
-- tests :: [TF.Test]
-- tests =
-- [ testGroup "unidirectional communications"
-- [ testCase "send unit" testSendUnit
-- , testCase "send number" testSendNumber
-- , testCase "send list of numbers" testSendListOfNumbers
-- ]
-- , testGroup "bidirectional communications"
-- [ testCase "addition" testAddition
-- ]
-- ]
-- makeChannels :: ZMQ.Context -> String -> IO (ZMQ.Socket ZMQ.Up,
-- ZMQ.Socket ZMQ.Down)
-- makeChannels ctx address = do
-- chan1 <- ZMQ.socket ctx ZMQ.Up
-- chan2 <- ZMQ.socket ctx ZMQ.Down
-- ZMQ.bind chan1 address
-- ZMQ.connect chan2 address
-- return (chan1, chan2)
-- makeSendTest :: (B.Binary a, Eq a, Show a) => a -> IO ()
-- makeSendTest value = do
-- ctx <- ZMQ.init 1
-- (chan_in, chan_out) <- makeChannels ctx "inproc://pipe"
-- result <- runProtocol actions chan_in chan_out `finally` do
-- ZMQ.close chan_out
-- ZMQ.close chan_in
-- ZMQ.term ctx
-- assertEqual "Was the correct value received?" value result
-- where actions = do
-- send value
-- flush
-- receive
-- testSendUnit :: IO ()
-- testSendUnit = makeSendTest ()
-- testSendNumber :: IO ()
-- testSendNumber = makeSendTest (3 :: Int)
-- testSendListOfNumbers :: IO ()
-- testSendListOfNumbers = makeSendTest [3 :: Int, 4, 5, 6]
-- makeExchangeTest :: (B.Binary a, Show a, Eq a) =>
-- a ->
-- (MVar a -> BinaryProtocol ZMQ.Up ZMQ.Down ()) ->
-- (MVar a -> BinaryProtocol ZMQ.Up ZMQ.Down ()) ->
-- IO ()
-- makeExchangeTest correct_result protocol1 protocol2 = do
-- resultMVar <- newEmptyMVar
-- ctx <- ZMQ.init 1
-- lock1 <- newEmptyMVar
-- lock2 <- newEmptyMVar
-- -- ZeroMQ sockets can only be used in the thread which created them.
-- -- We need some magic to get this right.
-- f $ forkOS $ runProtocol' address1 address2 ctx lock1 lock2
-- (protocol1 resultMVar)
-- f $ forkOS $ runProtocol' address2 address1 ctx lock2 lock1
-- (protocol2 resultMVar)
-- result <- readMVar resultMVar `finally` ZMQ.term ctx
-- assertEqual "Was the correct result computed?" correct_result result
-- where address1 = "inproc://pipe1"
-- address2 = "inproc://pipe2"
-- f :: IO a -> IO ()
-- f a = a >> return ()
-- runProtocol' :: String -> String -> ZMQ.Context ->
-- MVar () -> MVar () ->
-- BinaryProtocol ZMQ.Up ZMQ.Down () -> IO ()
-- runProtocol' a1 a2 ctx l1 l2 p = do
-- chan_in <- ZMQ.socket ctx ZMQ.Up
-- chan_out <- ZMQ.socket ctx ZMQ.Down
-- ZMQ.bind chan_in a1
-- putMVar l1 ()
-- f $ readMVar l2
-- ZMQ.connect chan_out a2
-- runProtocol p chan_in chan_out `finally` do
-- ZMQ.close chan_in
-- ZMQ.close chan_out
-- testAddition :: IO ()
-- testAddition =
-- makeExchangeTest (3 :: Int)
-- (\resultMVar -> do
-- send (1 :: Int)
-- flush
-- receive >>= liftIO . putMVar resultMVar
-- )
-- (\_ -> do
-- a <- receive
-- send (a + (2 :: Int))
-- flush
-- )
|