blob: 77d8d078aa4f4929aed0bff24b774ef9484deb05 [file] [log] [blame]
Nobuaki Sukegawaebd71ce2016-02-04 21:28:22 +09001-- 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 Meier6cf0ffc2014-04-05 00:45:42 +020017
18
19require('TSocket')
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090020require('TBufferedTransport')
21require('TFramedTransport')
Wang Yaofue432c6b2016-03-09 16:39:03 +080022require('THttpTransport')
Nobuaki Sukegawaebd71ce2016-02-04 21:28:22 +090023require('TCompactProtocol')
Wang Yaofu19a3a272016-02-14 18:15:45 +080024require('TJsonProtocol')
Roger Meier6cf0ffc2014-04-05 00:45:42 +020025require('TBinaryProtocol')
26require('ThriftTest_ThriftTest')
27require('liblualongnumber')
28
29local client
30
31function teardown()
32 if client then
Roger Meier6cf0ffc2014-04-05 00:45:42 +020033 -- close the connection
34 client:close()
35 end
36end
37
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090038function 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
Roger Meier6cf0ffc2014-04-05 00:45:42 +020054function assertEqual(val1, val2, msg)
55 assert(val1 == val2, msg)
56end
57
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090058function testBasicClient(rawArgs)
59 local opt = parseArgs(rawArgs)
Roger Meier6cf0ffc2014-04-05 00:45:42 +020060 local socket = TSocket:new{
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090061 port = tonumber(opt.port)
Roger Meier6cf0ffc2014-04-05 00:45:42 +020062 }
63 assert(socket, 'Failed to create client socket')
64 socket:setTimeout(5000)
65
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090066 local transports = {
67 buffered = TBufferedTransport,
68 framed = TFramedTransport,
Wang Yaofue432c6b2016-03-09 16:39:03 +080069 http = THttpTransport,
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090070 }
71 assert(transports[opt.transport] ~= nil)
72 local transport = transports[opt.transport]:new{
Wang Yaofue432c6b2016-03-09 16:39:03 +080073 trans = socket,
74 isServer = false
Roger Meier6cf0ffc2014-04-05 00:45:42 +020075 }
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090076
77 local protocols = {
78 binary = TBinaryProtocol,
Nobuaki Sukegawaebd71ce2016-02-04 21:28:22 +090079 compact = TCompactProtocol,
Wang Yaofu19a3a272016-02-14 18:15:45 +080080 json = TJSONProtocol,
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090081 }
82 assert(protocols[opt.protocol] ~= nil)
83 local protocol = protocols[opt.protocol]:new{
84 trans = transport
85 }
Roger Meier6cf0ffc2014-04-05 00:45:42 +020086 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 Phuttha3b89cc52016-02-04 14:23:27 +070093 -- Open the transport
94 local status, _ = pcall(transport.open, transport)
Roger Meier6cf0ffc2014-04-05 00:45:42 +020095 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 Sukegawad094e792016-02-01 21:47:49 +0900101 -- Bool
102 assertEqual(client:testBool(true), true, 'Failed testBool true')
Nobuaki Sukegawa1f647f02016-02-04 21:18:40 +0900103 assertEqual(client:testBool(false), false, 'Failed testBool false')
Nobuaki Sukegawad094e792016-02-01 21:47:49 +0900104
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200105 -- 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 Geyer8bcfdd92014-12-14 03:14:26 +0100155 -- TODO testBinary() ...
156
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200157 -- 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 Sukegawa08d67d72016-02-19 00:49:29 +0900163 local o = Xtruct:new{
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200164 string_thing = 'Zero',
165 byte_thing = 1,
166 i32_thing = -3,
167 i64_thing = long(-5)
168 }
Nobuaki Sukegawa08d67d72016-02-19 00:49:29 +0900169 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 Meier6cf0ffc2014-04-05 00:45:42 +0200174
Nobuaki Sukegawad094e792016-02-01 21:47:49 +0900175 -- TODO add list map set exception etc etc
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200176end
177
Nobuaki Sukegawad094e792016-02-01 21:47:49 +0900178testBasicClient(arg)
179teardown()