Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to bypass encoding and pass pre-encoded data. #3

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/ox_thrift_protocol.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ encode_message (ServiceModule, Function, MessageType, SeqId, Args) ->
].


encode (_, {ox_thrift_pre_encoded, ?THRIFT_PROTOCOL, Binary}, Acc) ->
%% Collect pre-encoded binary as-is
[ Binary | Acc ];

encode (_, {ox_thrift_pre_encoded, Protocol, _}, _) ->
throw(lists:flatten(io_lib:format("Protocol mismatch in a pre-encoded value. '~w' is expected while '~w' was given.", [?THRIFT_PROTOCOL, Protocol])));

encode ({struct, StructDef}, Data, Acc)
when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 ->
%% Encode a record from a struct definition.
Expand Down
15 changes: 15 additions & 0 deletions test/ox_thrift_binary_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
%% Copyright 2016-2024, OpenX. All rights reserved.
%% Licensed under the conditions specified in the accompanying LICENSE file.

-module(ox_thrift_binary_tests).

-define(STATS_TABLE, ox_thrift_stats_binary).
-define(GLOBALS_TABLE, ox_thrift_globals_binary).

-define(PROTOCOL, ox_thrift_protocol_binary).
-include("ox_thrift_tests.hrl").

echo (#'AllTypes'{byte_field = 43}) ->
{ox_thrift_pre_encoded, binary, <<2,0,1,1,3,0,2,43,6,0,6,127,14,8,0,5,255,255,255,133,10,0,4,1,35,69,103,138,188,222,240,4,0,3,64,36,64,0,0,0,0,0,11,0,7,0,0,0,5,120,121,122,122,121,15,0,8,8,0,0,0,3,0,0,0,1,0,0,0,2,0,0,0,3,14,0,9,11,0,0,0,3,0,0,0,2,98,98,0,0,0,3,99,99,99,0,0,0,1,97,13,0,10,11,8,0,0,0,3,0,0,0,1,73,0,0,0,1,0,0,0,1,88,0,0,0,10,0,0,0,1,86,0,0,0,5,15,0,11,2,0,0,0,4,1,1,0,1,15,0,12,3,0,0,0,0,15,0,13,4,0,0,0,2,63,240,0,0,0,0,0,0,192,0,0,0,0,0,0,0,15,0,14,11,0,0,0,3,0,0,0,3,111,110,101,0,0,0,3,116,119,111,0,0,0,5,116,104,114,101,101,0>>};
echo (V) ->
V.
17 changes: 17 additions & 0 deletions test/ox_thrift_compact_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%% Copyright 2016-2024, OpenX. All rights reserved.
%% Licensed under the conditions specified in the accompanying LICENSE file.

-module(ox_thrift_compact_tests).

-define(STATS_TABLE, ox_thrift_stats_compact).
-define(GLOBALS_TABLE, ox_thrift_globals_compact).

-define(PROTOCOL, ox_thrift_protocol_compact).
-include("ox_thrift_tests.hrl").

echo (#'AllTypes'{byte_field = 43}) ->
{ox_thrift_pre_encoded, compact,
<<17,19,43,68,156,252,3,5,10,245,1,6,8,224,251,230,171,241,217,162,163,2,7,6,0,0,0,0,0,64,36,64,72,5,120,121,122,122,121,25,53,2,4,6,26,56,2,98,98,3,99,99,99,1,97,27,3,133,1,73,2,1,88,20,1,86,10,25,65,2,2,0,2,25,3,25,39,0,0,0,0,0,0,240,63,0,0,0,0,0,0,0,192,25,56,3,111,110,101,3,116,119,111,5,116,104,114,101,101,0>>
};
echo (V) ->
V.
22 changes: 8 additions & 14 deletions test/ox_thrift_tests.erl → test/ox_thrift_tests.hrl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
%% Copyright 2016-2018, OpenX. All rights reserved.
%% Licensed under the conditions specified in the accompanying LICENSE file.

-module(ox_thrift_tests).

-include("ox_thrift.hrl").
-include("test_types.hrl").

Expand All @@ -16,8 +14,6 @@
-define(SERVICE, test_service_thrift).
-define(HANDLER, ?MODULE).
-define(STATS_MODULE, ?MODULE).
-define(STATS_TABLE, ox_thrift_stats).
-define(GLOBALS_TABLE, ox_thrift_globals).
-define(RECV_TIMEOUT_CLIENT, 200).

new_client_direct (Protocol) ->
Expand All @@ -34,7 +30,6 @@ destroy_client_direct (Client) ->

-define(LOCALHOST, "127.0.0.1").
-define(PORT, 8024).
-define(PROTOCOL, ox_thrift_protocol_binary).

new_client_socket () -> new_client_socket(dict, []).

Expand Down Expand Up @@ -86,11 +81,8 @@ destroy_stats_table () ->

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

direct_binary_test_ () ->
make_tests(direct, dict, fun () -> new_client_direct(ox_thrift_protocol_binary) end, fun destroy_client_direct/1).

direct_compact_test_ () ->
make_tests(direct, dict, fun () -> new_client_direct(ox_thrift_protocol_compact) end, fun destroy_client_direct/1).
direct_dict_test_ () ->
make_tests(direct, dict, fun () -> new_client_direct(?PROTOCOL) end, fun destroy_client_direct/1).

socket_dict_test_ () ->
make_tests(socket, dict, fun () -> new_client_socket(dict, []) end, fun destroy_client_socket/1).
Expand Down Expand Up @@ -237,7 +229,12 @@ all_types_test (_TestType, MapModule, NewClientFun, DestroyClientFun) ->
{ok, Client3, Reply3} = ox_thrift_client:call(Client2, echo, [ V3 ]),
?assertEqual(#'AllTypes'{string_field = <<"string">>}, Reply3),

DestroyClientFun(Client3).
%% Pre-encoded response
V4 = V1#'AllTypes'{byte_field = 43},
{ok, Client4, Reply4} = ox_thrift_client:call(Client3, echo, [ V4 ]),
?assertEqual(V4, Reply4),

DestroyClientFun(Client4).


is_open (Client) ->
Expand Down Expand Up @@ -499,9 +496,6 @@ sum_ints (#'Container'{first_field=FirstInt, second_struct=SecondStruct, third_f
end,
FirstInt + SecondSum + ThirdInt + SecondArg.

echo (V) ->
V.

simple_exception () ->
#'SimpleException'{message = <<"hello">>, line_number = 201}.

Expand Down
2 changes: 1 addition & 1 deletion test/thrift_shackle_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
-define(PORT, 8024).
-define(SERVICE, test_service_thrift).
-define(PROTOCOL, ox_thrift_protocol_binary).
-define(HANDLER, ox_thrift_tests).
-define(HANDLER, ox_thrift_binary_tests).

setup () ->
%% Start the thrift server.
Expand Down
Loading