Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
Signed-off-by: LiangliangSui <[email protected]>
  • Loading branch information
LiangliangSui committed Apr 18, 2024
1 parent 5e3ebc0 commit 7d18714
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ on:
push:
branches:
- main
- go_magic_number
- 'releases/**'
- 'deploy/**'
- 'test*'
Expand Down
16 changes: 16 additions & 0 deletions go/fury/fury.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ const (
isOutOfBandFlag
)

const MAGIC_NUMBER int16 = 0x62d4

type Fury struct {
typeResolver *typeResolver
refResolver *RefResolver
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions go/fury/fury_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go/fury/fury_xlang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 11 additions & 0 deletions python/pyfury/_fury.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
DEFAULT_DYNAMIC_WRITE_STRING_ID = -1


MAGIC_NUMBER = 0x62d4


class MetaStringBytes:
__slots__ = (
"data",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions python/pyfury/_serialization.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ cdef int8_t USE_CLASS_ID = 1
# preserve 0 as flag for class id not set in ClassInfo`
cdef int8_t NO_CLASS_ID = 0
cdef int8_t DEFAULT_DYNAMIC_WRITE_STRING_ID = -1
cdef int8_t MAGIC_NUMBER = 0x62d4
cdef int8_t PYINT_CLASS_ID = 1
cdef int8_t PYFLOAT_CLASS_ID = 2
cdef int8_t PYBOOL_CLASS_ID = 3
Expand Down Expand Up @@ -899,6 +900,8 @@ cdef class Fury:
else:
self.buffer.writer_index = 0
buffer = self.buffer
if self.language == Language.XLANG:
buffer.write_int16(MAGIC_NUMBER)
cdef int32_t mask_index = buffer.writer_index
# 1byte used for bit mask
buffer.grow(1)
Expand Down Expand Up @@ -1058,6 +1061,11 @@ cdef class Fury:
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()
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")
cdef int32_t reader_index = buffer.reader_index
buffer.reader_index = reader_index + 1
if get_bit(buffer, reader_index, 0):
Expand Down
3 changes: 2 additions & 1 deletion python/pyfury/tests/test_cross_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

def debug_print(*params):
"""print params if debug is needed."""
# print(*params)
print(*params)


def cross_language_test(test_func):
Expand Down Expand Up @@ -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}"
Expand Down

0 comments on commit 7d18714

Please sign in to comment.