Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [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 | {-# LANGUAGE OverloadedStrings, RecordWildCards, ScopedTypeVariables #-} |
| 21 | module Main where |
| 22 | |
| 23 | import Control.Exception |
| 24 | import Control.Monad |
| 25 | import Data.Functor |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 26 | import Data.List.Split |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 27 | import Data.String |
| 28 | import Network |
| 29 | import System.Environment |
| 30 | import System.Exit |
| 31 | import System.Posix.Unistd |
| 32 | import qualified Data.HashMap.Strict as Map |
| 33 | import qualified Data.HashSet as Set |
| 34 | import qualified Data.Vector as Vector |
| 35 | |
| 36 | import ThriftTest_Iface |
| 37 | import ThriftTest_Types |
| 38 | import qualified ThriftTest_Client as Client |
| 39 | |
| 40 | import Thrift.Transport |
| 41 | import Thrift.Transport.Handle |
| 42 | import Thrift.Protocol |
| 43 | import Thrift.Protocol.Binary |
| 44 | import Thrift.Protocol.Compact |
| 45 | import Thrift.Protocol.JSON |
| 46 | |
| 47 | data Options = Options |
| 48 | { host :: String |
| 49 | , port :: Int |
| 50 | , domainSocket :: String |
| 51 | , transport :: String |
| 52 | , protocol :: ProtocolType |
| 53 | , ssl :: Bool |
| 54 | , testLoops :: Int |
| 55 | } |
| 56 | deriving (Show, Eq) |
| 57 | |
| 58 | data ProtocolType = Binary |
| 59 | | Compact |
| 60 | | JSON |
| 61 | deriving (Show, Eq) |
| 62 | |
| 63 | getProtocol :: String -> ProtocolType |
| 64 | getProtocol "binary" = Binary |
| 65 | getProtocol "compact" = Compact |
| 66 | getProtocol "json" = JSON |
| 67 | getProtocol p = error $ "Unsupported Protocol: " ++ p |
| 68 | |
| 69 | defaultOptions :: Options |
| 70 | defaultOptions = Options |
| 71 | { port = 9090 |
| 72 | , domainSocket = "" |
| 73 | , host = "localhost" |
| 74 | , transport = "framed" |
| 75 | , protocol = Binary |
| 76 | , ssl = False |
| 77 | , testLoops = 1 |
| 78 | } |
| 79 | |
| 80 | runClient :: (Protocol p, Transport t) => p t -> IO () |
| 81 | runClient p = do |
| 82 | let prot = (p,p) |
| 83 | putStrLn "Starting Tests" |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 84 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 85 | -- VOID Test |
| 86 | Client.testVoid prot |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 87 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 88 | -- String Test |
| 89 | s <- Client.testString prot "Test" |
| 90 | when (s /= "Test") exitFailure |
| 91 | |
| 92 | -- Byte Test |
| 93 | byte <- Client.testByte prot 1 |
| 94 | when (byte /= 1) exitFailure |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 95 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 96 | -- I32 Test |
| 97 | i32 <- Client.testI32 prot (-1) |
| 98 | when (i32 /= -1) exitFailure |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 99 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 100 | -- I64 Test |
| 101 | i64 <- Client.testI64 prot (-34359738368) |
| 102 | when (i64 /= -34359738368) exitFailure |
| 103 | |
| 104 | -- Double Test |
| 105 | dub <- Client.testDouble prot (-5.2098523) |
| 106 | when (abs (dub + 5.2098523) > 0.001) exitFailure |
| 107 | |
| 108 | -- Struct Test |
| 109 | let structIn = Xtruct{ xtruct_string_thing = "Zero" |
| 110 | , xtruct_byte_thing = 1 |
| 111 | , xtruct_i32_thing = -3 |
| 112 | , xtruct_i64_thing = -5 |
| 113 | } |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 114 | structOut <- Client.testStruct prot structIn |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 115 | when (structIn /= structOut) exitFailure |
| 116 | |
| 117 | -- Nested Struct Test |
| 118 | let nestIn = Xtruct2{ xtruct2_byte_thing = 1 |
| 119 | , xtruct2_struct_thing = structIn |
| 120 | , xtruct2_i32_thing = 5 |
| 121 | } |
| 122 | nestOut <- Client.testNest prot nestIn |
| 123 | when (nestIn /= nestOut) exitSuccess |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 124 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 125 | -- Map Test |
| 126 | let mapIn = Map.fromList $ map (\i -> (i, i-10)) [1..5] |
| 127 | mapOut <- Client.testMap prot mapIn |
| 128 | when (mapIn /= mapOut) exitSuccess |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 129 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 130 | -- Set Test |
| 131 | let setIn = Set.fromList [-2..3] |
| 132 | setOut <- Client.testSet prot setIn |
| 133 | when (setIn /= setOut) exitFailure |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 134 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 135 | -- List Test |
| 136 | let listIn = Vector.fromList [-2..3] |
| 137 | listOut <- Client.testList prot listIn |
| 138 | when (listIn /= listOut) exitFailure |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 139 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 140 | -- Enum Test |
| 141 | numz1 <- Client.testEnum prot ONE |
| 142 | when (numz1 /= ONE) exitFailure |
| 143 | |
| 144 | numz2 <- Client.testEnum prot TWO |
| 145 | when (numz2 /= TWO) exitFailure |
| 146 | |
| 147 | numz5 <- Client.testEnum prot FIVE |
| 148 | when (numz5 /= FIVE) exitFailure |
| 149 | |
| 150 | -- Typedef Test |
| 151 | uid <- Client.testTypedef prot 309858235082523 |
| 152 | when (uid /= 309858235082523) exitFailure |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 153 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 154 | -- Nested Map Test |
| 155 | _ <- Client.testMapMap prot 1 |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 156 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 157 | -- Exception Test |
| 158 | exn1 <- try $ Client.testException prot "Xception" |
| 159 | case exn1 of |
| 160 | Left (Xception _ _) -> return () |
| 161 | _ -> putStrLn (show exn1) >> exitFailure |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 162 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 163 | exn2 <- try $ Client.testException prot "TException" |
| 164 | case exn2 of |
| 165 | Left (_ :: SomeException) -> return () |
| 166 | Right _ -> exitFailure |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 167 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 168 | exn3 <- try $ Client.testException prot "success" |
| 169 | case exn3 of |
| 170 | Left (_ :: SomeException) -> exitFailure |
| 171 | Right _ -> return () |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 172 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 173 | -- Multi Exception Test |
| 174 | multi1 <- try $ Client.testMultiException prot "Xception" "test 1" |
| 175 | case multi1 of |
| 176 | Left (Xception _ _) -> return () |
| 177 | _ -> exitFailure |
| 178 | |
| 179 | multi2 <- try $ Client.testMultiException prot "Xception2" "test 2" |
| 180 | case multi2 of |
| 181 | Left (Xception2 _ _) -> return () |
| 182 | _ -> exitFailure |
| 183 | |
| 184 | multi3 <- try $ Client.testMultiException prot "success" "test 3" |
| 185 | case multi3 of |
| 186 | Left (_ :: SomeException) -> exitFailure |
| 187 | Right _ -> return () |
| 188 | |
| 189 | |
| 190 | main :: IO () |
| 191 | main = do |
| 192 | options <- flip parseFlags defaultOptions <$> getArgs |
| 193 | case options of |
| 194 | Nothing -> showHelp |
| 195 | Just Options{..} -> do |
| 196 | handle <- hOpen (host, PortNumber $ fromIntegral port) |
| 197 | let client = case protocol of |
| 198 | Binary -> runClient $ BinaryProtocol handle |
| 199 | Compact -> runClient $ CompactProtocol handle |
| 200 | JSON -> runClient $ JSONProtocol handle |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 201 | replicateM_ testLoops client |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 202 | putStrLn "COMPLETED SUCCESSFULLY" |
| 203 | |
| 204 | parseFlags :: [String] -> Options -> Maybe Options |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 205 | parseFlags (flag : flags) opts = do |
| 206 | let pieces = splitOn "=" flag |
| 207 | case pieces of |
| 208 | "--port" : arg : _ -> parseFlags flags opts{ port = read arg } |
| 209 | "--domain-socket" : arg : _ -> parseFlags flags opts{ domainSocket = read arg } |
| 210 | "--host" : arg : _ -> parseFlags flags opts{ host = arg } |
| 211 | "--transport" : arg : _ -> parseFlags flags opts{ transport = arg } |
| 212 | "--protocol" : arg : _ -> parseFlags flags opts{ protocol = getProtocol arg } |
| 213 | "--h" : _ -> Nothing |
| 214 | "--help" : _ -> Nothing |
| 215 | "--ssl" : _ -> parseFlags flags opts{ ssl = True } |
| 216 | "--processor-events" : _ -> parseFlags flags opts |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 217 | parseFlags (flag : arg : flags) opts |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 218 | | flag == "-n" = parseFlags flags opts{ testLoops = read arg } |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 219 | parseFlags [] opts = Just opts |
| 220 | |
| 221 | showHelp :: IO () |
| 222 | showHelp = putStrLn |
| 223 | "Allowed options:\n\ |
| 224 | \ -h [ --help ] produce help message\n\ |
| 225 | \ --host arg (=localhost) Host to connect\n\ |
| 226 | \ --port arg (=9090) Port number to connect\n\ |
cdwijayarathna | d921791 | 2014-08-15 22:18:30 +0530 | [diff] [blame^] | 227 | \ --domain-socket arg Domain Socket (e.g. /tmp/ThriftTest.thrift),\n\ |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 228 | \ instead of host and port\n\ |
| 229 | \ --transport arg (=buffered) Transport: buffered, framed, http, evhttp\n\ |
| 230 | \ --protocol arg (=binary) Protocol: binary, compact, json\n\ |
| 231 | \ --ssl Encrypted Transport using SSL\n\ |
| 232 | \ -n [ --testloops ] arg (=1) Number of Tests" |