Nobuaki Sukegawa | ebd71ce | 2016-02-04 21:28:22 +0900 | [diff] [blame] | 1 | -- 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 Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 17 | |
| 18 | require('ThriftTest_ThriftTest') |
| 19 | require('TSocket') |
Nobuaki Sukegawa | 23ffb31 | 2016-02-19 00:50:17 +0900 | [diff] [blame] | 20 | require('TBufferedTransport') |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 21 | require('TFramedTransport') |
Wang Yaofu | e432c6b | 2016-03-09 16:39:03 +0800 | [diff] [blame] | 22 | require('THttpTransport') |
Nobuaki Sukegawa | 23ffb31 | 2016-02-19 00:50:17 +0900 | [diff] [blame] | 23 | require('TCompactProtocol') |
| 24 | require('TJsonProtocol') |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 25 | require('TBinaryProtocol') |
| 26 | require('TServer') |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 27 | local liblualongnumber = require('liblualongnumber') |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 28 | |
| 29 | -------------------------------------------------------------------------------- |
| 30 | -- Handler |
| 31 | TestHandler = ThriftTestIface:new{} |
| 32 | |
| 33 | -- Stops the server |
| 34 | function TestHandler:testVoid() |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 35 | end |
| 36 | |
| 37 | function TestHandler:testString(str) |
| 38 | return str |
| 39 | end |
| 40 | |
Nobuaki Sukegawa | 23ffb31 | 2016-02-19 00:50:17 +0900 | [diff] [blame] | 41 | function TestHandler:testBool(bool) |
| 42 | return bool |
| 43 | end |
| 44 | |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 45 | function TestHandler:testByte(byte) |
| 46 | return byte |
| 47 | end |
| 48 | |
| 49 | function TestHandler:testI32(i32) |
| 50 | return i32 |
| 51 | end |
| 52 | |
| 53 | function TestHandler:testI64(i64) |
| 54 | return i64 |
| 55 | end |
| 56 | |
| 57 | function TestHandler:testDouble(d) |
| 58 | return d |
| 59 | end |
| 60 | |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 61 | function TestHandler:testBinary(by) |
| 62 | return by |
| 63 | end |
| 64 | |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 65 | function TestHandler:testUuid(uuid) |
| 66 | return uuid |
| 67 | end |
| 68 | |
| 69 | function TestHandler:testNest(thing) |
| 70 | return thing |
| 71 | end |
| 72 | |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 73 | function TestHandler:testStruct(thing) |
| 74 | return thing |
| 75 | end |
| 76 | |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 77 | function TestHandler:testMap(thing) |
| 78 | return thing |
| 79 | end |
| 80 | |
| 81 | function TestHandler:testStringMap(thing) |
| 82 | return thing |
| 83 | end |
| 84 | |
| 85 | function TestHandler:testSet(thing) |
| 86 | return thing |
| 87 | end |
| 88 | |
| 89 | function TestHandler:testList(thing) |
| 90 | return thing |
| 91 | end |
| 92 | |
| 93 | function TestHandler:testEnum(thing) |
| 94 | return thing |
| 95 | end |
| 96 | |
| 97 | function TestHandler:testTypedef(thing) |
| 98 | return thing |
| 99 | end |
| 100 | |
| 101 | function 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 | } |
| 116 | end |
| 117 | |
| 118 | function 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 | }; |
| 134 | end |
| 135 | |
| 136 | function TestHandler:testMulti(arg0, arg1, arg2, arg3, arg4, arg5) |
| 137 | return Xtruct:new {} |
| 138 | end |
| 139 | |
| 140 | function 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 |
| 149 | end |
| 150 | |
| 151 | function 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 | } |
| 170 | end |
| 171 | |
longzhiri | 0371589 | 2020-08-04 22:01:09 +0800 | [diff] [blame] | 172 | function TestHandler:testOneway(secondsToSleep) |
| 173 | print("testOneway secondsToSleep:", secondsToSleep) |
| 174 | end |
| 175 | |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 176 | -------------------------------------------------------------------------------- |
| 177 | -- Test |
| 178 | local server |
| 179 | |
| 180 | function teardown() |
| 181 | if server then |
| 182 | server:close() |
| 183 | end |
| 184 | end |
| 185 | |
Nobuaki Sukegawa | 23ffb31 | 2016-02-19 00:50:17 +0900 | [diff] [blame] | 186 | function 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 |
| 200 | end |
| 201 | |
| 202 | function testBasicServer(rawArgs) |
| 203 | local opt = parseArgs(rawArgs) |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 204 | -- 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 Sukegawa | 23ffb31 | 2016-02-19 00:50:17 +0900 | [diff] [blame] | 214 | port = opt.port |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 215 | } |
| 216 | assert(socket, 'Failed to create server socket') |
| 217 | |
| 218 | -- Transport & Factory |
Nobuaki Sukegawa | 23ffb31 | 2016-02-19 00:50:17 +0900 | [diff] [blame] | 219 | local transports = { |
| 220 | buffered = TBufferedTransportFactory, |
| 221 | framed = TFramedTransportFactory, |
Wang Yaofu | e432c6b | 2016-03-09 16:39:03 +0800 | [diff] [blame] | 222 | http = THttpTransportFactory, |
Nobuaki Sukegawa | 23ffb31 | 2016-02-19 00:50:17 +0900 | [diff] [blame] | 223 | } |
| 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 Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 232 | 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') |
longzhiri | 0371589 | 2020-08-04 22:01:09 +0800 | [diff] [blame] | 242 | server:setExceptionHandler(function (err) error(err) end) |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 243 | |
| 244 | -- Serve |
| 245 | server:serve() |
| 246 | server = nil |
| 247 | end |
| 248 | |
Nobuaki Sukegawa | 23ffb31 | 2016-02-19 00:50:17 +0900 | [diff] [blame] | 249 | testBasicServer(arg) |
| 250 | teardown() |