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