Thrift now a TLP - INFRA-3116

git-svn-id: https://svn.apache.org/repos/asf/thrift/branches/0.1.x@1028168 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/test/erl/Makefile b/test/erl/Makefile
new file mode 100644
index 0000000..17e30da
--- /dev/null
+++ b/test/erl/Makefile
@@ -0,0 +1,66 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+GENDIR=gen
+GEN_INCLUDEDIR=$(GENDIR)/include
+GEN_SRCDIR=$(GENDIR)/src
+GEN_TARGETDIR=$(GENDIR)/ebin
+
+INCLUDEDIR=include
+TARGETDIR=ebin
+SRCDIR=src
+
+ALL_INCLUDEDIR=$(GEN_INCLUDEDIR) $(INCLUDEDIR) ../../lib/erl/include
+INCLUDEFLAGS=$(patsubst %,-I%, ${ALL_INCLUDEDIR})
+
+MODULES = stress_server test_server test_disklog test_membuffer
+
+INCLUDES = 
+TARGETS = $(patsubst %,${TARGETDIR}/%.beam,${MODULES})
+HEADERS = $(patsubst %,${INCLUDEDIR}/%.hrl,${INCLUDES})
+
+all: ${GEN_TARGETDIR}/ ${TARGETS}
+
+TEST_RPCFILE = ../ThriftTest.thrift
+STRESS_RPCFILE = ../StressTest.thrift
+THRIFT = ../../compiler/cpp/thrift
+
+${GENDIR}/: ${RPCFILE}
+	rm -rf ${GENDIR}
+	${THRIFT} --gen erl ${TEST_RPCFILE}
+	${THRIFT} --gen erl ${STRESS_RPCFILE}
+	mkdir -p ${GEN_INCLUDEDIR}
+	mkdir -p ${GEN_SRCDIR}
+	mkdir -p ${GEN_TARGETDIR}
+	mv -t ${GEN_INCLUDEDIR} gen-erl/*.hrl
+	mv -t ${GEN_SRCDIR} gen-erl/*.erl
+	rm -rf gen-erl
+
+${GEN_TARGETDIR}/: ${GENDIR}/
+	rm -rf ${GEN_TARGETDIR}
+	mkdir -p ${GEN_TARGETDIR}
+	erlc ${INCLUDEFLAGS} -o ${GEN_TARGETDIR} ${GEN_SRCDIR}/*.erl
+
+$(TARGETS): ${TARGETDIR}/%.beam: ${SRCDIR}/%.erl ${GEN_INCLUDEDIR}/ ${HEADERS}
+	mkdir -p ${TARGETDIR}
+	erlc ${INCLUDEFLAGS} -o ${TARGETDIR} $<
+
+clean:
+	rm -f ${TARGETDIR}/*.beam
+	rm -rf ${GENDIR}
diff --git a/test/erl/src/stress_server.erl b/test/erl/src/stress_server.erl
new file mode 100644
index 0000000..35fff06
--- /dev/null
+++ b/test/erl/src/stress_server.erl
@@ -0,0 +1,64 @@
+%%
+%% Licensed to the Apache Software Foundation (ASF) under one
+%% or more contributor license agreements. See the NOTICE file
+%% distributed with this work for additional information
+%% regarding copyright ownership. The ASF licenses this file
+%% to you under the Apache License, Version 2.0 (the
+%% "License"); you may not use this file except in compliance
+%% with the License. You may obtain a copy of the License at
+%%
+%%   http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing,
+%% software distributed under the License is distributed on an
+%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%% KIND, either express or implied. See the License for the
+%% specific language governing permissions and limitations
+%% under the License.
+%%
+
+-module(stress_server).
+
+
+-export([start_link/1,
+
+         handle_function/2,
+
+         echoVoid/0,
+         echoByte/1,
+         echoI32/1,
+         echoI64/1,
+         echoString/1,
+         echoList/1,
+         echoSet/1,
+         echoMap/1
+        ]).
+
+start_link(Port) ->
+    thrift_server:start_link(Port, service_thrift, ?MODULE).
+
+
+handle_function(Function, Args) ->
+    case apply(?MODULE, Function, tuple_to_list(Args)) of
+        ok ->
+             ok;
+        Else -> {reply, Else}
+    end.
+
+
+echoVoid() ->
+    ok.
+echoByte(X) ->
+    X.
+echoI32(X) ->
+    X.
+echoI64(X) ->
+    X.
+echoString(X) ->
+    X.
+echoList(X) ->
+    X.
+echoSet(X) ->
+    X.
+echoMap(X) ->
+    X.
diff --git a/test/erl/src/test_disklog.erl b/test/erl/src/test_disklog.erl
new file mode 100644
index 0000000..7b0be72
--- /dev/null
+++ b/test/erl/src/test_disklog.erl
@@ -0,0 +1,81 @@
+%%
+%% Licensed to the Apache Software Foundation (ASF) under one
+%% or more contributor license agreements. See the NOTICE file
+%% distributed with this work for additional information
+%% regarding copyright ownership. The ASF licenses this file
+%% to you under the Apache License, Version 2.0 (the
+%% "License"); you may not use this file except in compliance
+%% with the License. You may obtain a copy of the License at
+%%
+%%   http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing,
+%% software distributed under the License is distributed on an
+%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%% KIND, either express or implied. See the License for the
+%% specific language governing permissions and limitations
+%% under the License.
+%%
+
+-module(test_disklog).
+
+-compile(export_all).
+
+t() ->
+    {ok, TransportFactory} =
+        thrift_disk_log_transport:new_transport_factory(
+          test_disklog,
+          [{file, "/tmp/test_log"},
+           {size, {1024*1024, 10}}]),
+    {ok, ProtocolFactory} = thrift_binary_protocol:new_protocol_factory(
+                              TransportFactory, []),
+    {ok, Client} = thrift_client:start_link(ProtocolFactory, thriftTest_thrift),
+
+    io:format("Client started~n"),
+
+    % We have to make oneway calls into this client only since otherwise it will try
+    % to read from the disklog and go boom.
+    {ok, ok} = thrift_client:call(Client, testOneway, [16#deadbeef]),
+    io:format("Call written~n"),
+
+    % Use the send_call method to write a non-oneway call into the log
+    ok = thrift_client:send_call(Client, testString, [<<"hello world">>]),
+    io:format("Non-oneway call sent~n"),
+
+    ok = thrift_client:close(Client),
+    io:format("Client closed~n"),
+
+    ok.
+
+
+
+t_base64() ->
+    {ok, TransportFactory} =
+        thrift_disk_log_transport:new_transport_factory(
+          test_disklog,
+          [{file, "/tmp/test_b64_log"},
+           {size, {1024*1024, 10}}]),
+    {ok, B64Factory} =
+        thrift_base64_transport:new_transport_factory(TransportFactory),
+    {ok, BufFactory} =
+        thrift_buffered_transport:new_transport_factory(B64Factory),
+    {ok, ProtocolFactory} = thrift_binary_protocol:new_protocol_factory(
+                              BufFactory, []),
+    {ok, Client} = thrift_client:start_link(ProtocolFactory, thriftTest_thrift),
+
+    io:format("Client started~n"),
+
+    % We have to make oneway calls into this client only since otherwise it will try
+    % to read from the disklog and go boom.
+    {ok, ok} = thrift_client:call(Client, testOneway, [16#deadbeef]),
+    io:format("Call written~n"),
+
+    % Use the send_call method to write a non-oneway call into the log
+    ok = thrift_client:send_call(Client, testString, [<<"hello world">>]),
+    io:format("Non-oneway call sent~n"),
+
+    ok = thrift_client:close(Client),
+    io:format("Client closed~n"),
+
+    ok.
+    
diff --git a/test/erl/src/test_membuffer.erl b/test/erl/src/test_membuffer.erl
new file mode 100644
index 0000000..7bd23a0
--- /dev/null
+++ b/test/erl/src/test_membuffer.erl
@@ -0,0 +1,81 @@
+%%
+%% Licensed to the Apache Software Foundation (ASF) under one
+%% or more contributor license agreements. See the NOTICE file
+%% distributed with this work for additional information
+%% regarding copyright ownership. The ASF licenses this file
+%% to you under the Apache License, Version 2.0 (the
+%% "License"); you may not use this file except in compliance
+%% with the License. You may obtain a copy of the License at
+%%
+%%   http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing,
+%% software distributed under the License is distributed on an
+%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%% KIND, either express or implied. See the License for the
+%% specific language governing permissions and limitations
+%% under the License.
+%%
+
+-module(test_membuffer).
+-export([t/0]).
+
+-include("thriftTest_types.hrl").
+
+test_data() ->
+    #xtruct{string_thing = <<"foobar">>,
+	    byte_thing = 123,
+	    i32_thing = 1234567,
+	    i64_thing = 12345678900}.
+
+t1() ->
+    {ok, Transport} = thrift_memory_buffer:new(),
+    {ok, Protocol} = thrift_binary_protocol:new(Transport),
+    TestData = test_data(),
+    ok = thrift_protocol:write(Protocol,
+			       {{struct, element(2, thriftTest_types:struct_info('xtruct'))},
+				TestData}),
+    {ok, Result} = thrift_protocol:read(Protocol,
+					{struct, element(2, thriftTest_types:struct_info('xtruct'))},
+					'xtruct'),
+
+    Result = TestData.
+
+
+t2() ->
+    {ok, Transport} = thrift_memory_buffer:new(),
+    {ok, Protocol} = thrift_binary_protocol:new(Transport),
+    TestData = test_data(),
+    ok = thrift_protocol:write(Protocol,
+			       {{struct, element(2, thriftTest_types:struct_info('xtruct'))},
+				TestData}),
+    {ok, Result} = thrift_protocol:read(Protocol,
+					{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, Protocol} = thrift_binary_protocol:new(Transport),
+    TestData = #bools{im_true = true, im_false = false},
+    ok = thrift_protocol:write(Protocol,
+			       {{struct, element(2, thriftTest_types:struct_info('bools'))},
+				TestData}),
+    {ok, Result} = thrift_protocol:read(Protocol,
+					{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.
+
+
+t() ->
+    t1(),
+    t2(),
+    t3().
+
diff --git a/test/erl/src/test_server.erl b/test/erl/src/test_server.erl
new file mode 100644
index 0000000..cd439cc
--- /dev/null
+++ b/test/erl/src/test_server.erl
@@ -0,0 +1,174 @@
+%%
+%% Licensed to the Apache Software Foundation (ASF) under one
+%% or more contributor license agreements. See the NOTICE file
+%% distributed with this work for additional information
+%% regarding copyright ownership. The ASF licenses this file
+%% to you under the Apache License, Version 2.0 (the
+%% "License"); you may not use this file except in compliance
+%% with the License. You may obtain a copy of the License at
+%%
+%%   http://www.apache.org/licenses/LICENSE-2.0
+%%
+%% Unless required by applicable law or agreed to in writing,
+%% software distributed under the License is distributed on an
+%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+%% KIND, either express or implied. See the License for the
+%% specific language governing permissions and limitations
+%% under the License.
+%%
+
+-module(test_server).
+
+-export([start_link/1, handle_function/2]).
+
+-include("thriftTest_types.hrl").
+
+start_link(Port) ->
+    thrift_server:start_link(Port, thriftTest_thrift, ?MODULE).
+
+
+handle_function(testVoid, {}) ->
+    io:format("testVoid~n"),
+    ok;
+
+handle_function(testString, {S}) when is_binary(S) ->
+    io:format("testString: ~p~n", [S]),
+    {reply, S};
+
+handle_function(testByte, {I8}) when is_integer(I8) ->
+    io:format("testByte: ~p~n", [I8]),
+    {reply, I8};
+
+handle_function(testI32, {I32}) when is_integer(I32) ->
+    io:format("testI32: ~p~n", [I32]),
+    {reply, I32};
+
+handle_function(testI64, {I64}) when is_integer(I64) ->
+    io:format("testI64: ~p~n", [I64]),
+    {reply, I64};
+
+handle_function(testDouble, {Double}) when is_float(Double) ->
+    io:format("testDouble: ~p~n", [Double]),
+    {reply, Double};
+
+handle_function(testStruct,
+                {Struct = #xtruct{string_thing = String,
+                                 byte_thing = Byte,
+                                 i32_thing = I32,
+                                 i64_thing = I64}})
+when is_binary(String),
+     is_integer(Byte),
+     is_integer(I32),
+     is_integer(I64) ->
+    io:format("testStruct: ~p~n", [Struct]),
+    {reply, Struct};
+
+handle_function(testNest,
+                {Nest}) when is_record(Nest, xtruct2),
+                             is_record(Nest#xtruct2.struct_thing, xtruct) ->
+    io:format("testNest: ~p~n", [Nest]),
+    {reply, Nest};
+
+handle_function(testMap, {Map}) ->
+    io:format("testMap: ~p~n", [dict:to_list(Map)]),
+    {reply, Map};
+
+handle_function(testSet, {Set}) ->
+    true = sets:is_set(Set),
+    io:format("testSet: ~p~n", [sets:to_list(Set)]),
+    {reply, Set};
+
+handle_function(testList, {List}) when is_list(List) ->
+    io:format("testList: ~p~n", [List]),
+    {reply, List};
+
+handle_function(testEnum, {Enum}) when is_integer(Enum) ->
+    io:format("testEnum: ~p~n", [Enum]),
+    {reply, Enum};
+
+handle_function(testTypedef, {UserID}) when is_integer(UserID) ->
+    io:format("testTypedef: ~p~n", [UserID]),
+    {reply, UserID};
+
+handle_function(testMapMap, {Hello}) ->
+    io:format("testMapMap: ~p~n", [Hello]),
+
+    PosList = [{I, I}   || I <- lists:seq(1, 5)],
+    NegList = [{-I, -I} || I <- lists:seq(1, 5)],
+
+    MapMap = dict:from_list([{4,  dict:from_list(PosList)},
+                             {-4, dict:from_list(NegList)}]),
+    {reply, MapMap};
+
+handle_function(testInsanity, {Insanity}) when is_record(Insanity, insanity) ->
+    Hello = #xtruct{string_thing = <<"Hello2">>,
+                    byte_thing = 2,
+                    i32_thing = 2,
+                    i64_thing = 2},
+
+    Goodbye = #xtruct{string_thing = <<"Goodbye4">>,
+                      byte_thing = 4,
+                      i32_thing = 4,
+                      i64_thing = 4},
+    Crazy = #insanity{
+      userMap = dict:from_list([{?thriftTest_EIGHT, 8}]),
+      xtructs = [Goodbye]
+      },
+
+    Looney = #insanity{
+      userMap = dict:from_list([{?thriftTest_FIVE, 5}]),
+      xtructs = [Hello]
+      },
+
+    FirstMap = dict:from_list([{?thriftTest_TWO, Crazy},
+                               {?thriftTest_THREE, Crazy}]),
+
+    SecondMap = dict:from_list([{?thriftTest_SIX, Looney}]),
+    
+    Insane = dict:from_list([{1, FirstMap},
+                             {2, SecondMap}]),
+    
+    io:format("Return = ~p~n", [Insane]),
+    
+    {reply, Insane};
+
+handle_function(testMulti, Args = {Arg0, Arg1, Arg2, _Arg3, Arg4, Arg5})
+  when is_integer(Arg0),
+       is_integer(Arg1),
+       is_integer(Arg2),
+       is_integer(Arg4),
+       is_integer(Arg5) ->
+
+    io:format("testMulti(~p)~n", [Args]),
+    {reply, #xtruct{string_thing = <<"Hello2">>,
+                    byte_thing = Arg0,
+                    i32_thing = Arg1,
+                    i64_thing = Arg2}};
+
+handle_function(testException, {String}) when is_binary(String) ->
+    io:format("testException(~p)~n", [String]),
+    case String of
+        <<"Xception">> ->
+            throw(#xception{errorCode = 1001,
+                            message = <<"This is an Xception">>});
+        _ ->
+            ok
+    end;
+
+handle_function(testMultiException, {Arg0, Arg1}) ->
+    io:format("testMultiException(~p, ~p)~n", [Arg0, Arg1]),
+    case Arg0 of
+        <<"Xception">> ->
+            throw(#xception{errorCode = 1001,
+                                   message = <<"This is an Xception">>});
+        <<"Xception2">> ->
+            throw(#xception2{errorCode = 2002,
+                                    struct_thing =
+                                    #xtruct{string_thing = <<"This is an Xception2">>}});
+        _ ->
+            {reply, #xtruct{string_thing = Arg1}}
+    end;
+
+handle_function(testOneway, {Seconds}) ->
+    timer:sleep(1000 * Seconds),
+    ok.