blob: 0b421d1ed91baa58d6e5c6c4685cdd798de2c661 [file] [log] [blame]
Nobuaki Sukegawaebd71ce2016-02-04 21:28:22 +09001-- Licensed to the Apache Software Foundation (ASF) under one
2-- or more contributor license agreements. See the NOTICE file
3-- distributed with this work for additional information
4-- regarding copyright ownership. The ASF licenses this file
5-- to you under the Apache License, Version 2.0 (the
6-- "License"); you may not use this file except in compliance
7-- with the License. You may obtain a copy of the License at
8
9-- http://www.apache.org/licenses/LICENSE-2.0
10
11-- Unless required by applicable law or agreed to in writing,
12-- software distributed under the License is distributed on an
13-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14-- KIND, either express or implied. See the License for the
15-- specific language governing permissions and limitations
16-- under the License.
Roger Meier6cf0ffc2014-04-05 00:45:42 +020017
18require('ThriftTest_ThriftTest')
19require('TSocket')
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +090020require('TBufferedTransport')
Roger Meier6cf0ffc2014-04-05 00:45:42 +020021require('TFramedTransport')
Wang Yaofue432c6b2016-03-09 16:39:03 +080022require('THttpTransport')
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +090023require('TCompactProtocol')
24require('TJsonProtocol')
Roger Meier6cf0ffc2014-04-05 00:45:42 +020025require('TBinaryProtocol')
26require('TServer')
Thomaseb684d32024-07-28 15:32:23 +020027local liblualongnumber = require('liblualongnumber')
Roger Meier6cf0ffc2014-04-05 00:45:42 +020028
29--------------------------------------------------------------------------------
30-- Handler
31TestHandler = ThriftTestIface:new{}
32
33-- Stops the server
34function TestHandler:testVoid()
Roger Meier6cf0ffc2014-04-05 00:45:42 +020035end
36
37function TestHandler:testString(str)
38 return str
39end
40
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +090041function TestHandler:testBool(bool)
42 return bool
43end
44
Roger Meier6cf0ffc2014-04-05 00:45:42 +020045function TestHandler:testByte(byte)
46 return byte
47end
48
49function TestHandler:testI32(i32)
50 return i32
51end
52
53function TestHandler:testI64(i64)
54 return i64
55end
56
57function TestHandler:testDouble(d)
58 return d
59end
60
Jens Geyer8bcfdd92014-12-14 03:14:26 +010061function TestHandler:testBinary(by)
62 return by
63end
64
Thomaseb684d32024-07-28 15:32:23 +020065function TestHandler:testUuid(uuid)
66 return uuid
67end
68
69function TestHandler:testNest(thing)
70 return thing
71end
72
Roger Meier6cf0ffc2014-04-05 00:45:42 +020073function TestHandler:testStruct(thing)
74 return thing
75end
76
Thomaseb684d32024-07-28 15:32:23 +020077function TestHandler:testMap(thing)
78 return thing
79end
80
81function TestHandler:testStringMap(thing)
82 return thing
83end
84
85function TestHandler:testSet(thing)
86 return thing
87end
88
89function TestHandler:testList(thing)
90 return thing
91end
92
93function TestHandler:testEnum(thing)
94 return thing
95end
96
97function TestHandler:testTypedef(thing)
98 return thing
99end
100
101function TestHandler:testMapMap(hello)
102 return {
103 ["-4"] = {
104 ["-4"] = -4,
105 ["-3"] = -3,
106 ["-2"] = -2,
107 ["-1"] = -1
108 },
109 ["4"] = {
110 ["1"] = 1,
111 ["2"] = 2,
112 ["3"] = 3,
113 ["4"] = 4
114 }
115 }
116end
117
118function TestHandler:testInsanity(argument)
119 local first_map = {
120 [Numberz.TWO] = argument,
121 [Numberz.THREE] = argument
122 };
123 local second_map = {
124 [Numberz.SIX] = Insanity:new {
125 userMap = {},
126 xtructs = {}
127 }
128 }
129
130 return {
131 ["1"] = first_map,
132 ["2"] = second_map
133 };
134end
135
136function TestHandler:testMulti(arg0, arg1, arg2, arg3, arg4, arg5)
137 return Xtruct:new {}
138end
139
140function TestHandler:testException(arg)
141 if arg == "Xception" then
142 return Xception:new {
143 errorCode = 1001,
144 message = arg
145 }
146 elseif arg == "TException" then
147 error("")
148 end
149end
150
151function TestHandler:testMultiException(arg0, arg1)
152 if arg0 == "Xception" then
153 return Xception:new {
154 errorCode = 1001,
155 message = "This is an Xception"
156 }
157 elseif arg0 == "Xception2" then
158 return Xception2:new {
159 errorCode = 2002,
160 struct_thing = Xtruct:new {
161 string_thing = "This is an Xception2"
162 }
163 }
164 elseif arg0 == "TException" then
165 error("")
166 end
167 return Xtruct:new {
168 string_thing = arg1
169 }
170end
171
longzhiri03715892020-08-04 22:01:09 +0800172function TestHandler:testOneway(secondsToSleep)
173 print("testOneway secondsToSleep:", secondsToSleep)
174end
175
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200176--------------------------------------------------------------------------------
177-- Test
178local server
179
180function teardown()
181 if server then
182 server:close()
183 end
184end
185
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900186function parseArgs(rawArgs)
187 local opt = {
188 protocol='binary',
189 transport='buffered',
190 port='9090',
191 }
192 for i, str in pairs(rawArgs) do
193 if i > 0 then
194 k, v = string.match(str, '--(%w+)=(%w+)')
195 assert(opt[k] ~= nil, 'Unknown argument')
196 opt[k] = v
197 end
198 end
199 return opt
200end
201
202function testBasicServer(rawArgs)
203 local opt = parseArgs(rawArgs)
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200204 -- Handler & Processor
205 local handler = TestHandler:new{}
206 assert(handler, 'Failed to create handler')
207 local processor = ThriftTestProcessor:new{
208 handler = handler
209 }
210 assert(processor, 'Failed to create processor')
211
212 -- Server Socket
213 local socket = TServerSocket:new{
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900214 port = opt.port
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200215 }
216 assert(socket, 'Failed to create server socket')
217
218 -- Transport & Factory
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900219 local transports = {
220 buffered = TBufferedTransportFactory,
221 framed = TFramedTransportFactory,
Wang Yaofue432c6b2016-03-09 16:39:03 +0800222 http = THttpTransportFactory,
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900223 }
224 assert(transports[opt.transport], 'Failed to create framed transport factory')
225 local trans_factory = transports[opt.transport]:new{}
226 local protocols = {
227 binary = TBinaryProtocolFactory,
228 compact = TCompactProtocolFactory,
229 json = TJSONProtocolFactory,
230 }
231 local prot_factory = protocols[opt.protocol]:new{}
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200232 assert(prot_factory, 'Failed to create binary protocol factory')
233
234 -- Simple Server
235 server = TSimpleServer:new{
236 processor = processor,
237 serverTransport = socket,
238 transportFactory = trans_factory,
239 protocolFactory = prot_factory
240 }
241 assert(server, 'Failed to create server')
longzhiri03715892020-08-04 22:01:09 +0800242 server:setExceptionHandler(function (err) error(err) end)
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200243
244 -- Serve
245 server:serve()
246 server = nil
247end
248
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900249testBasicServer(arg)
250teardown()