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. 17 18 19require('TSocket') 20require('TBufferedTransport') 21require('TFramedTransport') 22require('THttpTransport') 23require('TCompactProtocol') 24require('TJsonProtocol') 25require('TBinaryProtocol') 26require('ThriftTest_ThriftTest') 27require('liblualongnumber') 28 29local client 30 31function teardown() 32 if client then 33 -- close the connection 34 client:close() 35 end 36end 37 38function parseArgs(rawArgs) 39 local opt = { 40 protocol='binary', 41 transport='buffered', 42 port='9090', 43 } 44 for i, str in pairs(rawArgs) do 45 if i > 0 then 46 k, v = string.match(str, '--(%w+)=(%w+)') 47 assert(opt[k] ~= nil, 'Unknown argument') 48 opt[k] = v 49 end 50 end 51 return opt 52end 53 54function assertEqual(val1, val2, msg) 55 assert(val1 == val2, msg) 56end 57 58function testBasicClient(rawArgs) 59 local opt = parseArgs(rawArgs) 60 local socket = TSocket:new{ 61 port = tonumber(opt.port) 62 } 63 assert(socket, 'Failed to create client socket') 64 socket:setTimeout(5000) 65 66 local transports = { 67 buffered = TBufferedTransport, 68 framed = TFramedTransport, 69 http = THttpTransport, 70 } 71 assert(transports[opt.transport] ~= nil) 72 local transport = transports[opt.transport]:new{ 73 trans = socket, 74 isServer = false 75 } 76 77 local protocols = { 78 binary = TBinaryProtocol, 79 compact = TCompactProtocol, 80 json = TJSONProtocol, 81 } 82 assert(protocols[opt.protocol] ~= nil) 83 local protocol = protocols[opt.protocol]:new{ 84 trans = transport 85 } 86 assert(protocol, 'Failed to create binary protocol') 87 88 client = ThriftTestClient:new{ 89 protocol = protocol 90 } 91 assert(client, 'Failed to create client') 92 93 -- Open the transport 94 local status, _ = pcall(transport.open, transport) 95 assert(status, 'Failed to connect to server') 96 97 -- String 98 assertEqual(client:testString('lala'), 'lala', 'Failed testString') 99 assertEqual(client:testString('wahoo'), 'wahoo', 'Failed testString') 100 101 -- Bool 102 assertEqual(client:testBool(true), true, 'Failed testBool true') 103 assertEqual(client:testBool(false), false, 'Failed testBool false') 104 105 -- Byte 106 assertEqual(client:testByte(0x01), 1, 'Failed testByte 1') 107 assertEqual(client:testByte(0x40), 64, 'Failed testByte 2') 108 assertEqual(client:testByte(0x7f), 127, 'Failed testByte 3') 109 assertEqual(client:testByte(0x80), -128, 'Failed testByte 4') 110 assertEqual(client:testByte(0xbf), -65, 'Failed testByte 5') 111 assertEqual(client:testByte(0xff), -1, 'Failed testByte 6') 112 assertEqual(client:testByte(128), -128, 'Failed testByte 7') 113 assertEqual(client:testByte(255), -1, 'Failed testByte 8') 114 115 -- I32 116 assertEqual(client:testI32(0x00000001), 1, 'Failed testI32 1') 117 assertEqual(client:testI32(0x40000000), 1073741824, 'Failed testI32 2') 118 assertEqual(client:testI32(0x7fffffff), 2147483647, 'Failed testI32 3') 119 assertEqual(client:testI32(0x80000000), -2147483648, 'Failed testI32 4') 120 assertEqual(client:testI32(0xbfffffff), -1073741825, 'Failed testI32 5') 121 assertEqual(client:testI32(0xffffffff), -1, 'Failed testI32 6') 122 assertEqual(client:testI32(2147483648), -2147483648, 'Failed testI32 7') 123 assertEqual(client:testI32(4294967295), -1, 'Failed testI32 8') 124 125 -- I64 (lua only supports 16 decimal precision so larger numbers are 126 -- initialized by their string value) 127 local long = liblualongnumber.new 128 assertEqual(client:testI64(long(0x0000000000000001)), 129 long(1), 130 'Failed testI64 1') 131 assertEqual(client:testI64(long(0x4000000000000000)), 132 long(4611686018427387904), 133 'Failed testI64 2') 134 assertEqual(client:testI64(long('0x7fffffffffffffff')), 135 long('9223372036854775807'), 136 'Failed testI64 3') 137 assertEqual(client:testI64(long(0x8000000000000000)), 138 long(-9223372036854775808), 139 'Failed testI64 4') 140 assertEqual(client:testI64(long('0xbfffffffffffffff')), 141 long('-4611686018427387905'), 142 'Failed testI64 5') 143 assertEqual(client:testI64(long('0xffffffffffffffff')), 144 long(-1), 145 'Failed testI64 6') 146 147 -- Double 148 assertEqual( 149 client:testDouble(1.23456789), 1.23456789, 'Failed testDouble 1') 150 assertEqual( 151 client:testDouble(0.123456789), 0.123456789, 'Failed testDouble 2') 152 assertEqual( 153 client:testDouble(0.123456789), 0.123456789, 'Failed testDouble 3') 154 155 -- TODO testBinary() ... 156 157 -- Accuracy of 16 decimal digits (rounds) 158 local a, b = 1.12345678906666663, 1.12345678906666661 159 assertEqual(a, b) 160 assertEqual(client:testDouble(a), b, 'Failed testDouble 5') 161 162 -- Struct 163 local o = Xtruct:new{ 164 string_thing = 'Zero', 165 byte_thing = 1, 166 i32_thing = -3, 167 i64_thing = long(-5) 168 } 169 local r = client:testStruct(o) 170 assertEqual(o.string_thing, r.string_thing, 'Failed testStruct 1') 171 assertEqual(o.byte_thing, r.byte_thing, 'Failed testStruct 2') 172 assertEqual(o.i32_thing, r.i32_thing, 'Failed testStruct 3') 173 assertEqual(o.i64_thing, r.i64_thing, 'Failed testStruct 4') 174 175 -- TODO add list map set exception etc etc 176end 177 178testBasicClient(arg) 179teardown() 180