blob: 4e85c4722477ebf46afded3963a9e160878eb782 [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{}).
30parse_args([], Opts) -> Opts;
31parse_args([Head | Rest], Opts) ->
32 NewOpts =
33 case catch list_to_integer(Head) of
34 Port when is_integer(Port) ->
35 Opts#options{port = Port};
36 _Else ->
37 case Head of
38 "framed" ->
39 Opts#options{client_opts = [{framed, true} | Opts#options.client_opts]};
40 "" ->
41 Opts;
42 _Else ->
43 erlang:error({bad_arg, Head})
44 end
45 end,
46 parse_args(Rest, NewOpts).
47
48
49start() -> start([]).
50start(Args) ->
51 #options{port = Port, client_opts = ClientOpts} = parse_args(Args),
David Reiss2c8d2282010-08-30 22:05:39 +000052 {ok, Client0} = thrift_client_util:new(
Jens Geyer6d15c302014-10-02 10:03:09 +020053 "127.0.0.1", Port, thrift_test_thrift, ClientOpts),
David Reiss2c8d2282010-08-30 22:05:39 +000054
Jens Geyer6d15c302014-10-02 10:03:09 +020055 DemoXtruct = #'Xtruct'{
David Reiss2c8d2282010-08-30 22:05:39 +000056 string_thing = <<"Zero">>,
57 byte_thing = 1,
58 i32_thing = 9128361,
59 i64_thing = 9223372036854775807},
60
Jens Geyer6d15c302014-10-02 10:03:09 +020061 DemoNest = #'Xtruct2'{
David Reiss2c8d2282010-08-30 22:05:39 +000062 byte_thing = 7,
63 struct_thing = DemoXtruct,
64 % Note that we don't set i32_thing, it will come back as undefined
65 % from the Python server, but 0 from the C++ server, since it is not
66 % optional
67 i32_thing = 2},
68
69 % Is it safe to match these things?
70 DemoDict = dict:from_list([ {Key, Key-10} || Key <- lists:seq(0,10) ]),
71 DemoSet = sets:from_list([ Key || Key <- lists:seq(-3,3) ]),
72
73 %DemoInsane = #insanity{
74 % userMap = dict:from_list([{?thriftTest_FIVE, 5000}]),
75 % xtructs = [#xtruct{ string_thing = <<"Truck">>, byte_thing = 8, i32_thing = 8, i64_thing = 8}]},
76
77 {Client01, {ok, ok}} = thrift_client:call(Client0, testVoid, []),
78
79 {Client02, {ok, <<"Test">>}} = thrift_client:call(Client01, testString, ["Test"]),
80 {Client03, {ok, <<"Test">>}} = thrift_client:call(Client02, testString, [<<"Test">>]),
81 {Client04, {ok, 63}} = thrift_client:call(Client03, testByte, [63]),
82 {Client05, {ok, -1}} = thrift_client:call(Client04, testI32, [-1]),
83 {Client06, {ok, 0}} = thrift_client:call(Client05, testI32, [0]),
84 {Client07, {ok, -34359738368}} = thrift_client:call(Client06, testI64, [-34359738368]),
85 {Client08, {ok, -5.2098523}} = thrift_client:call(Client07, testDouble, [-5.2098523]),
Jens Geyer8bcfdd92014-12-14 03:14:26 +010086 %% TODO: add testBinary() call
David Reiss2c8d2282010-08-30 22:05:39 +000087 {Client09, {ok, DemoXtruct}} = thrift_client:call(Client08, testStruct, [DemoXtruct]),
88 {Client10, {ok, DemoNest}} = thrift_client:call(Client09, testNest, [DemoNest]),
89 {Client11, {ok, DemoDict}} = thrift_client:call(Client10, testMap, [DemoDict]),
90 {Client12, {ok, DemoSet}} = thrift_client:call(Client11, testSet, [DemoSet]),
91 {Client13, {ok, [-1,2,3]}} = thrift_client:call(Client12, testList, [[-1,2,3]]),
Jens Geyer6d15c302014-10-02 10:03:09 +020092 {Client14, {ok, 1}} = thrift_client:call(Client13, testEnum, [?THRIFT_TEST_NUMBERZ_ONE]),
David Reiss2c8d2282010-08-30 22:05:39 +000093 {Client15, {ok, 309858235082523}} = thrift_client:call(Client14, testTypedef, [309858235082523]),
94
95 % No python implementation, but works with C++ and Erlang.
96 %{Client16, {ok, InsaneResult}} = thrift_client:call(Client15, testInsanity, [DemoInsane]),
97 %io:format("~p~n", [InsaneResult]),
98 Client16 = Client15,
99
Jens Geyer6d15c302014-10-02 10:03:09 +0200100 {Client17, {ok, #'Xtruct'{string_thing = <<"Message">>}}} =
David Reiss2c8d2282010-08-30 22:05:39 +0000101 thrift_client:call(Client16, testMultiException, ["Safe", "Message"]),
102
103 Client18 =
104 try
105 {ClientS1, Result1} = thrift_client:call(Client17, testMultiException, ["Xception", "Message"]),
106 io:format("Unexpected return! ~p~n", [Result1]),
107 ClientS1
108 catch
Jens Geyer6d15c302014-10-02 10:03:09 +0200109 throw:{ClientS2, {exception, ExnS1 = #'Xception'{}}} ->
110 #'Xception'{errorCode = 1001, message = <<"This is an Xception">>} = ExnS1,
David Reiss2c8d2282010-08-30 22:05:39 +0000111 ClientS2;
Jens Geyer6d15c302014-10-02 10:03:09 +0200112 throw:{ClientS2, {exception, _ExnS1 = #'Xception2'{}}} ->
David Reiss2c8d2282010-08-30 22:05:39 +0000113 io:format("Wrong exception type!~n", []),
114 ClientS2
115 end,
116
117 Client19 =
118 try
119 {ClientS3, Result2} = thrift_client:call(Client18, testMultiException, ["Xception2", "Message"]),
120 io:format("Unexpected return! ~p~n", [Result2]),
121 ClientS3
122 catch
Jens Geyer6d15c302014-10-02 10:03:09 +0200123 throw:{ClientS4, {exception, _ExnS2 = #'Xception'{}}} ->
David Reiss2c8d2282010-08-30 22:05:39 +0000124 io:format("Wrong exception type!~n", []),
125 ClientS4;
Jens Geyer6d15c302014-10-02 10:03:09 +0200126 throw:{ClientS4, {exception, ExnS2 = #'Xception2'{}}} ->
127 #'Xception2'{errorCode = 2002,
128 struct_thing = #'Xtruct'{
David Reiss2c8d2282010-08-30 22:05:39 +0000129 string_thing = <<"This is an Xception2">>}} = ExnS2,
130 ClientS4
131 end,
132
133 thrift_client:close(Client19).