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') |
| 27 | require('liblualongnumber') |
| 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 | |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 65 | function TestHandler:testStruct(thing) |
| 66 | return thing |
| 67 | end |
| 68 | |
| 69 | -------------------------------------------------------------------------------- |
| 70 | -- Test |
| 71 | local server |
| 72 | |
| 73 | function teardown() |
| 74 | if server then |
| 75 | server:close() |
| 76 | end |
| 77 | end |
| 78 | |
Nobuaki Sukegawa | 23ffb31 | 2016-02-19 00:50:17 +0900 | [diff] [blame] | 79 | function 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 |
| 93 | end |
| 94 | |
| 95 | function testBasicServer(rawArgs) |
| 96 | local opt = parseArgs(rawArgs) |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 97 | -- 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 Sukegawa | 23ffb31 | 2016-02-19 00:50:17 +0900 | [diff] [blame] | 107 | port = opt.port |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 108 | } |
| 109 | assert(socket, 'Failed to create server socket') |
| 110 | |
| 111 | -- Transport & Factory |
Nobuaki Sukegawa | 23ffb31 | 2016-02-19 00:50:17 +0900 | [diff] [blame] | 112 | local transports = { |
| 113 | buffered = TBufferedTransportFactory, |
| 114 | framed = TFramedTransportFactory, |
Wang Yaofu | e432c6b | 2016-03-09 16:39:03 +0800 | [diff] [blame] | 115 | http = THttpTransportFactory, |
Nobuaki Sukegawa | 23ffb31 | 2016-02-19 00:50:17 +0900 | [diff] [blame] | 116 | } |
| 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 Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 125 | 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 |
| 139 | end |
| 140 | |
Nobuaki Sukegawa | 23ffb31 | 2016-02-19 00:50:17 +0900 | [diff] [blame] | 141 | testBasicServer(arg) |
| 142 | teardown() |