blob: db4d2d14f2c1be355303a0ba197cc3671ed24fa7 [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]),
86 {Client09, {ok, DemoXtruct}} = thrift_client:call(Client08, testStruct, [DemoXtruct]),
87 {Client10, {ok, DemoNest}} = thrift_client:call(Client09, testNest, [DemoNest]),
88 {Client11, {ok, DemoDict}} = thrift_client:call(Client10, testMap, [DemoDict]),
89 {Client12, {ok, DemoSet}} = thrift_client:call(Client11, testSet, [DemoSet]),
90 {Client13, {ok, [-1,2,3]}} = thrift_client:call(Client12, testList, [[-1,2,3]]),
Jens Geyer6d15c302014-10-02 10:03:09 +020091 {Client14, {ok, 1}} = thrift_client:call(Client13, testEnum, [?THRIFT_TEST_NUMBERZ_ONE]),
David Reiss2c8d2282010-08-30 22:05:39 +000092 {Client15, {ok, 309858235082523}} = thrift_client:call(Client14, testTypedef, [309858235082523]),
93
94 % No python implementation, but works with C++ and Erlang.
95 %{Client16, {ok, InsaneResult}} = thrift_client:call(Client15, testInsanity, [DemoInsane]),
96 %io:format("~p~n", [InsaneResult]),
97 Client16 = Client15,
98
Jens Geyer6d15c302014-10-02 10:03:09 +020099 {Client17, {ok, #'Xtruct'{string_thing = <<"Message">>}}} =
David Reiss2c8d2282010-08-30 22:05:39 +0000100 thrift_client:call(Client16, testMultiException, ["Safe", "Message"]),
101
102 Client18 =
103 try
104 {ClientS1, Result1} = thrift_client:call(Client17, testMultiException, ["Xception", "Message"]),
105 io:format("Unexpected return! ~p~n", [Result1]),
106 ClientS1
107 catch
Jens Geyer6d15c302014-10-02 10:03:09 +0200108 throw:{ClientS2, {exception, ExnS1 = #'Xception'{}}} ->
109 #'Xception'{errorCode = 1001, message = <<"This is an Xception">>} = ExnS1,
David Reiss2c8d2282010-08-30 22:05:39 +0000110 ClientS2;
Jens Geyer6d15c302014-10-02 10:03:09 +0200111 throw:{ClientS2, {exception, _ExnS1 = #'Xception2'{}}} ->
David Reiss2c8d2282010-08-30 22:05:39 +0000112 io:format("Wrong exception type!~n", []),
113 ClientS2
114 end,
115
116 Client19 =
117 try
118 {ClientS3, Result2} = thrift_client:call(Client18, testMultiException, ["Xception2", "Message"]),
119 io:format("Unexpected return! ~p~n", [Result2]),
120 ClientS3
121 catch
Jens Geyer6d15c302014-10-02 10:03:09 +0200122 throw:{ClientS4, {exception, _ExnS2 = #'Xception'{}}} ->
David Reiss2c8d2282010-08-30 22:05:39 +0000123 io:format("Wrong exception type!~n", []),
124 ClientS4;
Jens Geyer6d15c302014-10-02 10:03:09 +0200125 throw:{ClientS4, {exception, ExnS2 = #'Xception2'{}}} ->
126 #'Xception2'{errorCode = 2002,
127 struct_thing = #'Xtruct'{
David Reiss2c8d2282010-08-30 22:05:39 +0000128 string_thing = <<"This is an Xception2">>}} = ExnS2,
129 ClientS4
130 end,
131
132 thrift_client:close(Client19).