package frame import ( "testing" "github.com/stretchr/testify/assert" ) func TestDataFrameEncode(t *testing.T) { var userDataTag Tag = 0x15 d := NewDataFrame() d.SetCarriage(userDataTag, []byte("hpds")) d.SetBroadcast(true) assert.EqualValues(t, "", d.SourceId()) tidBuf := []byte(d.TransactionId()) result := []byte{ 0x80 | byte(TagOfDataFrame), byte(len(tidBuf) + 4 + 8 + 5 + 3), 0x80 | byte(TagOfMetaFrame), byte(len(tidBuf) + 2 + 2 + 3), byte(TagOfTransactionId), byte(len(tidBuf))} result = append(result, tidBuf...) result = append(result, byte(TagOfSourceId), 0x0) result = append(result, byte(TagOfBroadcast), 0x1, 0x1) result = append(result, 0x80|byte(TagOfPayloadFrame), 0x09, 0x01, 0x1, 0x15, 0x02, 0x04, 0x68, 0x70, 0x64, 0x73) assert.Equal(t, result, d.Encode()) } func TestDataFrameDecode(t *testing.T) { var userDataTag Tag = 0x15 buf := []byte{ 0x80 | byte(TagOfDataFrame), 0x10 + 3, 0x80 | byte(TagOfMetaFrame), 0x06 + 3, byte(TagOfTransactionId), 0x04, 0x31, 0x32, 0x33, 0x34, byte(TagOfBroadcast), 0x01, 0x01, 0x80 | byte(TagOfPayloadFrame), 0x09, 0x01, 0x1, 0x15, 0x02, 0x04, 0x68, 0x70, 0x64, 0x73} data, err := DecodeToDataFrame(buf) defer data.Clean() assert.NoError(t, err) assert.EqualValues(t, 0x15, data.Tag()) assert.EqualValues(t, "1234", data.TransactionId()) assert.EqualValues(t, userDataTag, data.GetDataTag()) assert.EqualValues(t, []byte("hpds"), data.GetCarriage()) assert.EqualValues(t, true, data.IsBroadcast()) } func BenchmarkDataFramePool(b *testing.B) { var ( tag = Tag(0x15) payload = []byte("hpds") ) b.RunParallel(func(pb *testing.PB) { for pb.Next() { prev := NewDataFrame() prev.SetCarriage(tag, payload) prev.SetBroadcast(true) prev.Clean() } }) }