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