blob: acd2d79b82379a8469eea474aabde76205a5531e [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')
27require('liblualongnumber')
28
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
Roger Meier6cf0ffc2014-04-05 00:45:42 +020065function TestHandler:testStruct(thing)
66 return thing
67end
68
69--------------------------------------------------------------------------------
70-- Test
71local server
72
73function teardown()
74 if server then
75 server:close()
76 end
77end
78
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +090079function parseArgs(rawArgs)
80 local opt = {
81 protocol='binary',
82 transport='buffered',
83 port='9090',
84 }
85 for i, str in pairs(rawArgs) do
86 if i > 0 then
87 k, v = string.match(str, '--(%w+)=(%w+)')
88 assert(opt[k] ~= nil, 'Unknown argument')
89 opt[k] = v
90 end
91 end
92 return opt
93end
94
95function testBasicServer(rawArgs)
96 local opt = parseArgs(rawArgs)
Roger Meier6cf0ffc2014-04-05 00:45:42 +020097 -- Handler & Processor
98 local handler = TestHandler:new{}
99 assert(handler, 'Failed to create handler')
100 local processor = ThriftTestProcessor:new{
101 handler = handler
102 }
103 assert(processor, 'Failed to create processor')
104
105 -- Server Socket
106 local socket = TServerSocket:new{
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900107 port = opt.port
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200108 }
109 assert(socket, 'Failed to create server socket')
110
111 -- Transport & Factory
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900112 local transports = {
113 buffered = TBufferedTransportFactory,
114 framed = TFramedTransportFactory,
Wang Yaofue432c6b2016-03-09 16:39:03 +0800115 http = THttpTransportFactory,
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900116 }
117 assert(transports[opt.transport], 'Failed to create framed transport factory')
118 local trans_factory = transports[opt.transport]:new{}
119 local protocols = {
120 binary = TBinaryProtocolFactory,
121 compact = TCompactProtocolFactory,
122 json = TJSONProtocolFactory,
123 }
124 local prot_factory = protocols[opt.protocol]:new{}
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200125 assert(prot_factory, 'Failed to create binary protocol factory')
126
127 -- Simple Server
128 server = TSimpleServer:new{
129 processor = processor,
130 serverTransport = socket,
131 transportFactory = trans_factory,
132 protocolFactory = prot_factory
133 }
134 assert(server, 'Failed to create server')
135
136 -- Serve
137 server:serve()
138 server = nil
139end
140
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900141testBasicServer(arg)
142teardown()