records_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 := Records{msgSet: 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 !reflect.DeepEqual(set, r.msgSet) {
  40. t.Errorf("Wrong decoding for legacy records, wanted %#+v, got %#+v", set, r.msgSet)
  41. }
  42. n := r.numRecords()
  43. if n != 1 {
  44. t.Errorf("Wrong number of records, wanted 1, got %d", n)
  45. }
  46. p := r.isPartial()
  47. if p {
  48. t.Errorf("MessageSet shouldn't have a partial trailing message")
  49. }
  50. }
  51. func TestDefaultRecords(t *testing.T) {
  52. batch := &RecordBatch{
  53. Version: 2,
  54. Records: []*Record{
  55. {
  56. Value: []byte{1},
  57. },
  58. },
  59. }
  60. r := Records{recordBatch: batch}
  61. exp, err := encode(batch, nil)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. buf, err := encode(&r, nil)
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. if !bytes.Equal(buf, exp) {
  70. t.Errorf("Wrong encoding for default records, wanted %v, got %v", exp, buf)
  71. }
  72. batch = &RecordBatch{}
  73. r = Records{}
  74. err = decode(exp, batch)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. err = decode(buf, &r)
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. if !reflect.DeepEqual(batch, r.recordBatch) {
  83. t.Errorf("Wrong decoding for default records, wanted %#+v, got %#+v", batch, r.recordBatch)
  84. }
  85. n := r.numRecords()
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. if n != 1 {
  90. t.Errorf("Wrong number of records, wanted 1, got %d", n)
  91. }
  92. p := r.isPartial()
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. if p {
  97. t.Errorf("RecordBatch shouldn't have a partial trailing record")
  98. }
  99. if r.recordBatch.Control {
  100. t.Errorf("RecordBatch shouldn't be a control batch")
  101. }
  102. }