blob: 864b63d5da3855d35ec8564686dc17b999a6f123 [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')
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +090022require('TCompactProtocol')
23require('TJsonProtocol')
Roger Meier6cf0ffc2014-04-05 00:45:42 +020024require('TBinaryProtocol')
25require('TServer')
26require('liblualongnumber')
27
28--------------------------------------------------------------------------------
29-- Handler
30TestHandler = ThriftTestIface:new{}
31
32-- Stops the server
33function TestHandler:testVoid()
Roger Meier6cf0ffc2014-04-05 00:45:42 +020034end
35
36function TestHandler:testString(str)
37 return str
38end
39
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +090040function TestHandler:testBool(bool)
41 return bool
42end
43
Roger Meier6cf0ffc2014-04-05 00:45:42 +020044function TestHandler:testByte(byte)
45 return byte
46end
47
48function TestHandler:testI32(i32)
49 return i32
50end
51
52function TestHandler:testI64(i64)
53 return i64
54end
55
56function TestHandler:testDouble(d)
57 return d
58end
59
Jens Geyer8bcfdd92014-12-14 03:14:26 +010060function TestHandler:testBinary(by)
61 return by
62end
63
Roger Meier6cf0ffc2014-04-05 00:45:42 +020064function TestHandler:testStruct(thing)
65 return thing
66end
67
68--------------------------------------------------------------------------------
69-- Test
70local server
71
72function teardown()
73 if server then
74 server:close()
75 end
76end
77
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +090078function parseArgs(rawArgs)
79 local opt = {
80 protocol='binary',
81 transport='buffered',
82 port='9090',
83 }
84 for i, str in pairs(rawArgs) do
85 if i > 0 then
86 k, v = string.match(str, '--(%w+)=(%w+)')
87 assert(opt[k] ~= nil, 'Unknown argument')
88 opt[k] = v
89 end
90 end
91 return opt
92end
93
94function testBasicServer(rawArgs)
95 local opt = parseArgs(rawArgs)
Roger Meier6cf0ffc2014-04-05 00:45:42 +020096 -- Handler & Processor
97 local handler = TestHandler:new{}
98 assert(handler, 'Failed to create handler')
99 local processor = ThriftTestProcessor:new{
100 handler = handler
101 }
102 assert(processor, 'Failed to create processor')
103
104 -- Server Socket
105 local socket = TServerSocket:new{
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900106 port = opt.port
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200107 }
108 assert(socket, 'Failed to create server socket')
109
110 -- Transport & Factory
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900111 local transports = {
112 buffered = TBufferedTransportFactory,
113 framed = TFramedTransportFactory,
114 }
115 assert(transports[opt.transport], 'Failed to create framed transport factory')
116 local trans_factory = transports[opt.transport]:new{}
117 local protocols = {
118 binary = TBinaryProtocolFactory,
119 compact = TCompactProtocolFactory,
120 json = TJSONProtocolFactory,
121 }
122 local prot_factory = protocols[opt.protocol]:new{}
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200123 assert(prot_factory, 'Failed to create binary protocol factory')
124
125 -- Simple Server
126 server = TSimpleServer:new{
127 processor = processor,
128 serverTransport = socket,
129 transportFactory = trans_factory,
130 protocolFactory = prot_factory
131 }
132 assert(server, 'Failed to create server')
133
134 -- Serve
135 server:serve()
136 server = nil
137end
138
Nobuaki Sukegawa23ffb312016-02-19 00:50:17 +0900139testBasicServer(arg)
140teardown()