blob: 1139506cd43f768faa7e09d39929a7459f795f49 [file] [log] [blame]
Bryan Duxburyc6574472010-10-06 00:12:33 +00001{-# 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 Meierda74ff42012-05-18 09:25:02 +000021{-# LANGUAGE OverloadedStrings #-}
22
Bryan Duxburyc6574472010-10-06 00:12:33 +000023module Main where
24
25
26import qualified Control.Exception
Roger Meierda74ff42012-05-18 09:25:02 +000027import qualified Data.HashMap.Strict as Map
28import qualified Data.HashSet as Set
29import qualified Data.Vector as Vector
30
Bryan Duxburyc6574472010-10-06 00:12:33 +000031import qualified Network
32
33import Thrift
34import Thrift.Protocol.Binary
35import Thrift.Server
36import Thrift.Transport.Handle
37
38import qualified ThriftTestUtils
39
40import qualified ThriftTest
41import qualified ThriftTest_Client as Client
42import qualified ThriftTest_Iface as Iface
43import qualified ThriftTest_Types as Types
44
45
46data TestHandler = TestHandler
47instance Iface.ThriftTest_Iface TestHandler where
48 testVoid _ = return ()
49
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070050 testString _ s = do
Roger Meierda74ff42012-05-18 09:25:02 +000051 ThriftTestUtils.serverLog $ show s
Bryan Duxburyc6574472010-10-06 00:12:33 +000052 return s
53
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070054 testByte _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +000055 ThriftTestUtils.serverLog $ show x
56 return x
57
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070058 testI32 _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +000059 ThriftTestUtils.serverLog $ show x
60 return x
61
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070062 testI64 _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +000063 ThriftTestUtils.serverLog $ show x
64 return x
65
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070066 testDouble _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +000067 ThriftTestUtils.serverLog $ show x
68 return x
69
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070070 testStruct _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +000071 ThriftTestUtils.serverLog $ show x
72 return x
73
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070074 testNest _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +000075 ThriftTestUtils.serverLog $ show x
76 return x
77
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070078 testMap _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +000079 ThriftTestUtils.serverLog $ show x
80 return x
81
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070082 testStringMap _ x = do
Roger Meier0680a832011-06-21 04:42:43 +000083 ThriftTestUtils.serverLog $ show x
84 return x
85
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070086 testSet _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +000087 ThriftTestUtils.serverLog $ show x
88 return x
89
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070090 testList _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +000091 ThriftTestUtils.serverLog $ show x
92 return x
93
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070094 testEnum _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +000095 ThriftTestUtils.serverLog $ show x
96 return x
97
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070098 testTypedef _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +000099 ThriftTestUtils.serverLog $ show x
100 return x
101
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -0700102 testMapMap _ _ = do
Bryan Duxburyc6574472010-10-06 00:12:33 +0000103 return (Map.fromList [(1, Map.fromList [(2, 2)])])
104
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -0700105 testInsanity _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +0000106 return (Map.fromList [(1, Map.fromList [(Types.ONE, x)])])
107
Bryan Duxburyc6574472010-10-06 00:12:33 +0000108 testMulti _ _ _ _ _ _ _ = do
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -0700109 return (Types.Xtruct "" 0 0 0)
Bryan Duxburyc6574472010-10-06 00:12:33 +0000110
111 testException _ _ = do
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -0700112 Control.Exception.throw (Types.Xception 1 "bya")
Bryan Duxburyc6574472010-10-06 00:12:33 +0000113
114 testMultiException _ _ _ = do
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -0700115 Control.Exception.throw (Types.Xception 1 "xyz")
Bryan Duxburyc6574472010-10-06 00:12:33 +0000116
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -0700117 testOneway _ i = do
Bryan Duxburyc6574472010-10-06 00:12:33 +0000118 ThriftTestUtils.serverLog $ show i
119
Bryan Duxburyc6574472010-10-06 00:12:33 +0000120
121client :: (String, Network.PortID) -> IO ()
122client addr = do
123 to <- hOpen addr
124 let ps = (BinaryProtocol to, BinaryProtocol to)
125
126 v1 <- Client.testString ps "bya"
Roger Meierda74ff42012-05-18 09:25:02 +0000127 ThriftTestUtils.clientLog $ show v1
Bryan Duxburyc6574472010-10-06 00:12:33 +0000128
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 Meier0680a832011-06-21 04:42:43 +0000156 v11 <- Client.testStringMap ps (Map.fromList [("a","123"),("a b","with spaces "),("same","same"),("0","numeric key")])
Bryan Duxburyc6574472010-10-06 00:12:33 +0000157 ThriftTestUtils.clientLog $ show v11
158
Roger Meierda74ff42012-05-18 09:25:02 +0000159 v12 <- Client.testList ps (Vector.fromList [1,2,3,4,5])
Bryan Duxburyc6574472010-10-06 00:12:33 +0000160 ThriftTestUtils.clientLog $ show v12
161
Roger Meier0680a832011-06-21 04:42:43 +0000162 v13 <- Client.testSet ps (Set.fromList [1,2,3,4,5])
Bryan Duxburyc6574472010-10-06 00:12:33 +0000163 ThriftTestUtils.clientLog $ show v13
164
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -0700165 v14 <- Client.testStruct ps (Types.Xtruct "hi" 4 5 0)
Roger Meier0680a832011-06-21 04:42:43 +0000166 ThriftTestUtils.clientLog $ show v14
167
Bryan Duxburyc6574472010-10-06 00:12:33 +0000168 (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 Zilbersteinaf5d64a2014-07-31 15:44:13 -0700177 _ <- Client.testException ps "e"
Bryan Duxburyc6574472010-10-06 00:12:33 +0000178 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
199server :: Network.PortNumber -> IO ()
200server port = do
201 ThriftTestUtils.serverLog "Ready..."
202 (runBasicServer TestHandler ThriftTest.process port)
203 `Control.Exception.catch`
204 (\(TransportExn s _) -> error $ "FAILURE: " ++ s)
205
206
207main :: IO ()
208main = ThriftTestUtils.runTest server client