records_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package sarama
  2. import (
  3. "bytes"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestLegacyRecords(t *testing.T) {
  8. set := &MessageSet{
  9. Messages: []*MessageBlock{
  10. {
  11. Msg: &Message{
  12. Version: 1,
  13. },
  14. },
  15. },
  16. }
  17. r := newLegacyRecords(set)
  18. exp, err := encode(set, nil)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. buf, err := encode(&r, nil)
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. if !bytes.Equal(buf, exp) {
  27. t.Errorf("Wrong encoding for legacy records, wanted %v, got %v", exp, buf)
  28. }
  29. set = &MessageSet{}
  30. r = Records{}
  31. err = decode(exp, set)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. err = decode(buf, &r)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. if r.recordsType != legacyRecords {
  40. t.Fatalf("Wrong records type %v, expected %v", r.recordsType, legacyRecords)
  41. }
  42. if !reflect.DeepEqual(set, r.MsgSet) {
  43. t.Errorf("Wrong decoding for legacy records, wanted %#+v, got %#+v", set, r.MsgSet)
  44. }
  45. n, err := r.numRecords()
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. if n != 1 {
  50. t.Errorf("Wrong number of records, wanted 1, got %d", n)
  51. }
  52. p, err := r.isPartial()
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. if p {
  57. t.Errorf("MessageSet shouldn't have a partial trailing message")
  58. }
  59. c, err := r.isControl()
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. if c {
  64. t.Errorf("MessageSet can't be a control batch")
  65. }
  66. }
  67. func TestDefaultRecords(t *testing.T) {
  68. batch := &RecordBatch{
  69. Version: 2,
  70. Records: []*Record{
  71. {
  72. Value: []byte{1},
  73. },
  74. },
  75. }
  76. r := newDefaultRecords(batch)
  77. exp, err := encode(batch, nil)
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. buf, err := encode(&r, nil)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. if !bytes.Equal(buf, exp) {
  86. t.Errorf("Wrong encoding for default records, wanted %v, got %v", exp, buf)
  87. }
  88. batch = &RecordBatch{}
  89. r = Records{}
  90. err = decode(exp, batch)
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. err = decode(buf, &r)
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. if r.recordsType != defaultRecords {
  99. t.Fatalf("Wrong records type %v, expected %v", r.recordsType, defaultRecords)
  100. }
  101. if !reflect.DeepEqual(batch, r.RecordBatch) {
  102. t.Errorf("Wrong decoding for default records, wanted %#+v, got %#+v", batch, r.RecordBatch)
  103. }
  104. n, err := r.numRecords()
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. if n != 1 {
  109. t.Errorf("Wrong number of records, wanted 1, got %d", n)
  110. }
  111. p, err := r.isPartial()
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. if p {
  116. t.Errorf("RecordBatch shouldn't have a partial trailing record")
  117. }
  118. c, err := r.isControl()
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. if c {
  123. t.Errorf("RecordBatch shouldn't be a control batch")
  124. }
  125. }