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 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 50 | testString _ 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 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 54 | testByte _ x = do |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 55 | ThriftTestUtils.serverLog $ show x |
| 56 | return x |
| 57 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 58 | testI32 _ x = do |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 59 | ThriftTestUtils.serverLog $ show x |
| 60 | return x |
| 61 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 62 | testI64 _ x = do |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 63 | ThriftTestUtils.serverLog $ show x |
| 64 | return x |
| 65 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 66 | testDouble _ x = do |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 67 | ThriftTestUtils.serverLog $ show x |
| 68 | return x |
| 69 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 70 | testStruct _ x = do |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 71 | ThriftTestUtils.serverLog $ show x |
| 72 | return x |
| 73 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 74 | testNest _ x = do |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 75 | ThriftTestUtils.serverLog $ show x |
| 76 | return x |
| 77 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 78 | testMap _ x = do |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 79 | ThriftTestUtils.serverLog $ show x |
| 80 | return x |
| 81 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 82 | testStringMap _ x = do |
Roger Meier | 0680a83 | 2011-06-21 04:42:43 +0000 | [diff] [blame] | 83 | ThriftTestUtils.serverLog $ show x |
| 84 | return x |
| 85 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 86 | testSet _ x = do |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 87 | ThriftTestUtils.serverLog $ show x |
| 88 | return x |
| 89 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 90 | testList _ x = do |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 91 | ThriftTestUtils.serverLog $ show x |
| 92 | return x |
| 93 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 94 | testEnum _ x = do |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 95 | ThriftTestUtils.serverLog $ show x |
| 96 | return x |
| 97 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 98 | testTypedef _ x = do |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 99 | ThriftTestUtils.serverLog $ show x |
| 100 | return x |
| 101 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 102 | testMapMap _ _ = do |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 103 | return (Map.fromList [(1, Map.fromList [(2, 2)])]) |
| 104 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 105 | testInsanity _ x = do |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 106 | return (Map.fromList [(1, Map.fromList [(Types.ONE, x)])]) |
| 107 | |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 108 | testMulti _ _ _ _ _ _ _ = do |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 109 | return (Types.Xtruct "" 0 0 0) |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 110 | |
| 111 | testException _ _ = do |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 112 | Control.Exception.throw (Types.Xception 1 "bya") |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 113 | |
| 114 | testMultiException _ _ _ = do |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 115 | Control.Exception.throw (Types.Xception 1 "xyz") |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 116 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 117 | testOneway _ i = do |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 118 | ThriftTestUtils.serverLog $ show i |
| 119 | |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 120 | |
| 121 | client :: (String, Network.PortID) -> IO () |
| 122 | client addr = do |
| 123 | to <- hOpen addr |
| 124 | let ps = (BinaryProtocol to, BinaryProtocol to) |
| 125 | |
| 126 | v1 <- Client.testString ps "bya" |
Roger Meier | da74ff4 | 2012-05-18 09:25:02 +0000 | [diff] [blame] | 127 | ThriftTestUtils.clientLog $ show v1 |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 128 | |
| 129 | v2 <- Client.testByte ps 8 |
| 130 | ThriftTestUtils.clientLog $ show v2 |
| 131 | |
| 132 | v3 <- Client.testByte ps (-8) |
| 133 | ThriftTestUtils.clientLog $ show v3 |
| 134 | |
| 135 | v4 <- Client.testI32 ps 32 |
| 136 | ThriftTestUtils.clientLog $ show v4 |
| 137 | |
| 138 | v5 <- Client.testI32 ps (-32) |
| 139 | ThriftTestUtils.clientLog $ show v5 |
| 140 | |
| 141 | v6 <- Client.testI64 ps 64 |
| 142 | ThriftTestUtils.clientLog $ show v6 |
| 143 | |
| 144 | v7 <- Client.testI64 ps (-64) |
| 145 | ThriftTestUtils.clientLog $ show v7 |
| 146 | |
| 147 | v8 <- Client.testDouble ps 3.14 |
| 148 | ThriftTestUtils.clientLog $ show v8 |
| 149 | |
| 150 | v9 <- Client.testDouble ps (-3.14) |
| 151 | ThriftTestUtils.clientLog $ show v9 |
| 152 | |
| 153 | v10 <- Client.testMap ps (Map.fromList [(1,1),(2,2),(3,3)]) |
| 154 | ThriftTestUtils.clientLog $ show v10 |
| 155 | |
Roger Meier | 0680a83 | 2011-06-21 04:42:43 +0000 | [diff] [blame] | 156 | 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] | 157 | ThriftTestUtils.clientLog $ show v11 |
| 158 | |
Roger Meier | da74ff4 | 2012-05-18 09:25:02 +0000 | [diff] [blame] | 159 | v12 <- Client.testList ps (Vector.fromList [1,2,3,4,5]) |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 160 | ThriftTestUtils.clientLog $ show v12 |
| 161 | |
Roger Meier | 0680a83 | 2011-06-21 04:42:43 +0000 | [diff] [blame] | 162 | v13 <- Client.testSet ps (Set.fromList [1,2,3,4,5]) |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 163 | ThriftTestUtils.clientLog $ show v13 |
| 164 | |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 165 | v14 <- Client.testStruct ps (Types.Xtruct "hi" 4 5 0) |
Roger Meier | 0680a83 | 2011-06-21 04:42:43 +0000 | [diff] [blame] | 166 | ThriftTestUtils.clientLog $ show v14 |
| 167 | |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 168 | (testException ps "bad") `Control.Exception.catch` testExceptionHandler |
| 169 | |
| 170 | (testMultiException ps "ok") `Control.Exception.catch` testMultiExceptionHandler1 |
| 171 | (testMultiException ps "bad") `Control.Exception.catch` testMultiExceptionHandler2 `Control.Exception.catch` testMultiExceptionHandler3 |
| 172 | |
| 173 | -- ( (Client.testMultiException ps "e" "e2">> ThriftTestUtils.clientLog "bad") `Control.Exception.catch` |
| 174 | |
| 175 | tClose to |
| 176 | where testException ps msg = do |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 177 | _ <- Client.testException ps "e" |
Bryan Duxbury | c657447 | 2010-10-06 00:12:33 +0000 | [diff] [blame] | 178 | ThriftTestUtils.clientLog msg |
| 179 | return () |
| 180 | |
| 181 | testExceptionHandler (e :: Types.Xception) = do |
| 182 | ThriftTestUtils.clientLog $ show e |
| 183 | |
| 184 | testMultiException ps msg = do |
| 185 | _ <- Client.testMultiException ps "e" "e2" |
| 186 | ThriftTestUtils.clientLog msg |
| 187 | return () |
| 188 | |
| 189 | testMultiExceptionHandler1 (e :: Types.Xception) = do |
| 190 | ThriftTestUtils.clientLog $ show e |
| 191 | |
| 192 | testMultiExceptionHandler2 (e :: Types.Xception2) = do |
| 193 | ThriftTestUtils.clientLog $ show e |
| 194 | |
| 195 | testMultiExceptionHandler3 (_ :: Control.Exception.SomeException) = do |
| 196 | ThriftTestUtils.clientLog "ok" |
| 197 | |
| 198 | |
| 199 | server :: Network.PortNumber -> IO () |
| 200 | server port = do |
| 201 | ThriftTestUtils.serverLog "Ready..." |
| 202 | (runBasicServer TestHandler ThriftTest.process port) |
| 203 | `Control.Exception.catch` |
| 204 | (\(TransportExn s _) -> error $ "FAILURE: " ++ s) |
| 205 | |
| 206 | |
| 207 | main :: IO () |
| 208 | main = ThriftTestUtils.runTest server client |