THRIFT-1213 - make membuffer in erlang more useful

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1137121 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Anthony F. Molinaro 2011-06-18 03:54:04 +00:00
parent a1c416fbbd
commit 3d3f42103c
2 changed files with 87 additions and 56 deletions

View File

@ -22,7 +22,7 @@
-behaviour(thrift_transport).
%% API
-export([new/0, new_transport_factory/0]).
-export([new/0, new/1, new_transport_factory/0]).
%% thrift_transport callbacks
-export([write/2, read/2, flush/1, close/1]).
@ -35,6 +35,13 @@ new() ->
State = #memory_buffer{buffer = []},
thrift_transport:new(?MODULE, State).
new (Buf) when is_list (Buf) ->
State = #memory_buffer{buffer = Buf},
thrift_transport:new(?MODULE, State);
new (Buf) ->
State = #memory_buffer{buffer = [Buf]},
thrift_transport:new(?MODULE, State).
new_transport_factory() ->
{ok, fun() -> new() end}.
@ -42,8 +49,8 @@ new_transport_factory() ->
write(State = #memory_buffer{buffer = Buf}, Data) ->
{State#memory_buffer{buffer = [Buf, Data]}, ok}.
flush(State) ->
{State, ok}.
flush(State = #memory_buffer {buffer = Buf}) ->
{State#memory_buffer{buffer = []}, Buf}.
close(State) ->
{State, ok}.

View File

@ -23,74 +23,98 @@
-include("thriftTest_types.hrl").
test_data() ->
#xtruct{string_thing = <<"foobar">>,
byte_thing = 123,
i32_thing = 1234567,
i64_thing = 12345678900}.
#xtruct {
string_thing = <<"foobar">>,
byte_thing = 123,
i32_thing = 1234567,
i64_thing = 12345678900
}.
t1() ->
{ok, Transport} = thrift_memory_buffer:new(),
{ok, Protocol0} = thrift_binary_protocol:new(Transport),
TestData = test_data(),
{Protocol1, ok} = thrift_protocol:write(Protocol0,
{{struct, element(2, thriftTest_types:struct_info('xtruct'))},
TestData}),
{_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
{struct, element(2, thriftTest_types:struct_info('xtruct'))},
'xtruct'),
Result = TestData.
{ok, Transport} = thrift_memory_buffer:new(),
{ok, Protocol0} = thrift_binary_protocol:new(Transport),
TestData = test_data(),
{Protocol1, ok} = thrift_protocol:write(Protocol0,
{{struct, element(2, thriftTest_types:struct_info('xtruct'))},
TestData}),
{_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
{struct, element(2, thriftTest_types:struct_info('xtruct'))},
'xtruct'),
Result = TestData.
t2() ->
{ok, Transport} = thrift_memory_buffer:new(),
{ok, Protocol0} = thrift_binary_protocol:new(Transport),
TestData = test_data(),
{Protocol1, ok} = thrift_protocol:write(Protocol0,
{{struct, element(2, thriftTest_types:struct_info('xtruct'))},
TestData}),
{_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
{struct, element(2, thriftTest_types:struct_info('xtruct3'))},
'xtruct3'),
{ok, Transport} = thrift_memory_buffer:new(),
{ok, Protocol0} = thrift_binary_protocol:new(Transport),
TestData = test_data(),
{Protocol1, ok} = thrift_protocol:write(Protocol0,
{{struct, element(2, thriftTest_types:struct_info('xtruct'))},
TestData}),
{_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
{struct, element(2, thriftTest_types:struct_info('xtruct3'))},
'xtruct3'),
Result = #xtruct3{string_thing = TestData#xtruct.string_thing,
changed = undefined,
i32_thing = TestData#xtruct.i32_thing,
i64_thing = TestData#xtruct.i64_thing}.
Result = #xtruct3{string_thing = TestData#xtruct.string_thing,
changed = undefined,
i32_thing = TestData#xtruct.i32_thing,
i64_thing = TestData#xtruct.i64_thing}.
t3() ->
{ok, Transport} = thrift_memory_buffer:new(),
{ok, Protocol0} = thrift_binary_protocol:new(Transport),
TestData = #bools{im_true = true, im_false = false},
{Protocol1, ok} = thrift_protocol:write(Protocol0,
{{struct, element(2, thriftTest_types:struct_info('bools'))},
TestData}),
{_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
{struct, element(2, thriftTest_types:struct_info('bools'))},
'bools'),
{ok, Transport} = thrift_memory_buffer:new(),
{ok, Protocol0} = thrift_binary_protocol:new(Transport),
TestData = #bools{im_true = true, im_false = false},
{Protocol1, ok} = thrift_protocol:write(Protocol0,
{{struct, element(2, thriftTest_types:struct_info('bools'))},
TestData}),
{_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
{struct, element(2, thriftTest_types:struct_info('bools'))},
'bools'),
true = TestData#bools.im_true =:= Result#bools.im_true,
true = TestData#bools.im_false =:= Result#bools.im_false.
true = TestData#bools.im_true =:= Result#bools.im_true,
true = TestData#bools.im_false =:= Result#bools.im_false.
t4() ->
{ok, Transport} = thrift_memory_buffer:new(),
{ok, Protocol0} = thrift_binary_protocol:new(Transport),
TestData = #insanity{xtructs=[]},
{Protocol1, ok} = thrift_protocol:write(Protocol0,
{{struct, element(2, thriftTest_types:struct_info('insanity'))},
TestData}),
{_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
{struct, element(2, thriftTest_types:struct_info('insanity'))},
'insanity'),
{ok, Transport} = thrift_memory_buffer:new(),
{ok, Protocol0} = thrift_binary_protocol:new(Transport),
TestData = #insanity{xtructs=[]},
{Protocol1, ok} = thrift_protocol:write(Protocol0,
{{struct, element(2, thriftTest_types:struct_info('insanity'))},
TestData}),
{_Protocol2, {ok, Result}} = thrift_protocol:read(Protocol1,
{struct, element(2, thriftTest_types:struct_info('insanity'))},
'insanity'),
TestData = Result.
TestData = Result.
t5() ->
% test writing to a buffer, getting the bytes out, putting them
% in a new buffer and reading them
% here's the writing part
{ok, Transport0} = thrift_memory_buffer:new(),
{ok, Protocol0} = thrift_binary_protocol:new(Transport0),
TestData = test_data(),
{Protocol1, ok} = thrift_protocol:write(Protocol0,
{{struct, element(2, thriftTest_types:struct_info('xtruct'))},
TestData}),
% flush now returns the buffer
{_Protocol2, Buf} = thrift_protocol:flush_transport (Protocol1),
% now the reading part
{ok, T2} = thrift_memory_buffer:new (Buf),
{ok, P2} = thrift_binary_protocol:new(T2),
{_, {ok, Result}} = thrift_protocol:read(P2,
{struct, element(2, thriftTest_types:struct_info('xtruct'))},
'xtruct'),
Result = TestData.
t() ->
t1(),
t2(),
t3(),
t4().
t1(),
t2(),
t3(),
t4(),
t5().