Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 1 | -- |
| 2 | -- Licensed to the Apache Software Foundation (ASF) under one |
| 3 | -- or more contributor license agreements. See the NOTICE file |
| 4 | -- distributed with this work for additional information |
| 5 | -- regarding copyright ownership. The ASF licenses this file |
| 6 | -- to you under the Apache License, Version 2.0 (the |
| 7 | -- "License"); you may not use this file except in compliance |
| 8 | -- with the License. You may obtain a copy of the License at |
| 9 | -- |
| 10 | -- http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | -- |
| 12 | -- Unless required by applicable law or agreed to in writing, |
| 13 | -- software distributed under the License is distributed on an |
| 14 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | -- KIND, either express or implied. See the License for the |
| 16 | -- specific language governing permissions and limitations |
| 17 | -- under the License. |
| 18 | -- |
| 19 | |
| 20 | module ThriftTestUtils (ClientFunc, ServerFunc, clientLog, serverLog, testLog, runTest) where |
| 21 | |
| 22 | |
| 23 | import qualified Control.Concurrent |
| 24 | import qualified Network |
| 25 | import qualified System.IO |
| 26 | |
| 27 | |
| 28 | serverPort :: Network.PortNumber |
| 29 | serverPort = 9090 |
| 30 | |
| 31 | serverAddress :: (String, Network.PortID) |
| 32 | serverAddress = ("localhost", Network.PortNumber serverPort) |
| 33 | |
| 34 | |
| 35 | testLog :: String -> IO () |
| 36 | testLog str = do |
| 37 | System.IO.hPutStr System.IO.stdout $ str ++ "\n" |
| 38 | System.IO.hFlush System.IO.stdout |
| 39 | |
| 40 | |
| 41 | clientLog :: String -> IO () |
| 42 | clientLog str = testLog $ "CLIENT: " ++ str |
| 43 | |
| 44 | serverLog :: String -> IO () |
| 45 | serverLog str = testLog $ "SERVER: " ++ str |
| 46 | |
| 47 | |
| 48 | type ServerFunc = Network.PortNumber -> IO () |
| 49 | type ClientFunc = (String, Network.PortID) -> IO () |
| 50 | |
| 51 | runTest :: ServerFunc -> ClientFunc -> IO () |
| 52 | runTest server client = do |
| 53 | _ <- Control.Concurrent.forkIO (server serverPort) |
| 54 | |
| 55 | -- Fairly horrible; this does not 100% guarantees that the other thread |
| 56 | -- has actually opened the socket we need... but not much else we can do |
| 57 | -- without this, the client races the server to the socket, and wins every |
| 58 | -- time |
| 59 | Control.Concurrent.yield |
| 60 | Control.Concurrent.threadDelay $ 500 * 1000 -- unit is in _micro_seconds |
| 61 | Control.Concurrent.yield |
| 62 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 63 | client serverAddress |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 64 | |
| 65 | testLog "SUCCESS" |