2022-10-11 17:36:09 +08:00
|
|
|
package frame
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMetaFrameEncode(t *testing.T) {
|
2023-04-05 16:15:59 +08:00
|
|
|
m := &MetaFrame{tid: randString()}
|
|
|
|
m.SetBroadcast(true)
|
|
|
|
tidBuf := []byte(m.tid)
|
|
|
|
result := []byte{0x80 | byte(TagOfMetaFrame), byte(1 + 1 + len(tidBuf) + 2 + 3), 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)
|
2022-10-11 17:36:09 +08:00
|
|
|
assert.Equal(t, result, m.Encode())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaFrameDecode(t *testing.T) {
|
2023-04-05 16:15:59 +08:00
|
|
|
buf := []byte{0x80 | byte(TagOfMetaFrame), 0x0C, byte(TagOfTransactionId), 0x04, 0x31, 0x32, 0x33, 0x34, byte(TagOfSourceId), 0x01, 0x31, byte(TagOfBroadcast), 0x01, 0x01}
|
|
|
|
meta := &MetaFrame{}
|
|
|
|
err := DecodeToMetaFrame(buf, meta)
|
2022-10-11 17:36:09 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, "1234", meta.TransactionId())
|
|
|
|
assert.EqualValues(t, "1", meta.SourceId())
|
2023-04-05 16:15:59 +08:00
|
|
|
assert.EqualValues(t, true, meta.IsBroadcast())
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|