blob: 670023e2985f7fdf84f2f72c430bdf907f649864 [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
Jens Geyer8bcfdd92014-12-14 03:14:26 +010070 testBinary _ x = do
71 ThriftTestUtils.serverLog $ show x
72 return x
73
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070074 testStruct _ 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 testNest _ 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 testMap _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +000083 ThriftTestUtils.serverLog $ show x
84 return x
85
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070086 testStringMap _ x = do
Roger Meier0680a832011-06-21 04:42:43 +000087 ThriftTestUtils.serverLog $ show x
88 return x
89
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070090 testSet _ 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 testList _ 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 testEnum _ 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 testTypedef _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +0000103 ThriftTestUtils.serverLog $ show x
104 return x
105
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -0700106 testMapMap _ _ = do
Bryan Duxburyc6574472010-10-06 00:12:33 +0000107 return (Map.fromList [(1, Map.fromList [(2, 2)])])
108
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -0700109 testInsanity _ x = do
Bryan Duxburyc6574472010-10-06 00:12:33 +0000110 return (Map.fromList [(1, Map.fromList [(Types.ONE, x)])])
111
Bryan Duxburyc6574472010-10-06 00:12:33 +0000112 testMulti _ _ _ _ _ _ _ = do
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -0700113 return (Types.Xtruct "" 0 0 0)
Bryan Duxburyc6574472010-10-06 00:12:33 +0000114
115 testException _ _ = do
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -0700116 Control.Exception.throw (Types.Xception 1 "bya")
Bryan Duxburyc6574472010-10-06 00:12:33 +0000117
118 testMultiException _ _ _ = do
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -0700119 Control.Exception.throw (Types.Xception 1 "xyz")
Bryan Duxburyc6574472010-10-06 00:12:33 +0000120
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -0700121 testOneway _ i = do
Bryan Duxburyc6574472010-10-06 00:12:33 +0000122 ThriftTestUtils.serverLog $ show i
123
Bryan Duxburyc6574472010-10-06 00:12:33 +0000124
125client :: (String, Network.PortID) -> IO ()
126client addr = do
127 to <- hOpen addr
128 let ps = (BinaryProtocol to, BinaryProtocol to)
129
130 v1 <- Client.testString ps "bya"
Roger Meierda74ff42012-05-18 09:25:02 +0000131 ThriftTestUtils.clientLog $ show v1
Bryan Duxburyc6574472010-10-06 00:12:33 +0000132
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 Geyer8bcfdd92014-12-14 03:14:26 +0100157 -- TODO: Client.testBinary ...
158
Bryan Duxburyc6574472010-10-06 00:12:33 +0000159 v10 <- Client.testMap ps (Map.fromList [(1,1),(2,2),(3,3)])
160 ThriftTestUtils.clientLog $ show v10
161
Roger Meier0680a832011-06-21 04:42:43 +0000162 v11 <- Client.testStringMap ps (Map.fromList [("a","123"),("a b","with spaces "),("same","same"),("0","numeric key")])
Bryan Duxburyc6574472010-10-06 00:12:33 +0000163 ThriftTestUtils.clientLog $ show v11
164
Roger Meierda74ff42012-05-18 09:25:02 +0000165 v12 <- Client.testList ps (Vector.fromList [1,2,3,4,5])
Bryan Duxburyc6574472010-10-06 00:12:33 +0000166 ThriftTestUtils.clientLog $ show v12
167
Roger Meier0680a832011-06-21 04:42:43 +0000168 v13 <- Client.testSet ps (Set.fromList [1,2,3,4,5])
Bryan Duxburyc6574472010-10-06 00:12:33 +0000169 ThriftTestUtils.clientLog $ show v13
170
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -0700171 v14 <- Client.testStruct ps (Types.Xtruct "hi" 4 5 0)
Roger Meier0680a832011-06-21 04:42:43 +0000172 ThriftTestUtils.clientLog $ show v14
173
Bryan Duxburyc6574472010-10-06 00:12:33 +0000174 (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 Zilbersteinaf5d64a2014-07-31 15:44:13 -0700183 _ <- Client.testException ps "e"
Bryan Duxburyc6574472010-10-06 00:12:33 +0000184 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
205server :: Network.PortNumber -> IO ()
206server port = do
207 ThriftTestUtils.serverLog "Ready..."
208 (runBasicServer TestHandler ThriftTest.process port)
209 `Control.Exception.catch`
210 (\(TransportExn s _) -> error $ "FAILURE: " ++ s)
211
212
213main :: IO ()
214main = ThriftTestUtils.runTest server client