| 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') | 
| Wang Yaofu | e432c6b | 2016-03-09 16:39:03 +0800 | [diff] [blame] | 22 | require('THttpTransport') | 
| Nobuaki Sukegawa | ebd71ce | 2016-02-04 21:28:22 +0900 | [diff] [blame] | 23 | require('TCompactProtocol') | 
| Wang Yaofu | 19a3a27 | 2016-02-14 18:15:45 +0800 | [diff] [blame] | 24 | require('TJsonProtocol') | 
| Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 25 | require('TBinaryProtocol') | 
|  | 26 | require('ThriftTest_ThriftTest') | 
|  | 27 | require('liblualongnumber') | 
|  | 28 |  | 
|  | 29 | local client | 
|  | 30 |  | 
|  | 31 | function teardown() | 
|  | 32 | if client then | 
| Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 33 | -- close the connection | 
|  | 34 | client:close() | 
|  | 35 | end | 
|  | 36 | end | 
|  | 37 |  | 
| Nobuaki Sukegawa | d094e79 | 2016-02-01 21:47:49 +0900 | [diff] [blame] | 38 | function 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 | 
|  | 52 | end | 
|  | 53 |  | 
| Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 54 | function assertEqual(val1, val2, msg) | 
|  | 55 | assert(val1 == val2, msg) | 
|  | 56 | end | 
|  | 57 |  | 
| Nobuaki Sukegawa | d094e79 | 2016-02-01 21:47:49 +0900 | [diff] [blame] | 58 | function testBasicClient(rawArgs) | 
|  | 59 | local opt = parseArgs(rawArgs) | 
| Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 60 | local socket = TSocket:new{ | 
| Nobuaki Sukegawa | d094e79 | 2016-02-01 21:47:49 +0900 | [diff] [blame] | 61 | port = tonumber(opt.port) | 
| Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 62 | } | 
|  | 63 | assert(socket, 'Failed to create client socket') | 
|  | 64 | socket:setTimeout(5000) | 
|  | 65 |  | 
| Nobuaki Sukegawa | d094e79 | 2016-02-01 21:47:49 +0900 | [diff] [blame] | 66 | local transports = { | 
|  | 67 | buffered = TBufferedTransport, | 
|  | 68 | framed = TFramedTransport, | 
| Wang Yaofu | e432c6b | 2016-03-09 16:39:03 +0800 | [diff] [blame] | 69 | http = THttpTransport, | 
| Nobuaki Sukegawa | d094e79 | 2016-02-01 21:47:49 +0900 | [diff] [blame] | 70 | } | 
|  | 71 | assert(transports[opt.transport] ~= nil) | 
|  | 72 | local transport = transports[opt.transport]:new{ | 
| Wang Yaofu | e432c6b | 2016-03-09 16:39:03 +0800 | [diff] [blame] | 73 | trans = socket, | 
|  | 74 | isServer = false | 
| Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 75 | } | 
| Nobuaki Sukegawa | d094e79 | 2016-02-01 21:47:49 +0900 | [diff] [blame] | 76 |  | 
|  | 77 | local protocols = { | 
|  | 78 | binary = TBinaryProtocol, | 
| Nobuaki Sukegawa | ebd71ce | 2016-02-04 21:28:22 +0900 | [diff] [blame] | 79 | compact = TCompactProtocol, | 
| Wang Yaofu | 19a3a27 | 2016-02-14 18:15:45 +0800 | [diff] [blame] | 80 | json = TJSONProtocol, | 
| Nobuaki Sukegawa | d094e79 | 2016-02-01 21:47:49 +0900 | [diff] [blame] | 81 | } | 
|  | 82 | assert(protocols[opt.protocol] ~= nil) | 
|  | 83 | local protocol = protocols[opt.protocol]:new{ | 
|  | 84 | trans = transport | 
|  | 85 | } | 
| Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 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 |  | 
| Phongphan Phuttha | 3b89cc5 | 2016-02-04 14:23:27 +0700 | [diff] [blame] | 93 | -- Open the transport | 
|  | 94 | local status, _ = pcall(transport.open, transport) | 
| Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 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 |  | 
| Nobuaki Sukegawa | d094e79 | 2016-02-01 21:47:49 +0900 | [diff] [blame] | 101 | -- Bool | 
|  | 102 | assertEqual(client:testBool(true), true, 'Failed testBool true') | 
| Nobuaki Sukegawa | 1f647f0 | 2016-02-04 21:18:40 +0900 | [diff] [blame] | 103 | assertEqual(client:testBool(false), false, 'Failed testBool false') | 
| Nobuaki Sukegawa | d094e79 | 2016-02-01 21:47:49 +0900 | [diff] [blame] | 104 |  | 
| Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 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 |  | 
| Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 155 | -- TODO testBinary() ... | 
|  | 156 |  | 
| Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 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 | 
| Nobuaki Sukegawa | 08d67d7 | 2016-02-19 00:49:29 +0900 | [diff] [blame] | 163 | local o = Xtruct:new{ | 
| Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 164 | string_thing = 'Zero', | 
|  | 165 | byte_thing = 1, | 
|  | 166 | i32_thing = -3, | 
|  | 167 | i64_thing = long(-5) | 
|  | 168 | } | 
| Nobuaki Sukegawa | 08d67d7 | 2016-02-19 00:49:29 +0900 | [diff] [blame] | 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') | 
| Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 174 |  | 
| Nobuaki Sukegawa | d094e79 | 2016-02-01 21:47:49 +0900 | [diff] [blame] | 175 | -- TODO add list map set exception etc etc | 
| Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 176 | end | 
|  | 177 |  | 
| Nobuaki Sukegawa | d094e79 | 2016-02-01 21:47:49 +0900 | [diff] [blame] | 178 | testBasicClient(arg) | 
|  | 179 | teardown() |