message_serde_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package segment
  2. import (
  3. flatbuffers "github.com/google/flatbuffers/go"
  4. "github.com/seaweedfs/seaweedfs/weed/pb/message_fbs"
  5. "github.com/stretchr/testify/assert"
  6. "testing"
  7. )
  8. func TestMessageSerde(t *testing.T) {
  9. b := flatbuffers.NewBuilder(1024)
  10. prop := make(map[string][]byte)
  11. prop["n1"] = []byte("v1")
  12. prop["n2"] = []byte("v2")
  13. bb := NewMessageBatchBuilder(b, 1, 2, 3, 4)
  14. bb.AddMessage(5, 6, prop, []byte("the primary key"), []byte("body is here"))
  15. bb.AddMessage(5, 7, prop, []byte("the primary 2"), []byte("body is 2"))
  16. bb.BuildMessageBatch()
  17. buf := bb.GetBytes()
  18. println("serialized size", len(buf))
  19. mb := message_fbs.GetRootAsMessageBatch(buf, 0)
  20. assert.Equal(t, int32(1), mb.ProducerId())
  21. assert.Equal(t, int32(2), mb.ProducerEpoch())
  22. assert.Equal(t, int32(3), mb.SegmentId())
  23. assert.Equal(t, int32(4), mb.Flags())
  24. assert.Equal(t, int64(5), mb.SegmentSeqBase())
  25. assert.Equal(t, int32(0), mb.SegmentSeqMaxDelta())
  26. assert.Equal(t, int64(6), mb.TsMsBase())
  27. assert.Equal(t, int32(1), mb.TsMsMaxDelta())
  28. assert.Equal(t, 2, mb.MessagesLength())
  29. m := &message_fbs.Message{}
  30. mb.Messages(m, 0)
  31. /*
  32. // the vector seems not consistent
  33. nv := &message_fbs.NameValue{}
  34. m.Properties(nv, 0)
  35. assert.Equal(t, "n1", string(nv.Name()))
  36. assert.Equal(t, "v1", string(nv.Value()))
  37. m.Properties(nv, 1)
  38. assert.Equal(t, "n2", string(nv.Name()))
  39. assert.Equal(t, "v2", string(nv.Value()))
  40. */
  41. assert.Equal(t, []byte("the primary key"), m.Key())
  42. assert.Equal(t, []byte("body is here"), m.Data())
  43. assert.Equal(t, int32(0), m.SeqDelta())
  44. assert.Equal(t, int32(0), m.TsMsDelta())
  45. }