123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package sarama
- import (
- "bytes"
- "reflect"
- "testing"
- )
- func TestLegacyRecords(t *testing.T) {
- set := &MessageSet{
- Messages: []*MessageBlock{
- {
- Msg: &Message{
- Version: 1,
- },
- },
- },
- }
- r := newLegacyRecords(set)
- exp, err := encode(set, nil)
- if err != nil {
- t.Fatal(err)
- }
- buf, err := encode(&r, nil)
- if err != nil {
- t.Fatal(err)
- }
- if !bytes.Equal(buf, exp) {
- t.Errorf("Wrong encoding for legacy records, wanted %v, got %v", exp, buf)
- }
- set = &MessageSet{}
- r = Records{}
- err = decode(exp, set)
- if err != nil {
- t.Fatal(err)
- }
- err = decode(buf, &r)
- if err != nil {
- t.Fatal(err)
- }
- if r.recordsType != legacyRecords {
- t.Fatalf("Wrong records type %v, expected %v", r.recordsType, legacyRecords)
- }
- if !reflect.DeepEqual(set, r.MsgSet) {
- t.Errorf("Wrong decoding for legacy records, wanted %#+v, got %#+v", set, r.MsgSet)
- }
- n, err := r.numRecords()
- if err != nil {
- t.Fatal(err)
- }
- if n != 1 {
- t.Errorf("Wrong number of records, wanted 1, got %d", n)
- }
- p, err := r.isPartial()
- if err != nil {
- t.Fatal(err)
- }
- if p {
- t.Errorf("MessageSet shouldn't have a partial trailing message")
- }
- c, err := r.isControl()
- if err != nil {
- t.Fatal(err)
- }
- if c {
- t.Errorf("MessageSet can't be a control batch")
- }
- }
- func TestDefaultRecords(t *testing.T) {
- batch := &RecordBatch{
- Version: 2,
- Records: []*Record{
- {
- Value: []byte{1},
- },
- },
- }
- r := newDefaultRecords(batch)
- exp, err := encode(batch, nil)
- if err != nil {
- t.Fatal(err)
- }
- buf, err := encode(&r, nil)
- if err != nil {
- t.Fatal(err)
- }
- if !bytes.Equal(buf, exp) {
- t.Errorf("Wrong encoding for default records, wanted %v, got %v", exp, buf)
- }
- batch = &RecordBatch{}
- r = Records{}
- err = decode(exp, batch)
- if err != nil {
- t.Fatal(err)
- }
- err = decode(buf, &r)
- if err != nil {
- t.Fatal(err)
- }
- if r.recordsType != defaultRecords {
- t.Fatalf("Wrong records type %v, expected %v", r.recordsType, defaultRecords)
- }
- if !reflect.DeepEqual(batch, r.RecordBatch) {
- t.Errorf("Wrong decoding for default records, wanted %#+v, got %#+v", batch, r.RecordBatch)
- }
- n, err := r.numRecords()
- if err != nil {
- t.Fatal(err)
- }
- if n != 1 {
- t.Errorf("Wrong number of records, wanted 1, got %d", n)
- }
- p, err := r.isPartial()
- if err != nil {
- t.Fatal(err)
- }
- if p {
- t.Errorf("RecordBatch shouldn't have a partial trailing record")
- }
- c, err := r.isControl()
- if err != nil {
- t.Fatal(err)
- }
- if c {
- t.Errorf("RecordBatch shouldn't be a control batch")
- }
- }
|