From: Anthony F. Molinaro Date: Sat, 18 Jun 2011 03:54:04 +0000 (+0000) Subject: THRIFT-1213 - make membuffer in erlang more useful X-Git-Tag: 0.7.0~62 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=3d3f42103ca2427da24f32d896e545618ae769eb;p=common%2Fthrift.git 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 --- diff --git a/lib/erl/src/thrift_memory_buffer.erl b/lib/erl/src/thrift_memory_buffer.erl index c44449ec..ce7c1dad 100644 --- a/lib/erl/src/thrift_memory_buffer.erl +++ b/lib/erl/src/thrift_memory_buffer.erl @@ -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}. diff --git a/test/erl/src/test_membuffer.erl b/test/erl/src/test_membuffer.erl index 19ac5277..fcf993a1 100644 --- a/test/erl/src/test_membuffer.erl +++ b/test/erl/src/test_membuffer.erl @@ -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'), - - Result = #xtruct3{string_thing = TestData#xtruct.string_thing, - changed = undefined, - i32_thing = TestData#xtruct.i32_thing, - i64_thing = TestData#xtruct.i64_thing}. + {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}. 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'), - - TestData = Result. - + {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. + +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().