blob: fea426d9e6db46fa3d26069f64e019c3c51ce6d7 [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')
Nobuaki Sukegawaebd71ce2016-02-04 21:28:22 +090022require('TCompactProtocol')
Wang Yaofu19a3a272016-02-14 18:15:45 +080023require('TJsonProtocol')
Roger Meier6cf0ffc2014-04-05 00:45:42 +020024require('TBinaryProtocol')
25require('ThriftTest_ThriftTest')
26require('liblualongnumber')
27
28local client
29
30function teardown()
31 if client then
Roger Meier6cf0ffc2014-04-05 00:45:42 +020032 -- close the connection
33 client:close()
34 end
35end
36
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090037function 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
51end
52
Roger Meier6cf0ffc2014-04-05 00:45:42 +020053function assertEqual(val1, val2, msg)
54 assert(val1 == val2, msg)
55end
56
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090057function testBasicClient(rawArgs)
58 local opt = parseArgs(rawArgs)
Roger Meier6cf0ffc2014-04-05 00:45:42 +020059 local socket = TSocket:new{
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090060 port = tonumber(opt.port)
Roger Meier6cf0ffc2014-04-05 00:45:42 +020061 }
62 assert(socket, 'Failed to create client socket')
63 socket:setTimeout(5000)
64
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090065 local transports = {
66 buffered = TBufferedTransport,
67 framed = TFramedTransport,
68 }
69 assert(transports[opt.transport] ~= nil)
70 local transport = transports[opt.transport]:new{
Roger Meier6cf0ffc2014-04-05 00:45:42 +020071 trans = socket
72 }
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090073
74 local protocols = {
75 binary = TBinaryProtocol,
Nobuaki Sukegawaebd71ce2016-02-04 21:28:22 +090076 compact = TCompactProtocol,
Wang Yaofu19a3a272016-02-14 18:15:45 +080077 json = TJSONProtocol,
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090078 }
79 assert(protocols[opt.protocol] ~= nil)
80 local protocol = protocols[opt.protocol]:new{
81 trans = transport
82 }
Roger Meier6cf0ffc2014-04-05 00:45:42 +020083 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 Phuttha3b89cc52016-02-04 14:23:27 +070090 -- Open the transport
91 local status, _ = pcall(transport.open, transport)
Roger Meier6cf0ffc2014-04-05 00:45:42 +020092 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 Sukegawad094e792016-02-01 21:47:49 +090098 -- Bool
99 assertEqual(client:testBool(true), true, 'Failed testBool true')
Nobuaki Sukegawa1f647f02016-02-04 21:18:40 +0900100 assertEqual(client:testBool(false), false, 'Failed testBool false')
Nobuaki Sukegawad094e792016-02-01 21:47:49 +0900101
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200102 -- 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 Geyer8bcfdd92014-12-14 03:14:26 +0100152 -- TODO testBinary() ...
153
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200154 -- 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 Sukegawa08d67d72016-02-19 00:49:29 +0900160 local o = Xtruct:new{
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200161 string_thing = 'Zero',
162 byte_thing = 1,
163 i32_thing = -3,
164 i64_thing = long(-5)
165 }
Nobuaki Sukegawa08d67d72016-02-19 00:49:29 +0900166 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 Meier6cf0ffc2014-04-05 00:45:42 +0200171
Nobuaki Sukegawad094e792016-02-01 21:47:49 +0900172 -- TODO add list map set exception etc etc
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200173end
174
Nobuaki Sukegawad094e792016-02-01 21:47:49 +0900175testBasicClient(arg)
176teardown()