blob: b89f25dd3bb8b22c340feb3ff7ad8986bbef210a [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')
Roger Meier6cf0ffc2014-04-05 00:45:42 +020023require('TBinaryProtocol')
24require('ThriftTest_ThriftTest')
25require('liblualongnumber')
26
27local client
28
29function teardown()
30 if client then
Roger Meier6cf0ffc2014-04-05 00:45:42 +020031 -- close the connection
32 client:close()
33 end
34end
35
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090036function 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
50end
51
Roger Meier6cf0ffc2014-04-05 00:45:42 +020052function assertEqual(val1, val2, msg)
53 assert(val1 == val2, msg)
54end
55
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090056function testBasicClient(rawArgs)
57 local opt = parseArgs(rawArgs)
Roger Meier6cf0ffc2014-04-05 00:45:42 +020058 local socket = TSocket:new{
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090059 port = tonumber(opt.port)
Roger Meier6cf0ffc2014-04-05 00:45:42 +020060 }
61 assert(socket, 'Failed to create client socket')
62 socket:setTimeout(5000)
63
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090064 local transports = {
65 buffered = TBufferedTransport,
66 framed = TFramedTransport,
67 }
68 assert(transports[opt.transport] ~= nil)
69 local transport = transports[opt.transport]:new{
Roger Meier6cf0ffc2014-04-05 00:45:42 +020070 trans = socket
71 }
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090072
73 local protocols = {
74 binary = TBinaryProtocol,
Nobuaki Sukegawaebd71ce2016-02-04 21:28:22 +090075 compact = TCompactProtocol,
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090076 }
77 assert(protocols[opt.protocol] ~= nil)
78 local protocol = protocols[opt.protocol]:new{
79 trans = transport
80 }
Roger Meier6cf0ffc2014-04-05 00:45:42 +020081 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 Phuttha3b89cc52016-02-04 14:23:27 +070088 -- Open the transport
89 local status, _ = pcall(transport.open, transport)
Roger Meier6cf0ffc2014-04-05 00:45:42 +020090 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 Sukegawad094e792016-02-01 21:47:49 +090096 -- Bool
97 assertEqual(client:testBool(true), true, 'Failed testBool true')
Nobuaki Sukegawa1f647f02016-02-04 21:18:40 +090098 assertEqual(client:testBool(false), false, 'Failed testBool false')
Nobuaki Sukegawad094e792016-02-01 21:47:49 +090099
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200100 -- 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 Geyer8bcfdd92014-12-14 03:14:26 +0100150 -- TODO testBinary() ...
151
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200152 -- 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 Sukegawad094e792016-02-01 21:47:49 +0900168 -- TODO add list map set exception etc etc
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200169end
170
Nobuaki Sukegawad094e792016-02-01 21:47:49 +0900171testBasicClient(arg)
172teardown()