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