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