You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.5 KiB

2 years ago
2 years ago
  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]string)
  11. prop["n1"] = "v1"
  12. prop["n2"] = "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.BuildMessageBatch()
  16. buf := bb.GetBytes()
  17. println("serialized size", len(buf))
  18. mb := message_fbs.GetRootAsMessageBatch(buf, 0)
  19. assert.Equal(t, int32(1), mb.ProducerId())
  20. assert.Equal(t, int32(2), mb.ProducerEpoch())
  21. assert.Equal(t, int32(3), mb.SegmentId())
  22. assert.Equal(t, int32(4), mb.Flags())
  23. assert.Equal(t, int64(5), mb.SegmentSeqBase())
  24. assert.Equal(t, int32(0), mb.SegmentSeqMaxDelta())
  25. assert.Equal(t, int64(6), mb.TsMsBase())
  26. assert.Equal(t, int32(0), mb.TsMsMaxDelta())
  27. assert.Equal(t, 1, mb.MessagesLength())
  28. m := &message_fbs.Message{}
  29. mb.Messages(m, 0)
  30. /*
  31. // the vector seems not consistent
  32. nv := &message_fbs.NameValue{}
  33. m.Properties(nv, 0)
  34. assert.Equal(t, "n1", string(nv.Name()))
  35. assert.Equal(t, "v1", string(nv.Value()))
  36. m.Properties(nv, 1)
  37. assert.Equal(t, "n2", string(nv.Name()))
  38. assert.Equal(t, "v2", string(nv.Value()))
  39. */
  40. assert.Equal(t, []byte("the primary key"), m.Key())
  41. assert.Equal(t, []byte("body is here"), m.Data())
  42. assert.Equal(t, int32(0), m.SeqDelta())
  43. assert.Equal(t, int32(0), m.TsMsDelta())
  44. }