github.com/streamdal/segmentio-kafka-go@v0.4.47-streamdal/crc32_test.go (about)

     1  package kafka
     2  
     3  import (
     4  	"bytes"
     5  	"hash/crc32"
     6  	"testing"
     7  )
     8  
     9  func TestMessageCRC32(t *testing.T) {
    10  	m := message{
    11  		MagicByte: 1,
    12  		Timestamp: 42,
    13  		Key:       nil,
    14  		Value:     []byte("Hello World!"),
    15  	}
    16  
    17  	b := &bytes.Buffer{}
    18  	w := &writeBuffer{w: b}
    19  	w.write(m)
    20  
    21  	h := crc32.New(crc32.IEEETable)
    22  	h.Write(b.Bytes()[4:])
    23  
    24  	sum1 := h.Sum32()
    25  	sum2 := uint32(m.crc32(&crc32Writer{table: crc32.IEEETable}))
    26  
    27  	if sum1 != sum2 {
    28  		t.Error("bad CRC32:")
    29  		t.Logf("expected: %d", sum1)
    30  		t.Logf("found:    %d", sum2)
    31  	}
    32  }