blob: a26467f0c27d9f178de932eddb43a407864f5d8b [file] [log] [blame]
Bryan Duxburyd3879f82010-08-19 05:06:02 +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
24-include("thriftTest_types.hrl").
25
26-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),
52 {ok, Client0} = thrift_client_util:new(
53 "127.0.0.1", Port, thriftTest_thrift, ClientOpts),
54
55 DemoXtruct = #xtruct{
56 string_thing = <<"Zero">>,
57 byte_thing = 1,
58 i32_thing = 9128361,
59 i64_thing = 9223372036854775807},
60
61 DemoNest = #xtruct2{
62 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]]),
91 {Client14, {ok, 1}} = thrift_client:call(Client13, testEnum, [?thriftTest_ONE]),
92 {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
99 {Client17, {ok, #xtruct{string_thing = <<"Message">>}}} =
100 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
108 throw:{ClientS2, {exception, ExnS1 = #xception{}}} ->
109 #xception{errorCode = 1001, message = <<"This is an Xception">>} = ExnS1,
110 ClientS2;
111 throw:{ClientS2, {exception, _ExnS1 = #xception2{}}} ->
112 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
122 throw:{ClientS4, {exception, _ExnS2 = #xception{}}} ->
123 io:format("Wrong exception type!~n", []),
124 ClientS4;
125 throw:{ClientS4, {exception, ExnS2 = #xception2{}}} ->
126 #xception2{errorCode = 2002,
127 struct_thing = #xtruct{
128 string_thing = <<"This is an Xception2">>}} = ExnS2,
129 ClientS4
130 end,
131
132 thrift_client:close(Client19).