blob: f97bc8aaa3fb02aa99c7e83c2da0af42d237807c [file] [log] [blame]
David Reiss2c8d2282010-08-30 22:05:39 +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%%
19
20-module(test_client).
21
22-export([start/0, start/1]).
23
Jens Geyer6d15c302014-10-02 10:03:09 +020024-include("gen-erl/thrift_test_types.hrl").
David Reiss2c8d2282010-08-30 22:05:39 +000025
David Reissa508b332010-08-30 22:05:41 +000026-record(options, {port = 9090,
27 client_opts = []}).
28
29parse_args(Args) -> parse_args(Args, #options{}).
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090030parse_args([], Opts) ->
31 Opts;
David Reissa508b332010-08-30 22:05:41 +000032parse_args([Head | Rest], Opts) ->
33 NewOpts =
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090034 case Head of
35 "--port=" ++ Port ->
36 case string:to_integer(Port) of
37 {IntPort,_} when is_integer(IntPort) ->
38 Opts#options{port = IntPort};
39 _Else ->
40 erlang:error({bad_arg, Head})
41 end;
42 "--transport=" ++ Trans ->
43 % TODO: Enable Buffered and HTTP transport
44 case Trans of
David Reissa508b332010-08-30 22:05:41 +000045 "framed" ->
46 Opts#options{client_opts = [{framed, true} | Opts#options.client_opts]};
David Reissa508b332010-08-30 22:05:41 +000047 _Else ->
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090048 Opts
49 end;
David Robakowskia7d6a972013-08-07 05:51:00 +020050 "--ssl" ->
51 ssl:start(),
52 SslOptions =
53 {ssloptions, [
54 {certfile, "../keys/client.crt"}
55 ,{keyfile, "../keys/server.key"}
56 ]},
57 Opts#options{client_opts = [{ssltransport, true} | [SslOptions | Opts#options.client_opts]]};
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +090058 "--protocol=" ++ Proto ->
59 Opts#options{client_opts = [{protocol, list_to_atom(Proto)}]};
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090060 _Else ->
61 erlang:error({bad_arg, Head})
David Reissa508b332010-08-30 22:05:41 +000062 end,
63 parse_args(Rest, NewOpts).
64
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090065start() -> start(init:get_plain_arguments()).
David Reissa508b332010-08-30 22:05:41 +000066start(Args) ->
67 #options{port = Port, client_opts = ClientOpts} = parse_args(Args),
David Reiss2c8d2282010-08-30 22:05:39 +000068 {ok, Client0} = thrift_client_util:new(
Jens Geyer6d15c302014-10-02 10:03:09 +020069 "127.0.0.1", Port, thrift_test_thrift, ClientOpts),
David Reiss2c8d2282010-08-30 22:05:39 +000070
Jens Geyer6d15c302014-10-02 10:03:09 +020071 DemoXtruct = #'Xtruct'{
David Reiss2c8d2282010-08-30 22:05:39 +000072 string_thing = <<"Zero">>,
73 byte_thing = 1,
74 i32_thing = 9128361,
75 i64_thing = 9223372036854775807},
76
Jens Geyer6d15c302014-10-02 10:03:09 +020077 DemoNest = #'Xtruct2'{
David Reiss2c8d2282010-08-30 22:05:39 +000078 byte_thing = 7,
79 struct_thing = DemoXtruct,
80 % Note that we don't set i32_thing, it will come back as undefined
81 % from the Python server, but 0 from the C++ server, since it is not
82 % optional
83 i32_thing = 2},
84
85 % Is it safe to match these things?
86 DemoDict = dict:from_list([ {Key, Key-10} || Key <- lists:seq(0,10) ]),
87 DemoSet = sets:from_list([ Key || Key <- lists:seq(-3,3) ]),
88
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +090089 DemoInsane = #'Insanity'{
90 userMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_FIVE, 5000}]),
91 xtructs = [#'Xtruct'{ string_thing = <<"Truck">>, byte_thing = 8, i32_thing = 8, i64_thing = 8}]},
David Reiss2c8d2282010-08-30 22:05:39 +000092
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +090093 error_logger:info_msg("testVoid"),
David Reiss2c8d2282010-08-30 22:05:39 +000094 {Client01, {ok, ok}} = thrift_client:call(Client0, testVoid, []),
95
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +090096 error_logger:info_msg("testString"),
David Reiss2c8d2282010-08-30 22:05:39 +000097 {Client02, {ok, <<"Test">>}} = thrift_client:call(Client01, testString, ["Test"]),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +090098 error_logger:info_msg("testString"),
David Reiss2c8d2282010-08-30 22:05:39 +000099 {Client03, {ok, <<"Test">>}} = thrift_client:call(Client02, testString, [<<"Test">>]),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900100 error_logger:info_msg("testByte"),
David Reiss2c8d2282010-08-30 22:05:39 +0000101 {Client04, {ok, 63}} = thrift_client:call(Client03, testByte, [63]),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900102 error_logger:info_msg("testI32"),
David Reiss2c8d2282010-08-30 22:05:39 +0000103 {Client05, {ok, -1}} = thrift_client:call(Client04, testI32, [-1]),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900104 error_logger:info_msg("testI32"),
David Reiss2c8d2282010-08-30 22:05:39 +0000105 {Client06, {ok, 0}} = thrift_client:call(Client05, testI32, [0]),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900106 error_logger:info_msg("testI64"),
David Reiss2c8d2282010-08-30 22:05:39 +0000107 {Client07, {ok, -34359738368}} = thrift_client:call(Client06, testI64, [-34359738368]),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900108 error_logger:info_msg("testDouble"),
David Reiss2c8d2282010-08-30 22:05:39 +0000109 {Client08, {ok, -5.2098523}} = thrift_client:call(Client07, testDouble, [-5.2098523]),
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100110 %% TODO: add testBinary() call
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900111 error_logger:info_msg("testStruct"),
David Reiss2c8d2282010-08-30 22:05:39 +0000112 {Client09, {ok, DemoXtruct}} = thrift_client:call(Client08, testStruct, [DemoXtruct]),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900113 error_logger:info_msg("testNest"),
David Reiss2c8d2282010-08-30 22:05:39 +0000114 {Client10, {ok, DemoNest}} = thrift_client:call(Client09, testNest, [DemoNest]),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900115 error_logger:info_msg("testMap"),
David Reiss2c8d2282010-08-30 22:05:39 +0000116 {Client11, {ok, DemoDict}} = thrift_client:call(Client10, testMap, [DemoDict]),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900117 error_logger:info_msg("testSet"),
David Reiss2c8d2282010-08-30 22:05:39 +0000118 {Client12, {ok, DemoSet}} = thrift_client:call(Client11, testSet, [DemoSet]),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900119 error_logger:info_msg("testList"),
David Reiss2c8d2282010-08-30 22:05:39 +0000120 {Client13, {ok, [-1,2,3]}} = thrift_client:call(Client12, testList, [[-1,2,3]]),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900121 error_logger:info_msg("testEnum"),
Jens Geyer6d15c302014-10-02 10:03:09 +0200122 {Client14, {ok, 1}} = thrift_client:call(Client13, testEnum, [?THRIFT_TEST_NUMBERZ_ONE]),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900123 error_logger:info_msg("testTypedef"),
David Reiss2c8d2282010-08-30 22:05:39 +0000124 {Client15, {ok, 309858235082523}} = thrift_client:call(Client14, testTypedef, [309858235082523]),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900125 error_logger:info_msg("testInsanity"),
Nobuaki Sukegawa826ea992015-10-28 22:19:45 +0900126 {Client16, {ok, InsaneResult}} = thrift_client:call(Client15, testInsanity, [DemoInsane]),
127 io:format("~p~n", [InsaneResult]),
David Reiss2c8d2282010-08-30 22:05:39 +0000128
Jens Geyer6d15c302014-10-02 10:03:09 +0200129 {Client17, {ok, #'Xtruct'{string_thing = <<"Message">>}}} =
David Reiss2c8d2282010-08-30 22:05:39 +0000130 thrift_client:call(Client16, testMultiException, ["Safe", "Message"]),
131
132 Client18 =
133 try
134 {ClientS1, Result1} = thrift_client:call(Client17, testMultiException, ["Xception", "Message"]),
135 io:format("Unexpected return! ~p~n", [Result1]),
136 ClientS1
137 catch
Jens Geyer6d15c302014-10-02 10:03:09 +0200138 throw:{ClientS2, {exception, ExnS1 = #'Xception'{}}} ->
139 #'Xception'{errorCode = 1001, message = <<"This is an Xception">>} = ExnS1,
David Reiss2c8d2282010-08-30 22:05:39 +0000140 ClientS2;
Jens Geyer6d15c302014-10-02 10:03:09 +0200141 throw:{ClientS2, {exception, _ExnS1 = #'Xception2'{}}} ->
David Reiss2c8d2282010-08-30 22:05:39 +0000142 io:format("Wrong exception type!~n", []),
143 ClientS2
144 end,
145
146 Client19 =
147 try
148 {ClientS3, Result2} = thrift_client:call(Client18, testMultiException, ["Xception2", "Message"]),
149 io:format("Unexpected return! ~p~n", [Result2]),
150 ClientS3
151 catch
Jens Geyer6d15c302014-10-02 10:03:09 +0200152 throw:{ClientS4, {exception, _ExnS2 = #'Xception'{}}} ->
David Reiss2c8d2282010-08-30 22:05:39 +0000153 io:format("Wrong exception type!~n", []),
154 ClientS4;
Jens Geyer6d15c302014-10-02 10:03:09 +0200155 throw:{ClientS4, {exception, ExnS2 = #'Xception2'{}}} ->
156 #'Xception2'{errorCode = 2002,
157 struct_thing = #'Xtruct'{
David Reiss2c8d2282010-08-30 22:05:39 +0000158 string_thing = <<"This is an Xception2">>}} = ExnS2,
159 ClientS4
160 end,
161
Nobuaki Sukegawa7ab56e82015-10-31 12:17:46 +0900162 %% Use deprecated erlang:now until we start requiring OTP18
163 %% Started = erlang:monotonic_time(milli_seconds),
164 {_, StartSec, StartUSec} = erlang:now(),
Nobuaki Sukegawab31f0902015-11-01 17:00:34 +0900165 error_logger:info_msg("testOneway"),
alisdair sullivan7bdba5c2014-09-30 22:03:34 -0700166 {Client20, {ok, ok}} = thrift_client:call(Client19, testOneway, [1]),
Nobuaki Sukegawa7ab56e82015-10-31 12:17:46 +0900167 {_, EndSec, EndUSec} = erlang:now(),
168 Elapsed = (EndSec - StartSec) * 1000 + (EndUSec - StartUSec) / 1000,
169 if
170 Elapsed > 1000 -> exit(1);
171 true -> true
172 end,
alisdair sullivan7bdba5c2014-09-30 22:03:34 -0700173
174 thrift_client:close(Client20).