blob: a258b64508ee9239f1c4e1ee8fe46b718dbd9d45 [file] [log] [blame]
Jake Farrellb95b0ff2012-03-22 21:49:10 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19module thrift_test_client;
20
21import std.conv;
22import std.datetime;
23import std.exception : enforce;
24import std.getopt;
25import std.stdio;
26import std.string;
27import std.traits;
28import thrift.codegen.client;
29import thrift.protocol.base;
30import thrift.protocol.binary;
31import thrift.protocol.compact;
32import thrift.protocol.json;
33import thrift.transport.base;
34import thrift.transport.buffered;
35import thrift.transport.framed;
36import thrift.transport.http;
37import thrift.transport.socket;
38import thrift.transport.ssl;
39import thrift.util.hashset;
40
41import thrift_test_common;
42import thrift.test.ThriftTest;
43import thrift.test.ThriftTest_types;
44
45enum TransportType {
46 buffered,
47 framed,
48 http,
49 raw
50}
51
52TProtocol createProtocol(T)(T trans, ProtocolType type) {
53 final switch (type) {
54 case ProtocolType.binary:
55 return tBinaryProtocol(trans);
56 case ProtocolType.compact:
57 return tCompactProtocol(trans);
58 case ProtocolType.json:
59 return tJsonProtocol(trans);
60 }
61}
62
63void main(string[] args) {
64 string host = "localhost";
65 ushort port = 9090;
66 uint numTests = 1;
67 bool ssl;
68 ProtocolType protocolType;
69 TransportType transportType;
70 bool trace;
71
72 getopt(args,
73 "numTests|n", &numTests,
74 "protocol", &protocolType,
75 "ssl", &ssl,
76 "transport", &transportType,
77 "trace", &trace,
78 "host", (string _, string value) {
79 auto parts = split(value, ":");
80 if (parts.length > 1) {
81 // IPv6 addresses can contain colons, so take the last part for the
82 // port.
83 host = join(parts[0 .. $ - 1], ":");
84 port = to!ushort(parts[$ - 1]);
85 } else {
86 host = value;
87 }
88 }
89 );
90
91 TSocket socket;
92 if (ssl) {
93 auto sslContext = new TSSLContext();
94 sslContext.ciphers = "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH";
95 sslContext.authenticate = true;
96 sslContext.loadTrustedCertificates("./trusted-ca-certificate.pem");
97 socket = new TSSLSocket(sslContext, host, port);
98 } else {
99 socket = new TSocket(host, port);
100 }
101
102 TProtocol protocol;
103 final switch (transportType) {
104 case TransportType.buffered:
105 protocol = createProtocol(new TBufferedTransport(socket), protocolType);
106 break;
107 case TransportType.framed:
108 protocol = createProtocol(new TFramedTransport(socket), protocolType);
109 break;
110 case TransportType.http:
111 protocol = createProtocol(
112 new TClientHttpTransport(socket, host, "/service"), protocolType);
113 break;
114 case TransportType.raw:
115 protocol = createProtocol(socket, protocolType);
116 break;
117 }
118
119 auto client = tClient!ThriftTest(protocol);
120
121 ulong time_min;
122 ulong time_max;
123 ulong time_tot;
124
125 StopWatch sw;
126 foreach(test; 0 .. numTests) {
127 sw.start();
128
129 protocol.transport.open();
130
131 if (trace) writefln("Test #%s, connect %s:%s", test + 1, host, port);
132
133 if (trace) write("testVoid()");
134 client.testVoid();
135 if (trace) writeln(" = void");
136
137 if (trace) write("testString(\"Test\")");
138 string s = client.testString("Test");
139 if (trace) writefln(" = \"%s\"", s);
140 enforce(s == "Test");
141
142 if (trace) write("testByte(1)");
143 byte u8 = client.testByte(1);
144 if (trace) writefln(" = %s", u8);
145 enforce(u8 == 1);
146
147 if (trace) write("testI32(-1)");
148 int i32 = client.testI32(-1);
149 if (trace) writefln(" = %s", i32);
150 enforce(i32 == -1);
151
152 if (trace) write("testI64(-34359738368)");
153 long i64 = client.testI64(-34359738368L);
154 if (trace) writefln(" = %s", i64);
155 enforce(i64 == -34359738368L);
156
157 if (trace) write("testDouble(-5.2098523)");
158 double dub = client.testDouble(-5.2098523);
159 if (trace) writefln(" = %s", dub);
160 enforce(dub == -5.2098523);
161
162 Xtruct out1;
163 out1.string_thing = "Zero";
164 out1.byte_thing = 1;
165 out1.i32_thing = -3;
166 out1.i64_thing = -5;
167 if (trace) writef("testStruct(%s)", out1);
168 auto in1 = client.testStruct(out1);
169 if (trace) writefln(" = %s", in1);
170 enforce(in1 == out1);
171
172 if (trace) write("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
173 Xtruct2 out2;
174 out2.byte_thing = 1;
175 out2.struct_thing = out1;
176 out2.i32_thing = 5;
177 auto in2 = client.testNest(out2);
178 in1 = in2.struct_thing;
179 if (trace) writefln(" = {%s, {\"%s\", %s, %s, %s}, %s}", in2.byte_thing,
180 in1.string_thing, in1.byte_thing, in1.i32_thing, in1.i64_thing,
181 in2.i32_thing);
182 enforce(in2 == out2);
183
184 int[int] mapout;
185 for (int i = 0; i < 5; ++i) {
186 mapout[i] = i - 10;
187 }
188 if (trace) writef("testMap({%s})", mapout);
189 auto mapin = client.testMap(mapout);
190 if (trace) writefln(" = {%s}", mapin);
191 enforce(mapin == mapout);
192
193 auto setout = new HashSet!int;
194 for (int i = -2; i < 3; ++i) {
195 setout ~= i;
196 }
197 if (trace) writef("testSet(%s)", setout);
198 auto setin = client.testSet(setout);
199 if (trace) writefln(" = %s", setin);
200 enforce(setin == setout);
201
202 int[] listout;
203 for (int i = -2; i < 3; ++i) {
204 listout ~= i;
205 }
206 if (trace) writef("testList(%s)", listout);
207 auto listin = client.testList(listout);
208 if (trace) writefln(" = %s", listin);
209 enforce(listin == listout);
210
211 {
212 if (trace) write("testEnum(ONE)");
213 auto ret = client.testEnum(Numberz.ONE);
214 if (trace) writefln(" = %s", ret);
215 enforce(ret == Numberz.ONE);
216
217 if (trace) write("testEnum(TWO)");
218 ret = client.testEnum(Numberz.TWO);
219 if (trace) writefln(" = %s", ret);
220 enforce(ret == Numberz.TWO);
221
222 if (trace) write("testEnum(THREE)");
223 ret = client.testEnum(Numberz.THREE);
224 if (trace) writefln(" = %s", ret);
225 enforce(ret == Numberz.THREE);
226
227 if (trace) write("testEnum(FIVE)");
228 ret = client.testEnum(Numberz.FIVE);
229 if (trace) writefln(" = %s", ret);
230 enforce(ret == Numberz.FIVE);
231
232 if (trace) write("testEnum(EIGHT)");
233 ret = client.testEnum(Numberz.EIGHT);
234 if (trace) writefln(" = %s", ret);
235 enforce(ret == Numberz.EIGHT);
236 }
237
238 if (trace) write("testTypedef(309858235082523)");
239 UserId uid = client.testTypedef(309858235082523L);
240 if (trace) writefln(" = %s", uid);
241 enforce(uid == 309858235082523L);
242
243 if (trace) write("testMapMap(1)");
244 auto mm = client.testMapMap(1);
245 if (trace) writefln(" = {%s}", mm);
246 // Simply doing == doesn't seem to work for nested AAs.
247 foreach (key, value; mm) {
248 enforce(testMapMapReturn[key] == value);
249 }
250 foreach (key, value; testMapMapReturn) {
251 enforce(mm[key] == value);
252 }
253
254 Insanity insane;
255 insane.userMap[Numberz.FIVE] = 5000;
256 Xtruct truck;
257 truck.string_thing = "Truck";
258 truck.byte_thing = 8;
259 truck.i32_thing = 8;
260 truck.i64_thing = 8;
261 insane.xtructs ~= truck;
262 if (trace) write("testInsanity()");
263 auto whoa = client.testInsanity(insane);
264 if (trace) writefln(" = %s", whoa);
265
266 // Commented for now, this is cumbersome to write without opEqual getting
267 // called on AA comparison.
268 // enforce(whoa == testInsanityReturn);
269
270 {
271 try {
272 if (trace) write("client.testException(\"Xception\") =>");
273 client.testException("Xception");
274 if (trace) writeln(" void\nFAILURE");
275 throw new Exception("testException failed.");
276 } catch (Xception e) {
277 if (trace) writefln(" {%s, \"%s\"}", e.errorCode, e.message);
278 }
279
280 try {
281 if (trace) write("client.testException(\"success\") =>");
282 client.testException("success");
283 if (trace) writeln(" void");
284 } catch (Exception e) {
285 if (trace) writeln(" exception\nFAILURE");
286 throw new Exception("testException failed.");
287 }
288 }
289
290 {
291 try {
292 if (trace) write("client.testMultiException(\"Xception\", \"test 1\") =>");
293 auto result = client.testMultiException("Xception", "test 1");
294 if (trace) writeln(" result\nFAILURE");
295 throw new Exception("testMultiException failed.");
296 } catch (Xception e) {
297 if (trace) writefln(" {%s, \"%s\"}", e.errorCode, e.message);
298 }
299
300 try {
301 if (trace) write("client.testMultiException(\"Xception2\", \"test 2\") =>");
302 auto result = client.testMultiException("Xception2", "test 2");
303 if (trace) writeln(" result\nFAILURE");
304 throw new Exception("testMultiException failed.");
305 } catch (Xception2 e) {
306 if (trace) writefln(" {%s, {\"%s\"}}",
307 e.errorCode, e.struct_thing.string_thing);
308 }
309
310 try {
311 if (trace) writef("client.testMultiException(\"success\", \"test 3\") =>");
312 auto result = client.testMultiException("success", "test 3");
313 if (trace) writefln(" {{\"%s\"}}", result.string_thing);
314 } catch (Exception e) {
315 if (trace) writeln(" exception\nFAILURE");
316 throw new Exception("testMultiException failed.");
317 }
318 }
319
320 // Do not run oneway test when doing multiple iterations, as it blocks the
321 // server for three seconds.
322 if (numTests == 1) {
323 if (trace) writef("client.testOneway(3) =>");
324 auto onewayWatch = StopWatch(AutoStart.yes);
325 client.testOneway(3);
326 onewayWatch.stop();
327 if (onewayWatch.peek().msecs > 200) {
328 if (trace) {
329 writefln(" FAILURE - took %s ms", onewayWatch.peek().usecs / 1000.0);
330 }
331 throw new Exception("testOneway failed.");
332 } else {
333 if (trace) {
334 writefln(" success - took %s ms", onewayWatch.peek().usecs / 1000.0);
335 }
336 }
337
338 // Redo a simple test after the oneway to make sure we aren't "off by
339 // one", which would be the case if the server treated oneway methods
340 // like normal ones.
341 if (trace) write("re-test testI32(-1)");
342 i32 = client.testI32(-1);
343 if (trace) writefln(" = %s", i32);
344 }
345
346 // Time metering.
347 sw.stop();
348
349 immutable tot = sw.peek().usecs;
350 if (trace) writefln("Total time: %s us\n", tot);
351
352 time_tot += tot;
353 if (time_min == 0 || tot < time_min) {
354 time_min = tot;
355 }
356 if (tot > time_max) {
357 time_max = tot;
358 }
359 protocol.transport.close();
360
361 sw.reset();
362 }
363
364 writeln("All tests done.");
365
366 if (numTests > 1) {
367 auto time_avg = time_tot / numTests;
368 writefln("Min time: %s us", time_min);
369 writefln("Max time: %s us", time_max);
370 writefln("Avg time: %s us", time_avg);
371 }
372}