| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 1 | %% | 
|  | 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 Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 24 | -include("gen-erl/thrift_test_types.hrl"). | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 25 |  | 
| David Reiss | a508b33 | 2010-08-30 22:05:41 +0000 | [diff] [blame] | 26 | -record(options, {port = 9090, | 
|  | 27 | client_opts = []}). | 
|  | 28 |  | 
|  | 29 | parse_args(Args) -> parse_args(Args, #options{}). | 
| Nobuaki Sukegawa | 826ea99 | 2015-10-28 22:19:45 +0900 | [diff] [blame] | 30 | parse_args([], Opts) -> | 
|  | 31 | Opts; | 
| David Reiss | a508b33 | 2010-08-30 22:05:41 +0000 | [diff] [blame] | 32 | parse_args([Head | Rest], Opts) -> | 
|  | 33 | NewOpts = | 
| Nobuaki Sukegawa | 826ea99 | 2015-10-28 22:19:45 +0900 | [diff] [blame] | 34 | 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 Reiss | a508b33 | 2010-08-30 22:05:41 +0000 | [diff] [blame] | 45 | "framed" -> | 
|  | 46 | Opts#options{client_opts = [{framed, true} | Opts#options.client_opts]}; | 
| David Reiss | a508b33 | 2010-08-30 22:05:41 +0000 | [diff] [blame] | 47 | _Else -> | 
| Nobuaki Sukegawa | 826ea99 | 2015-10-28 22:19:45 +0900 | [diff] [blame] | 48 | Opts | 
|  | 49 | end; | 
| David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 50 | "--ssl" -> | 
|  | 51 | ssl:start(), | 
|  | 52 | SslOptions = | 
|  | 53 | {ssloptions, [ | 
| James E. King, III | 377719c | 2017-02-15 14:33:20 -0500 | [diff] [blame] | 54 | {cacertfile, "../keys/CA.pem"}, | 
|  | 55 | {certfile, "../keys/client.pem"}, | 
|  | 56 | {keyfile, "../keys/client.key"} | 
| David Robakowski | a7d6a97 | 2013-08-07 05:51:00 +0200 | [diff] [blame] | 57 | ]}, | 
|  | 58 | Opts#options{client_opts = [{ssltransport, true} | [SslOptions | Opts#options.client_opts]]}; | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 59 | "--protocol=" ++ Proto -> | 
|  | 60 | Opts#options{client_opts = [{protocol, list_to_atom(Proto)}]}; | 
| Nobuaki Sukegawa | 826ea99 | 2015-10-28 22:19:45 +0900 | [diff] [blame] | 61 | _Else -> | 
|  | 62 | erlang:error({bad_arg, Head}) | 
| David Reiss | a508b33 | 2010-08-30 22:05:41 +0000 | [diff] [blame] | 63 | end, | 
|  | 64 | parse_args(Rest, NewOpts). | 
|  | 65 |  | 
| Nobuaki Sukegawa | 826ea99 | 2015-10-28 22:19:45 +0900 | [diff] [blame] | 66 | start() -> start(init:get_plain_arguments()). | 
| David Reiss | a508b33 | 2010-08-30 22:05:41 +0000 | [diff] [blame] | 67 | start(Args) -> | 
|  | 68 | #options{port = Port, client_opts = ClientOpts} = parse_args(Args), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 69 | {ok, Client0} = thrift_client_util:new( | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 70 | "127.0.0.1", Port, thrift_test_thrift, ClientOpts), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 71 |  | 
| Steve Cohen | 7ea4a87 | 2016-06-14 00:32:48 +0200 | [diff] [blame] | 72 | DemoXtruct = #'thrift.test.Xtruct'{ | 
|  | 73 | string_thing = <<"Zero">>, | 
|  | 74 | byte_thing = 1, | 
|  | 75 | i32_thing = 9128361, | 
|  | 76 | i64_thing = 9223372036854775807}, | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 77 |  | 
| Steve Cohen | 7ea4a87 | 2016-06-14 00:32:48 +0200 | [diff] [blame] | 78 | DemoNest = #'thrift.test.Xtruct2'{ | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 79 | byte_thing = 7, | 
|  | 80 | struct_thing = DemoXtruct, | 
|  | 81 | % Note that we don't set i32_thing, it will come back as undefined | 
|  | 82 | % from the Python server, but 0 from the C++ server, since it is not | 
|  | 83 | % optional | 
|  | 84 | i32_thing = 2}, | 
|  | 85 |  | 
|  | 86 | % Is it safe to match these things? | 
|  | 87 | DemoDict = dict:from_list([ {Key, Key-10} || Key <- lists:seq(0,10) ]), | 
|  | 88 | DemoSet = sets:from_list([ Key || Key <- lists:seq(-3,3) ]), | 
|  | 89 |  | 
| Steve Cohen | 7ea4a87 | 2016-06-14 00:32:48 +0200 | [diff] [blame] | 90 | DemoInsane = #'thrift.test.Insanity'{ | 
| Nobuaki Sukegawa | 826ea99 | 2015-10-28 22:19:45 +0900 | [diff] [blame] | 91 | userMap = dict:from_list([{?THRIFT_TEST_NUMBERZ_FIVE, 5000}]), | 
| Steve Cohen | 7ea4a87 | 2016-06-14 00:32:48 +0200 | [diff] [blame] | 92 | xtructs = [#'thrift.test.Xtruct'{ string_thing = <<"Truck">>, byte_thing = 8, i32_thing = 8, i64_thing = 8}]}, | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 93 |  | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 94 | error_logger:info_msg("testVoid"), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 95 | {Client01, {ok, ok}} = thrift_client:call(Client0, testVoid, []), | 
|  | 96 |  | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 97 | error_logger:info_msg("testString"), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 98 | {Client02, {ok, <<"Test">>}}      = thrift_client:call(Client01, testString, ["Test"]), | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 99 | error_logger:info_msg("testString"), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 100 | {Client03, {ok, <<"Test">>}}      = thrift_client:call(Client02, testString, [<<"Test">>]), | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 101 | error_logger:info_msg("testByte"), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 102 | {Client04, {ok, 63}}              = thrift_client:call(Client03, testByte, [63]), | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 103 | error_logger:info_msg("testI32"), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 104 | {Client05, {ok, -1}}              = thrift_client:call(Client04, testI32, [-1]), | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 105 | error_logger:info_msg("testI32"), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 106 | {Client06, {ok, 0}}               = thrift_client:call(Client05, testI32, [0]), | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 107 | error_logger:info_msg("testI64"), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 108 | {Client07, {ok, -34359738368}}    = thrift_client:call(Client06, testI64, [-34359738368]), | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 109 | error_logger:info_msg("testDouble"), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 110 | {Client08, {ok, -5.2098523}}      = thrift_client:call(Client07, testDouble, [-5.2098523]), | 
| Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 111 | %% TODO: add testBinary() call | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 112 | error_logger:info_msg("testStruct"), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 113 | {Client09, {ok, DemoXtruct}}      = thrift_client:call(Client08, testStruct, [DemoXtruct]), | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 114 | error_logger:info_msg("testNest"), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 115 | {Client10, {ok, DemoNest}}        = thrift_client:call(Client09, testNest, [DemoNest]), | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 116 | error_logger:info_msg("testMap"), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 117 | {Client11, {ok, DemoDict}}        = thrift_client:call(Client10, testMap, [DemoDict]), | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 118 | error_logger:info_msg("testSet"), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 119 | {Client12, {ok, DemoSet}}         = thrift_client:call(Client11, testSet, [DemoSet]), | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 120 | error_logger:info_msg("testList"), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 121 | {Client13, {ok, [-1,2,3]}}        = thrift_client:call(Client12, testList, [[-1,2,3]]), | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 122 | error_logger:info_msg("testEnum"), | 
| Jens Geyer | 6d15c30 | 2014-10-02 10:03:09 +0200 | [diff] [blame] | 123 | {Client14, {ok, 1}}               = thrift_client:call(Client13, testEnum, [?THRIFT_TEST_NUMBERZ_ONE]), | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 124 | error_logger:info_msg("testTypedef"), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 125 | {Client15, {ok, 309858235082523}} = thrift_client:call(Client14, testTypedef, [309858235082523]), | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 126 | error_logger:info_msg("testInsanity"), | 
| Nobuaki Sukegawa | 826ea99 | 2015-10-28 22:19:45 +0900 | [diff] [blame] | 127 | {Client16, {ok, InsaneResult}}    = thrift_client:call(Client15, testInsanity, [DemoInsane]), | 
|  | 128 | io:format("~p~n", [InsaneResult]), | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 129 |  | 
| Steve Cohen | 7ea4a87 | 2016-06-14 00:32:48 +0200 | [diff] [blame] | 130 | {Client17, {ok, #'thrift.test.Xtruct'{string_thing = <<"Message">>}}} = | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 131 | thrift_client:call(Client16, testMultiException, ["Safe", "Message"]), | 
|  | 132 |  | 
|  | 133 | Client18 = | 
|  | 134 | try | 
|  | 135 | {ClientS1, Result1} = thrift_client:call(Client17, testMultiException, ["Xception", "Message"]), | 
|  | 136 | io:format("Unexpected return! ~p~n", [Result1]), | 
|  | 137 | ClientS1 | 
|  | 138 | catch | 
| Steve Cohen | 7ea4a87 | 2016-06-14 00:32:48 +0200 | [diff] [blame] | 139 | throw:{ClientS2, {exception, ExnS1 = #'thrift.test.Xception'{}}} -> | 
|  | 140 | #'thrift.test.Xception'{errorCode = 1001, message = <<"This is an Xception">>} = ExnS1, | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 141 | ClientS2; | 
| Steve Cohen | 7ea4a87 | 2016-06-14 00:32:48 +0200 | [diff] [blame] | 142 | throw:{ClientS2, {exception, _ExnS1 = #'thrift.test.Xception2'{}}} -> | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 143 | io:format("Wrong exception type!~n", []), | 
|  | 144 | ClientS2 | 
|  | 145 | end, | 
|  | 146 |  | 
|  | 147 | Client19 = | 
|  | 148 | try | 
|  | 149 | {ClientS3, Result2} = thrift_client:call(Client18, testMultiException, ["Xception2", "Message"]), | 
|  | 150 | io:format("Unexpected return! ~p~n", [Result2]), | 
|  | 151 | ClientS3 | 
|  | 152 | catch | 
| Steve Cohen | 7ea4a87 | 2016-06-14 00:32:48 +0200 | [diff] [blame] | 153 | throw:{ClientS4, {exception, _ExnS2 = #'thrift.test.Xception'{}}} -> | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 154 | io:format("Wrong exception type!~n", []), | 
|  | 155 | ClientS4; | 
| Steve Cohen | 7ea4a87 | 2016-06-14 00:32:48 +0200 | [diff] [blame] | 156 | throw:{ClientS4, {exception, ExnS2 = #'thrift.test.Xception2'{}}} -> | 
|  | 157 | #'thrift.test.Xception2'{errorCode = 2002, | 
|  | 158 | struct_thing = #'thrift.test.Xtruct'{ | 
| David Reiss | 2c8d228 | 2010-08-30 22:05:39 +0000 | [diff] [blame] | 159 | string_thing = <<"This is an Xception2">>}} = ExnS2, | 
|  | 160 | ClientS4 | 
|  | 161 | end, | 
|  | 162 |  | 
| Nobuaki Sukegawa | 7ab56e8 | 2015-10-31 12:17:46 +0900 | [diff] [blame] | 163 | %% Started = erlang:monotonic_time(milli_seconds), | 
| walter-weinmann | f8e62fb | 2017-09-07 06:14:28 +0200 | [diff] [blame] | 164 | {_, StartSec, StartUSec} = erlang:timestamp(), | 
| Nobuaki Sukegawa | b31f090 | 2015-11-01 17:00:34 +0900 | [diff] [blame] | 165 | error_logger:info_msg("testOneway"), | 
| alisdair sullivan | 7bdba5c | 2014-09-30 22:03:34 -0700 | [diff] [blame] | 166 | {Client20, {ok, ok}} = thrift_client:call(Client19, testOneway, [1]), | 
| walter-weinmann | f8e62fb | 2017-09-07 06:14:28 +0200 | [diff] [blame] | 167 | {_, EndSec, EndUSec} = erlang:timestamp(), | 
| Nobuaki Sukegawa | 7ab56e8 | 2015-10-31 12:17:46 +0900 | [diff] [blame] | 168 | Elapsed = (EndSec - StartSec) * 1000 + (EndUSec - StartUSec) / 1000, | 
|  | 169 | if | 
|  | 170 | Elapsed > 1000 -> exit(1); | 
|  | 171 | true -> true | 
|  | 172 | end, | 
| alisdair sullivan | 7bdba5c | 2014-09-30 22:03:34 -0700 | [diff] [blame] | 173 |  | 
|  | 174 | thrift_client:close(Client20). |