Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 1 | {-# LANGUAGE ScopedTypeVariables #-} |
| 2 | -- |
| 3 | -- Licensed to the Apache Software Foundation (ASF) under one |
| 4 | -- or more contributor license agreements. See the NOTICE file |
| 5 | -- distributed with this work for additional information |
| 6 | -- regarding copyright ownership. The ASF licenses this file |
| 7 | -- to you under the Apache License, Version 2.0 (the |
| 8 | -- "License"); you may not use this file except in compliance |
| 9 | -- with the License. You may obtain a copy of the License at |
| 10 | -- |
| 11 | -- http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | -- |
| 13 | -- Unless required by applicable law or agreed to in writing, |
| 14 | -- software distributed under the License is distributed on an |
| 15 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | -- KIND, either express or implied. See the License for the |
| 17 | -- specific language governing permissions and limitations |
| 18 | -- under the License. |
| 19 | -- |
| 20 | |
Roger Meier | da74ff4 | 2012-05-18 09:25:02 +0000 | [diff] [blame] | 21 | {-# LANGUAGE OverloadedStrings #-} |
| 22 | |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 23 | module Main where |
| 24 | |
| 25 | |
| 26 | import qualified Control.Exception |
Roger Meier | da74ff4 | 2012-05-18 09:25:02 +0000 | [diff] [blame] | 27 | import qualified Data.HashMap.Strict as Map |
| 28 | import qualified Data.HashSet as Set |
| 29 | import qualified Data.Vector as Vector |
| 30 | |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 31 | import qualified Network |
| 32 | |
| 33 | import Thrift |
| 34 | import Thrift.Protocol.Binary |
| 35 | import Thrift.Server |
| 36 | import Thrift.Transport.Handle |
| 37 | |
| 38 | import qualified ThriftTestUtils |
| 39 | |
| 40 | import qualified ThriftTest |
| 41 | import qualified ThriftTest_Client as Client |
| 42 | import qualified ThriftTest_Iface as Iface |
| 43 | import qualified ThriftTest_Types as Types |
| 44 | |
| 45 | |
| 46 | data TestHandler = TestHandler |
| 47 | instance Iface.ThriftTest_Iface TestHandler where |
| 48 | testVoid _ = return () |
| 49 | |
| 50 | testString _ (Just s) = do |
Roger Meier | da74ff4 | 2012-05-18 09:25:02 +0000 | [diff] [blame] | 51 | ThriftTestUtils.serverLog $ show s |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 52 | return s |
| 53 | |
| 54 | testString _ Nothing = do |
| 55 | error $ "Unsupported testString form" |
| 56 | |
| 57 | testByte _ (Just x) = do |
| 58 | ThriftTestUtils.serverLog $ show x |
| 59 | return x |
| 60 | |
| 61 | testByte _ Nothing = do |
| 62 | error $ "Unsupported testByte form" |
| 63 | |
| 64 | testI32 _ (Just x) = do |
| 65 | ThriftTestUtils.serverLog $ show x |
| 66 | return x |
| 67 | |
| 68 | testI32 _ Nothing = do |
| 69 | error $ "Unsupported testI32 form" |
| 70 | |
| 71 | testI64 _ (Just x) = do |
| 72 | ThriftTestUtils.serverLog $ show x |
| 73 | return x |
| 74 | |
| 75 | testI64 _ Nothing = do |
| 76 | error $ "Unsupported testI64 form" |
| 77 | |
| 78 | testDouble _ (Just x) = do |
| 79 | ThriftTestUtils.serverLog $ show x |
| 80 | return x |
| 81 | |
| 82 | testDouble _ Nothing = do |
| 83 | error $ "Unsupported testDouble form" |
| 84 | |
| 85 | testStruct _ (Just x) = do |
| 86 | ThriftTestUtils.serverLog $ show x |
| 87 | return x |
| 88 | |
| 89 | testStruct _ Nothing = do |
| 90 | error $ "Unsupported testStruct form" |
| 91 | |
| 92 | testNest _ (Just x) = do |
| 93 | ThriftTestUtils.serverLog $ show x |
| 94 | return x |
| 95 | |
| 96 | testNest _ Nothing = do |
| 97 | error $ "Unsupported testNest form" |
| 98 | |
| 99 | testMap _ (Just x) = do |
| 100 | ThriftTestUtils.serverLog $ show x |
| 101 | return x |
| 102 | |
| 103 | testMap _ Nothing = do |
| 104 | error $ "Unsupported testMap form" |
| 105 | |
Roger Meier | 0680a83 | 2011-06-21 04:42:43 +0000 | [diff] [blame] | 106 | testStringMap _ (Just x) = do |
| 107 | ThriftTestUtils.serverLog $ show x |
| 108 | return x |
| 109 | |
| 110 | testStringMap _ Nothing = do |
| 111 | error $ "Unsupported testMap form" |
| 112 | |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 113 | testSet _ (Just x) = do |
| 114 | ThriftTestUtils.serverLog $ show x |
| 115 | return x |
| 116 | |
| 117 | testSet _ Nothing = do |
| 118 | error $ "Unsupported testSet form" |
| 119 | |
| 120 | testList _ (Just x) = do |
| 121 | ThriftTestUtils.serverLog $ show x |
| 122 | return x |
| 123 | |
| 124 | testList _ Nothing = do |
| 125 | error $ "Unsupported testList form" |
| 126 | |
| 127 | testEnum _ (Just x) = do |
| 128 | ThriftTestUtils.serverLog $ show x |
| 129 | return x |
| 130 | |
| 131 | testEnum _ Nothing = do |
| 132 | error $ "Unsupported testEnum form" |
| 133 | |
| 134 | testTypedef _ (Just x) = do |
| 135 | ThriftTestUtils.serverLog $ show x |
| 136 | return x |
| 137 | |
| 138 | testTypedef _ Nothing = do |
| 139 | error $ "Unsupported testTypedef form" |
| 140 | |
| 141 | testMapMap _ (Just _) = do |
| 142 | return (Map.fromList [(1, Map.fromList [(2, 2)])]) |
| 143 | |
| 144 | testMapMap _ Nothing = do |
| 145 | error $ "Unsupported testMapMap form" |
| 146 | |
| 147 | testInsanity _ (Just x) = do |
| 148 | return (Map.fromList [(1, Map.fromList [(Types.ONE, x)])]) |
| 149 | |
| 150 | testInsanity _ Nothing = do |
| 151 | error $ "Unsupported testInsanity form" |
| 152 | |
| 153 | testMulti _ _ _ _ _ _ _ = do |
| 154 | return (Types.Xtruct Nothing Nothing Nothing Nothing) |
| 155 | |
| 156 | testException _ _ = do |
| 157 | Control.Exception.throw (Types.Xception (Just 1) (Just "bya")) |
| 158 | |
| 159 | testMultiException _ _ _ = do |
| 160 | Control.Exception.throw (Types.Xception (Just 1) (Just "xyz")) |
| 161 | |
| 162 | testOneway _ (Just i) = do |
| 163 | ThriftTestUtils.serverLog $ show i |
| 164 | |
| 165 | testOneway _ Nothing = do |
| 166 | error $ "Unsupported testOneway form" |
| 167 | |
| 168 | |
| 169 | client :: (String, Network.PortID) -> IO () |
| 170 | client addr = do |
| 171 | to <- hOpen addr |
| 172 | let ps = (BinaryProtocol to, BinaryProtocol to) |
| 173 | |
| 174 | v1 <- Client.testString ps "bya" |
Roger Meier | da74ff4 | 2012-05-18 09:25:02 +0000 | [diff] [blame] | 175 | ThriftTestUtils.clientLog $ show v1 |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 176 | |
| 177 | v2 <- Client.testByte ps 8 |
| 178 | ThriftTestUtils.clientLog $ show v2 |
| 179 | |
| 180 | v3 <- Client.testByte ps (-8) |
| 181 | ThriftTestUtils.clientLog $ show v3 |
| 182 | |
| 183 | v4 <- Client.testI32 ps 32 |
| 184 | ThriftTestUtils.clientLog $ show v4 |
| 185 | |
| 186 | v5 <- Client.testI32 ps (-32) |
| 187 | ThriftTestUtils.clientLog $ show v5 |
| 188 | |
| 189 | v6 <- Client.testI64 ps 64 |
| 190 | ThriftTestUtils.clientLog $ show v6 |
| 191 | |
| 192 | v7 <- Client.testI64 ps (-64) |
| 193 | ThriftTestUtils.clientLog $ show v7 |
| 194 | |
| 195 | v8 <- Client.testDouble ps 3.14 |
| 196 | ThriftTestUtils.clientLog $ show v8 |
| 197 | |
| 198 | v9 <- Client.testDouble ps (-3.14) |
| 199 | ThriftTestUtils.clientLog $ show v9 |
| 200 | |
| 201 | v10 <- Client.testMap ps (Map.fromList [(1,1),(2,2),(3,3)]) |
| 202 | ThriftTestUtils.clientLog $ show v10 |
| 203 | |
Roger Meier | 0680a83 | 2011-06-21 04:42:43 +0000 | [diff] [blame] | 204 | v11 <- Client.testStringMap ps (Map.fromList [("a","123"),("a b","with spaces "),("same","same"),("0","numeric key")]) |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 205 | ThriftTestUtils.clientLog $ show v11 |
| 206 | |
Roger Meier | da74ff4 | 2012-05-18 09:25:02 +0000 | [diff] [blame] | 207 | v12 <- Client.testList ps (Vector.fromList [1,2,3,4,5]) |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 208 | ThriftTestUtils.clientLog $ show v12 |
| 209 | |
Roger Meier | 0680a83 | 2011-06-21 04:42:43 +0000 | [diff] [blame] | 210 | v13 <- Client.testSet ps (Set.fromList [1,2,3,4,5]) |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 211 | ThriftTestUtils.clientLog $ show v13 |
| 212 | |
Roger Meier | 0680a83 | 2011-06-21 04:42:43 +0000 | [diff] [blame] | 213 | v14 <- Client.testStruct ps (Types.Xtruct (Just "hi") (Just 4) (Just 5) Nothing) |
| 214 | ThriftTestUtils.clientLog $ show v14 |
| 215 | |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 216 | (testException ps "bad") `Control.Exception.catch` testExceptionHandler |
| 217 | |
| 218 | (testMultiException ps "ok") `Control.Exception.catch` testMultiExceptionHandler1 |
| 219 | (testMultiException ps "bad") `Control.Exception.catch` testMultiExceptionHandler2 `Control.Exception.catch` testMultiExceptionHandler3 |
| 220 | |
| 221 | -- ( (Client.testMultiException ps "e" "e2">> ThriftTestUtils.clientLog "bad") `Control.Exception.catch` |
| 222 | |
| 223 | tClose to |
| 224 | where testException ps msg = do |
| 225 | Client.testException ps "e" |
| 226 | ThriftTestUtils.clientLog msg |
| 227 | return () |
| 228 | |
| 229 | testExceptionHandler (e :: Types.Xception) = do |
| 230 | ThriftTestUtils.clientLog $ show e |
| 231 | |
| 232 | testMultiException ps msg = do |
| 233 | _ <- Client.testMultiException ps "e" "e2" |
| 234 | ThriftTestUtils.clientLog msg |
| 235 | return () |
| 236 | |
| 237 | testMultiExceptionHandler1 (e :: Types.Xception) = do |
| 238 | ThriftTestUtils.clientLog $ show e |
| 239 | |
| 240 | testMultiExceptionHandler2 (e :: Types.Xception2) = do |
| 241 | ThriftTestUtils.clientLog $ show e |
| 242 | |
| 243 | testMultiExceptionHandler3 (_ :: Control.Exception.SomeException) = do |
| 244 | ThriftTestUtils.clientLog "ok" |
| 245 | |
| 246 | |
| 247 | server :: Network.PortNumber -> IO () |
| 248 | server port = do |
| 249 | ThriftTestUtils.serverLog "Ready..." |
| 250 | (runBasicServer TestHandler ThriftTest.process port) |
| 251 | `Control.Exception.catch` |
| 252 | (\(TransportExn s _) -> error $ "FAILURE: " ++ s) |
| 253 | |
| 254 | |
| 255 | main :: IO () |
| 256 | main = ThriftTestUtils.runTest server client |