blob: 20ac407c8dadeb44e29d51cd9a71c09631f50d5d [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
longzhiri03715892020-08-04 22:01:09 +080069function TestHandler:testOneway(secondsToSleep)
70 print("testOneway secondsToSleep:", secondsToSleep)
71end
72
Roger Meier6cf0ffc2014-04-05 00:45:42 +020073--------------------------------------------------------------------------------
74-- Test
75local server
76
77function teardown()
78 if server then
79 server:close()
80 end
81end
82
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +090083function parseArgs(rawArgs)
84 local opt = {
85 protocol='binary',
86 transport='buffered',
87 port='9090',
88 }
89 for i, str in pairs(rawArgs) do
90 if i > 0 then
91 k, v = string.match(str, '--(%w+)=(%w+)')
92 assert(opt[k] ~= nil, 'Unknown argument')
93 opt[k] = v
94 end
95 end
96 return opt
97end
98
99function testBasicServer(rawArgs)
100 local opt = parseArgs(rawArgs)
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200101 -- Handler & Processor
102 local handler = TestHandler:new{}
103 assert(handler, 'Failed to create handler')
104 local processor = ThriftTestProcessor:new{
105 handler = handler
106 }
107 assert(processor, 'Failed to create processor')
108
109 -- Server Socket
110 local socket = TServerSocket:new{
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900111 port = opt.port
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200112 }
113 assert(socket, 'Failed to create server socket')
114
115 -- Transport & Factory
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900116 local transports = {
117 buffered = TBufferedTransportFactory,
118 framed = TFramedTransportFactory,
Wang Yaofue432c6b2016-03-09 16:39:03 +0800119 http = THttpTransportFactory,
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900120 }
121 assert(transports[opt.transport], 'Failed to create framed transport factory')
122 local trans_factory = transports[opt.transport]:new{}
123 local protocols = {
124 binary = TBinaryProtocolFactory,
125 compact = TCompactProtocolFactory,
126 json = TJSONProtocolFactory,
127 }
128 local prot_factory = protocols[opt.protocol]:new{}
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200129 assert(prot_factory, 'Failed to create binary protocol factory')
130
131 -- Simple Server
132 server = TSimpleServer:new{
133 processor = processor,
134 serverTransport = socket,
135 transportFactory = trans_factory,
136 protocolFactory = prot_factory
137 }
138 assert(server, 'Failed to create server')
longzhiri03715892020-08-04 22:01:09 +0800139 server:setExceptionHandler(function (err) error(err) end)
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200140
141 -- Serve
142 server:serve()
143 server = nil
144end
145
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900146testBasicServer(arg)
147teardown()