github.com/wfusion/gofusion@v1.1.14/test/common/utils/cases/serialize_test.go (about) 1 package cases 2 3 import ( 4 "context" 5 "io" 6 "reflect" 7 "testing" 8 9 "github.com/stretchr/testify/suite" 10 "github.com/wfusion/gofusion/test/internal/mock" 11 12 "github.com/wfusion/gofusion/common/utils" 13 "github.com/wfusion/gofusion/common/utils/serialize" 14 "github.com/wfusion/gofusion/log" 15 testUtl "github.com/wfusion/gofusion/test/common/utils" 16 ) 17 18 func TestSerialize(t *testing.T) { 19 t.Parallel() 20 testingSuite := &Serialize{Test: new(testUtl.Test)} 21 suite.Run(t, testingSuite) 22 } 23 24 type Serialize struct { 25 *testUtl.Test 26 } 27 28 func (t *Serialize) BeforeTest(suiteName, testName string) { 29 t.Catch(func() { 30 log.Info(context.Background(), "right before %s %s", suiteName, testName) 31 }) 32 } 33 34 func (t *Serialize) AfterTest(suiteName, testName string) { 35 t.Catch(func() { 36 log.Info(context.Background(), "right after %s %s", suiteName, testName) 37 }) 38 } 39 40 func (t *Serialize) TestBytes() { 41 t.Catch(func() { 42 algos := []serialize.Algorithm{ 43 serialize.AlgorithmGob, 44 serialize.AlgorithmJson, 45 serialize.AlgorithmMsgpack, 46 serialize.AlgorithmCbor, 47 } 48 49 for _, algo := range algos { 50 var ( 51 unmarshalSingleFunc func([]byte) (any, error) 52 unmarshalMultipleFunc func([]byte) (any, error) 53 ) 54 55 expected := mock.GenObjBySerializeAlgo(algo) 56 expectedList := mock.GenObjListBySerializeAlgo(algo, 3) 57 switch expected.(type) { 58 case *mock.CommonObj: 59 unmarshalSingleFunc = func(s []byte) (any, error) { 60 return serialize.UnmarshalFunc[*mock.CommonObj](algo)(s) 61 } 62 unmarshalMultipleFunc = func(s []byte) (any, error) { 63 return serialize.UnmarshalFunc[[]*mock.CommonObj](algo)(s) 64 } 65 case *mock.RandomObj: 66 unmarshalSingleFunc = func(s []byte) (any, error) { 67 return serialize.UnmarshalFunc[*mock.RandomObj](algo)(s) 68 } 69 unmarshalMultipleFunc = func(s []byte) (any, error) { 70 return serialize.UnmarshalFunc[[]*mock.RandomObj](algo)(s) 71 } 72 } 73 marshalFunc := serialize.MarshalFunc(algo) 74 t.Run(algo.String(), func() { 75 // single 76 marshaled, err := marshalFunc(expected) 77 t.NoError(err) 78 actualSingle, err := unmarshalSingleFunc(marshaled) 79 t.NoError(err) 80 t.EqualValues(expected, actualSingle) 81 82 // multiple 83 marshaled, err = marshalFunc(expectedList) 84 t.NoError(err) 85 actualMultiple, err := unmarshalMultipleFunc(marshaled) 86 t.NoError(err) 87 t.EqualValues(expectedList, actualMultiple) 88 }) 89 } 90 }) 91 } 92 93 func (t *Serialize) TestStream() { 94 t.Catch(func() { 95 algos := []serialize.Algorithm{ 96 serialize.AlgorithmGob, 97 serialize.AlgorithmJson, 98 serialize.AlgorithmMsgpack, 99 serialize.AlgorithmCbor, 100 } 101 commonType := reflect.TypeOf((*mock.CommonObj)(nil)) 102 commonListType := reflect.SliceOf(commonType) 103 randomType := reflect.TypeOf((*mock.RandomObj)(nil)) 104 randomListType := reflect.SliceOf(randomType) 105 106 for _, algo := range algos { 107 var ( 108 unmarshalSingleFunc func(io.Reader) (any, error) 109 unmarshalMultipleFunc func(io.Reader) (any, error) 110 ) 111 expected := mock.GenObjBySerializeAlgo(algo) 112 expectedList := mock.GenObjListBySerializeAlgo(algo, 3) 113 switch expected.(type) { 114 case *mock.CommonObj: 115 unmarshalSingleFunc = serialize.UnmarshalStreamFuncByType(algo, commonType) 116 unmarshalMultipleFunc = serialize.UnmarshalStreamFuncByType(algo, commonListType) 117 case *mock.RandomObj: 118 unmarshalSingleFunc = serialize.UnmarshalStreamFuncByType(algo, randomType) 119 unmarshalMultipleFunc = serialize.UnmarshalStreamFuncByType(algo, randomListType) 120 } 121 122 marshalFunc := serialize.MarshalStreamFunc(algo) 123 t.Run(algo.String(), func() { 124 marshaled, cb := utils.BytesBufferPool.Get(nil) 125 defer cb() 126 127 // single 128 err := marshalFunc(marshaled, expected) 129 t.NoError(err) 130 actualSingle, err := unmarshalSingleFunc(marshaled) 131 t.NoError(err) 132 t.EqualValues(expected, actualSingle) 133 134 marshaled.Reset() 135 136 // multiple 137 err = marshalFunc(marshaled, expectedList) 138 t.NoError(err) 139 actualMultiple, err := unmarshalMultipleFunc(marshaled) 140 t.NoError(err) 141 t.EqualValues(expectedList, actualMultiple) 142 }) 143 } 144 }) 145 }