blob: ab032bb19b2781a8f8d2500ec2d56b1572bac2d4 [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
24-include("thriftTest_types.hrl").
25
26start() -> start(["9090"]).
27start([PortStr]) ->
28 Port = list_to_integer(PortStr),
29 {ok, Client0} = thrift_client_util:new(
30 "127.0.0.1", Port, thriftTest_thrift, []),
31
32 DemoXtruct = #xtruct{
33 string_thing = <<"Zero">>,
34 byte_thing = 1,
35 i32_thing = 9128361,
36 i64_thing = 9223372036854775807},
37
38 DemoNest = #xtruct2{
39 byte_thing = 7,
40 struct_thing = DemoXtruct,
41 % Note that we don't set i32_thing, it will come back as undefined
42 % from the Python server, but 0 from the C++ server, since it is not
43 % optional
44 i32_thing = 2},
45
46 % Is it safe to match these things?
47 DemoDict = dict:from_list([ {Key, Key-10} || Key <- lists:seq(0,10) ]),
48 DemoSet = sets:from_list([ Key || Key <- lists:seq(-3,3) ]),
49
50 %DemoInsane = #insanity{
51 % userMap = dict:from_list([{?thriftTest_FIVE, 5000}]),
52 % xtructs = [#xtruct{ string_thing = <<"Truck">>, byte_thing = 8, i32_thing = 8, i64_thing = 8}]},
53
54 {Client01, {ok, ok}} = thrift_client:call(Client0, testVoid, []),
55
56 {Client02, {ok, <<"Test">>}} = thrift_client:call(Client01, testString, ["Test"]),
57 {Client03, {ok, <<"Test">>}} = thrift_client:call(Client02, testString, [<<"Test">>]),
58 {Client04, {ok, 63}} = thrift_client:call(Client03, testByte, [63]),
59 {Client05, {ok, -1}} = thrift_client:call(Client04, testI32, [-1]),
60 {Client06, {ok, 0}} = thrift_client:call(Client05, testI32, [0]),
61 {Client07, {ok, -34359738368}} = thrift_client:call(Client06, testI64, [-34359738368]),
62 {Client08, {ok, -5.2098523}} = thrift_client:call(Client07, testDouble, [-5.2098523]),
63 {Client09, {ok, DemoXtruct}} = thrift_client:call(Client08, testStruct, [DemoXtruct]),
64 {Client10, {ok, DemoNest}} = thrift_client:call(Client09, testNest, [DemoNest]),
65 {Client11, {ok, DemoDict}} = thrift_client:call(Client10, testMap, [DemoDict]),
66 {Client12, {ok, DemoSet}} = thrift_client:call(Client11, testSet, [DemoSet]),
67 {Client13, {ok, [-1,2,3]}} = thrift_client:call(Client12, testList, [[-1,2,3]]),
68 {Client14, {ok, 1}} = thrift_client:call(Client13, testEnum, [?thriftTest_ONE]),
69 {Client15, {ok, 309858235082523}} = thrift_client:call(Client14, testTypedef, [309858235082523]),
70
71 % No python implementation, but works with C++ and Erlang.
72 %{Client16, {ok, InsaneResult}} = thrift_client:call(Client15, testInsanity, [DemoInsane]),
73 %io:format("~p~n", [InsaneResult]),
74 Client16 = Client15,
75
76 {Client17, {ok, #xtruct{string_thing = <<"Message">>}}} =
77 thrift_client:call(Client16, testMultiException, ["Safe", "Message"]),
78
79 Client18 =
80 try
81 {ClientS1, Result1} = thrift_client:call(Client17, testMultiException, ["Xception", "Message"]),
82 io:format("Unexpected return! ~p~n", [Result1]),
83 ClientS1
84 catch
85 throw:{ClientS2, {exception, ExnS1 = #xception{}}} ->
86 #xception{errorCode = 1001, message = <<"This is an Xception">>} = ExnS1,
87 ClientS2;
88 throw:{ClientS2, {exception, _ExnS1 = #xception2{}}} ->
89 io:format("Wrong exception type!~n", []),
90 ClientS2
91 end,
92
93 Client19 =
94 try
95 {ClientS3, Result2} = thrift_client:call(Client18, testMultiException, ["Xception2", "Message"]),
96 io:format("Unexpected return! ~p~n", [Result2]),
97 ClientS3
98 catch
99 throw:{ClientS4, {exception, _ExnS2 = #xception{}}} ->
100 io:format("Wrong exception type!~n", []),
101 ClientS4;
102 throw:{ClientS4, {exception, ExnS2 = #xception2{}}} ->
103 #xception2{errorCode = 2002,
104 struct_thing = #xtruct{
105 string_thing = <<"This is an Xception2">>}} = ExnS2,
106 ClientS4
107 end,
108
109 thrift_client:close(Client19).