From 36336fc417ddca27d616396cf2373391310c9fa2 Mon Sep 17 00:00:00 2001 From: LiangliangSui Date: Thu, 18 Apr 2024 20:15:47 +0800 Subject: [PATCH] test Signed-off-by: LiangliangSui --- .github/workflows/ci.yml | 1 + go/fury/fury.go | 16 ++++++++++++++++ go/fury/fury_test.go | 12 ++++++++++++ go/fury/fury_xlang_test.go | 2 ++ python/pyfury/_fury.py | 11 +++++++++++ python/pyfury/tests/test_cross_language.py | 3 ++- 6 files changed, 44 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c3528629b0..1e59e9c865 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,6 +21,7 @@ on: push: branches: - main + - go_magic_number - 'releases/**' - 'deploy/**' - 'test*' diff --git a/go/fury/fury.go b/go/fury/fury.go index dff88c9049..16463837c4 100644 --- a/go/fury/fury.go +++ b/go/fury/fury.go @@ -90,6 +90,8 @@ const ( isOutOfBandFlag ) +const MAGIC_NUMBER int16 = 0x62d4 + type Fury struct { typeResolver *typeResolver refResolver *RefResolver @@ -121,6 +123,11 @@ func (f *Fury) Serialize(buf *ByteBuffer, v interface{}, callback BufferCallback buffer = f.buffer buffer.writerIndex = 0 } + if f.language == XLANG { + buffer.WriteInt16(MAGIC_NUMBER) + } else { + return fmt.Errorf("%d language is not supported", f.language) + } var bitmap byte = 0 if isNil(reflect.ValueOf(v)) { bitmap |= isNilFlag @@ -310,6 +317,15 @@ func (f *Fury) Unmarshal(data []byte, v interface{}) error { func (f *Fury) Deserialize(buf *ByteBuffer, v interface{}, buffers []*ByteBuffer) error { defer f.resetRead() + if f.language == XLANG { + magicNumber := buf.ReadInt16() + if magicNumber != MAGIC_NUMBER { + return fmt.Errorf("the XLANG cross-language protocol is not currently used. For details," + + "see https://github.com/apache/incubator-fury/blob/main/docs/specification/xlang_serialization_spec.md") + } + } else { + return fmt.Errorf("%d language is not supported", f.language) + } var bitmap = buf.ReadByte_() if bitmap&isNilFlag == isNilFlag { return nil diff --git a/go/fury/fury_test.go b/go/fury/fury_test.go index 5e80db1f03..a6c4d834fe 100644 --- a/go/fury/fury_test.go +++ b/go/fury/fury_test.go @@ -243,6 +243,18 @@ func TestSerializeStructSimple(t *testing.T) { } } +func TestSerializeBeginWithMagicNumber(t *testing.T) { + strSlice := []string{"str1", "str1", "", "", "str2"} + fury := NewFury(true) + bytes, err := fury.Marshal(strSlice) + require.Nil(t, err, fmt.Sprintf("serialize value %s with type %s failed: %s", + reflect.ValueOf(strSlice), reflect.TypeOf(strSlice), err)) + // Contains at least two bytes. + require.True(t, len(bytes) > 2) + magicNumber := int16(bytes[0]) | (int16(bytes[1]) << 8) + require.Equal(t, magicNumber, MAGIC_NUMBER) +} + type Foo struct { F1 int32 F2 string diff --git a/go/fury/fury_xlang_test.go b/go/fury/fury_xlang_test.go index d97719bd4c..f72f68e2e5 100644 --- a/go/fury/fury_xlang_test.go +++ b/go/fury/fury_xlang_test.go @@ -267,6 +267,8 @@ func structRoundBack(t *testing.T, fury_ *fury.Fury, obj interface{}, testName s } marshal, err := fury_.Marshal(obj) require.Nil(t, err) + fmt.Println("marshal len:", len(marshal)) + fmt.Println("marshal:", marshal) check(marshal) require.Nil(t, ioutil.WriteFile(testName, marshal, 0644)) diff --git a/python/pyfury/_fury.py b/python/pyfury/_fury.py index 8f714cdec0..139f3ec200 100644 --- a/python/pyfury/_fury.py +++ b/python/pyfury/_fury.py @@ -89,6 +89,9 @@ DEFAULT_DYNAMIC_WRITE_STRING_ID = -1 +MAGIC_NUMBER = 0x62d4 + + class MetaStringBytes: __slots__ = ( "data", @@ -697,6 +700,8 @@ def _serialize( else: self.buffer.writer_index = 0 buffer = self.buffer + if self.language == Language.XLANG: + buffer.write_int16(MAGIC_NUMBER) mask_index = buffer.writer_index # 1byte used for bit mask buffer.grow(1) @@ -843,6 +848,12 @@ def _deserialize( self.unpickler = Unpickler(buffer) if unsupported_objects is not None: self._unsupported_objects = iter(unsupported_objects) + if self.language == Language.XLANG: + magic_numer = buffer.read_int16() + print("sll_ttest", magic_numer) + assert magic_numer == MAGIC_NUMBER, ( + "The XLANG cross-language protocol is not currently used. For details," + "see https://github.com/apache/incubator-fury/blob/main/docs/specification/xlang_serialization_spec.md") reader_index = buffer.reader_index buffer.reader_index = reader_index + 1 if get_bit(buffer, reader_index, 0): diff --git a/python/pyfury/tests/test_cross_language.py b/python/pyfury/tests/test_cross_language.py index 9c0cc93e95..652730aa25 100644 --- a/python/pyfury/tests/test_cross_language.py +++ b/python/pyfury/tests/test_cross_language.py @@ -33,7 +33,7 @@ def debug_print(*params): """print params if debug is needed.""" - # print(*params) + print(*params) def cross_language_test(test_func): @@ -488,6 +488,7 @@ def struct_round_back(data_file_path, fury, obj1): with open(data_file_path, "rb") as f: data_bytes = f.read() debug_print(f"len {len(data_bytes)}") + debug_print(data_bytes) new_obj = fury.deserialize(data_bytes) debug_print(new_obj) assert new_obj == obj1, f"new_obj {new_obj}\n expected {obj1}"