You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2019/11/19 18:23:15 UTC

[couchdb-thrift-protocol] 03/19: Add test for `thrift_protocol_binary` module

This is an automated email from the ASF dual-hosted git repository.

davisp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-thrift-protocol.git

commit 69a661b687b5e2d7546618cfddf83e9344f46a10
Author: Takeru Ohta <ph...@gmail.com>
AuthorDate: Wed Oct 18 19:46:16 2017 +0900

    Add test for `thrift_protocol_binary` module
---
 src/thrift_protocol_binary.erl        |  6 +--
 test/thrift_protocol_binary_tests.erl | 80 +++++++++++++++++++++++++++++++++++
 2 files changed, 83 insertions(+), 3 deletions(-)

diff --git a/src/thrift_protocol_binary.erl b/src/thrift_protocol_binary.erl
index 0bccf20..26af1cc 100644
--- a/src/thrift_protocol_binary.erl
+++ b/src/thrift_protocol_binary.erl
@@ -21,10 +21,10 @@ decode_message(<<1:1, ?VERSION:15, _:8, 0:5, TypeByte:3, NameLen:32, Name:NameLe
 
 -spec decode_struct(binary(), Fields) -> {thrift_protocol:struct(), binary()} when
       Fields :: #{thrift_protocol:field_id() => thrift_protocol:data()}.
-decode_struct(<<0:32, Rest/binary>>, Fields) ->
+decode_struct(<<0:8, Rest/binary>>, Fields) ->
     {#thrift_protocol_struct{fields = Fields}, Rest};
 decode_struct(<<TypeByte:8, Id:16/signed, Rest0/binary>>, Fields0) ->
-    Type = thrift_protocol_struct:to_data_type(TypeByte),
+    Type = thrift_protocol_byte:to_data_type(TypeByte),
     {Data, Rest1} = decode_data(Type, Rest0),
     Fields1 = maps:put(Id, Data, Fields0),
     decode_struct(Rest1, Fields1).
@@ -87,7 +87,7 @@ encode_struct(#thrift_protocol_struct{fields = Fields}) ->
               Type = thrift_protocol_byte:from_data_type(thrift_protocol:data_type(Data)),
               [<<Type:8, Id:16>>, encode_data(Data) | Acc]
       end,
-      [<<0:32>>],
+      [<<0:8>>],
       Fields).
 
 -spec encode_data(thrift_protocol:data()) -> iodata().
diff --git a/test/thrift_protocol_binary_tests.erl b/test/thrift_protocol_binary_tests.erl
new file mode 100644
index 0000000..fe97d89
--- /dev/null
+++ b/test/thrift_protocol_binary_tests.erl
@@ -0,0 +1,80 @@
+-module(thrift_protocol_binary_tests).
+
+-include_lib("eunit/include/eunit.hrl").
+-include("thrift_protocol.hrl").
+
+decode_test() ->
+    Input = bytes(),
+    {Message, <<>>} = thrift_protocol_binary:decode_message(Input),
+    ?assertEqual(message(), Message).
+
+encode_test() ->
+    Bytes = thrift_protocol_binary:encode_message(message()),
+    {Message, <<>>} = thrift_protocol_binary:decode_message(list_to_binary(Bytes)),
+    ?assertEqual(message(), Message),
+    ?assertEqual(lists:sort(binary_to_list(bytes())),
+                 lists:sort(binary_to_list(list_to_binary(Bytes)))).
+
+-spec bytes() -> binary().
+bytes() ->
+    <<128,1,0,4,0,0,0,9,101,109,105,116,66,97,116,99,104,0,0,0,1, 12,
+      0,1,12,0,1,11,0,1,0,0,0,11,102,111,111,95,115,101,114,118, 105,99,
+      101,15,0,2,12,0,0,0,3,11,0,1,0,0,0,14,106,97,101,103, 101,114,46,
+      118,101,114,115,105,111,110,8,0,2,0,0,0,0,11,0,3, 0,0,0,8,71,
+      111,45,50,46,57,46,48,0,11,0,1,0,0,0,8,104,111,115,116,110,97,
+      109,101,8,0,2,0,0,0,0,11,0,3,0,0,0,3,117,98,117,0,11,0,1,0,0,
+      0,2,105,112,8,0,2,0,0,0,0,11,0,3,0,0,0,9,49,48,46,48,46,50,46,
+      49,53,0,0,15,0,2,12,0,0,0,1,10,0,1,0,5,91,207,149,156,234,128,10,
+      0,2,0,0,0,0,0,0,0,0,10,0,3,0,0,0,0,0,0,3,21,10,0,4,0,0,0,0,0,0,
+      0,0,11,0,5,0,0,0,4,109,97,105,110,8,0,7,0,0,0,1,10,0,8,0,5,
+      91,207,149,156,234,128,10,0,9,0,0,0,0,0,1,226,64,0,0,0>>.
+
+-spec message() -> thrift_protocol:message().
+message() ->
+    Process =
+        #thrift_protocol_struct{
+           fields =
+               #{1 => <<"foo_service">>,
+                 2 => #thrift_protocol_list{
+                         element_type = struct,
+                         elements =
+                             [
+                              #thrift_protocol_struct{
+                                 fields = #{1 => <<"jaeger.version">>,
+                                            2 => {i32,0},
+                                            3 => <<"Go-2.9.0">>}},
+                              #thrift_protocol_struct{
+                                 fields = #{1 => <<"hostname">>,
+                                            2 => {i32,0},
+                                            3 => <<"ubu">>}},
+                              #thrift_protocol_struct{
+                                 fields = #{1 => <<"ip">>,
+                                            2 => {i32,0},
+                                            3 => <<"10.0.2.15">>}}
+                             ]
+                        }
+                }
+          },
+    Spans =
+        #thrift_protocol_list{
+           element_type = struct,
+           elements =
+               [#thrift_protocol_struct{
+                   fields =
+                       #{1 => {i64,1508322010000000},
+                         2 => {i64,0},
+                         3 => {i64,789},
+                         4 => {i64,0},
+                         5 => <<"main">>,
+                         7 => {i32,1},
+                         8 => {i64,1508322010000000},
+                         9 => {i64,123456}}}]
+          },
+    Batch =
+        #thrift_protocol_struct{fields = #{1 => Process, 2 => Spans}},
+    #thrift_protocol_message{
+       method_name = <<"emitBatch">>,
+       message_type = oneway,
+       sequence_id = 1,
+       body = #thrift_protocol_struct{fields = #{1 => Batch}}
+      }.