2022-10-11 17:36:09 +08:00
|
|
|
package frame
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDataFrameEncode(t *testing.T) {
|
2023-04-05 16:15:59 +08:00
|
|
|
var userDataTag Tag = 0x15
|
2022-10-11 17:36:09 +08:00
|
|
|
d := NewDataFrame()
|
|
|
|
d.SetCarriage(userDataTag, []byte("hpds"))
|
2023-04-05 16:15:59 +08:00
|
|
|
d.SetBroadcast(true)
|
|
|
|
|
|
|
|
assert.EqualValues(t, "", d.SourceId())
|
2022-10-11 17:36:09 +08:00
|
|
|
|
2022-10-12 10:16:59 +08:00
|
|
|
tidBuf := []byte(d.TransactionId())
|
2022-10-11 17:36:09 +08:00
|
|
|
result := []byte{
|
2023-04-05 16:15:59 +08:00
|
|
|
0x80 | byte(TagOfDataFrame), byte(len(tidBuf) + 4 + 8 + 5 + 3),
|
|
|
|
0x80 | byte(TagOfMetaFrame), byte(len(tidBuf) + 2 + 2 + 3),
|
2022-10-12 10:16:59 +08:00
|
|
|
byte(TagOfTransactionId), byte(len(tidBuf))}
|
|
|
|
result = append(result, tidBuf...)
|
2022-10-11 17:36:09 +08:00
|
|
|
result = append(result, byte(TagOfSourceId), 0x0)
|
2023-04-05 16:15:59 +08:00
|
|
|
result = append(result, byte(TagOfBroadcast), 0x1, 0x1)
|
|
|
|
result = append(result, 0x80|byte(TagOfPayloadFrame), 0x09,
|
|
|
|
0x01, 0x1, 0x15, 0x02, 0x04, 0x68, 0x70, 0x64, 0x73)
|
2022-10-11 17:36:09 +08:00
|
|
|
assert.Equal(t, result, d.Encode())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDataFrameDecode(t *testing.T) {
|
2023-04-05 16:15:59 +08:00
|
|
|
var userDataTag Tag = 0x15
|
2022-10-11 17:36:09 +08:00
|
|
|
buf := []byte{
|
2023-04-05 16:15:59 +08:00
|
|
|
0x80 | byte(TagOfDataFrame), 0x10 + 3,
|
|
|
|
0x80 | byte(TagOfMetaFrame), 0x06 + 3,
|
2022-10-11 17:36:09 +08:00
|
|
|
byte(TagOfTransactionId), 0x04, 0x31, 0x32, 0x33, 0x34,
|
2023-04-05 16:15:59 +08:00
|
|
|
byte(TagOfBroadcast), 0x01, 0x01,
|
|
|
|
0x80 | byte(TagOfPayloadFrame), 0x09,
|
|
|
|
0x01, 0x1, 0x15, 0x02, 0x04, 0x68, 0x70, 0x64, 0x73}
|
2022-10-11 17:36:09 +08:00
|
|
|
data, err := DecodeToDataFrame(buf)
|
2023-04-05 16:15:59 +08:00
|
|
|
defer data.Clean()
|
2022-10-11 17:36:09 +08:00
|
|
|
assert.NoError(t, err)
|
2023-04-05 16:15:59 +08:00
|
|
|
|
|
|
|
assert.EqualValues(t, 0x15, data.Tag())
|
2022-10-11 17:36:09 +08:00
|
|
|
assert.EqualValues(t, "1234", data.TransactionId())
|
|
|
|
assert.EqualValues(t, userDataTag, data.GetDataTag())
|
|
|
|
assert.EqualValues(t, []byte("hpds"), data.GetCarriage())
|
2023-04-05 16:15:59 +08:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
})
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|