records_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. IsTransactional: true,
  70. Version: 2,
  71. Records: []*Record{
  72. {
  73. Value: []byte{1},
  74. },
  75. },
  76. }
  77. r := newDefaultRecords(batch)
  78. exp, err := encode(batch, nil)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. buf, err := encode(&r, nil)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. if !bytes.Equal(buf, exp) {
  87. t.Errorf("Wrong encoding for default records, wanted %v, got %v", exp, buf)
  88. }
  89. batch = &RecordBatch{}
  90. r = Records{}
  91. err = decode(exp, batch)
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. err = decode(buf, &r)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. if r.recordsType != defaultRecords {
  100. t.Fatalf("Wrong records type %v, expected %v", r.recordsType, defaultRecords)
  101. }
  102. if !reflect.DeepEqual(batch, r.RecordBatch) {
  103. t.Errorf("Wrong decoding for default records, wanted %#+v, got %#+v", batch, r.RecordBatch)
  104. }
  105. n, err := r.numRecords()
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. if n != 1 {
  110. t.Errorf("Wrong number of records, wanted 1, got %d", n)
  111. }
  112. p, err := r.isPartial()
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. if p {
  117. t.Errorf("RecordBatch shouldn't have a partial trailing record")
  118. }
  119. c, err := r.isControl()
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. if c {
  124. t.Errorf("RecordBatch shouldn't be a control batch")
  125. }
  126. }