Skip to content

Commit

Permalink
Add tests for lz4 and snappy
Browse files Browse the repository at this point in the history
  • Loading branch information
Pryz committed Jul 2, 2018
1 parent 4b43636 commit 1b8bebd
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
2 changes: 2 additions & 0 deletions compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const (
CompressionLZ4
)

// CompressionCodec represents a compression codec to encode and decode
// the messages.
type CompressionCodec struct {
str func() string
encode func(src []byte, level int) ([]byte, error)
Expand Down
1 change: 0 additions & 1 deletion gzip/gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ func TestGzip(t *testing.T) {
t.Log("got: ", r2)
}
})

}
35 changes: 35 additions & 0 deletions lz4/lz4_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package lz4

import (
"bytes"
"testing"
)

func TestLZ4(t *testing.T) {
var r1, r2 []byte
var err error
payload := []byte("message")

t.Run("encode", func(t *testing.T) {
r1, err = Encode(payload, 1)
if err != nil {
t.Error(err)
}
if bytes.Equal(payload, r1) {
t.Error("failed to encode payload")
t.Log("got: ", r1)
}
})

t.Run("decode", func(t *testing.T) {
r2, err = Decode(r1)
if err != nil {
t.Error(err)
}
if !bytes.Equal(payload, r2) {
t.Error("failed to decode payload")
t.Log("expected: ", payload)
t.Log("got: ", r2)
}
})
}
3 changes: 3 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
// See : https://cwiki.apache.org/confluence/display/KAFKA/Compression
var codecs map[int8]CompressionCodec

// RegisterCompressionCodec registers a compression codec so it can be used by a Writer.
func RegisterCompressionCodec(code int8, str func() string, encode func(src []byte, level int) ([]byte, error), decode func(src []byte) ([]byte, error)) error {
if codecs == nil {
codecs = make(map[int8]CompressionCodec)
Expand Down Expand Up @@ -66,6 +67,7 @@ func (msg Message) message() message {
return m
}

// Encode encodes the Message using the CompressionCodec and CompressionLevel.
func (msg Message) Encode() (Message, error) {
codec, ok := codecs[msg.CompressionCodec]
if !ok {
Expand All @@ -81,6 +83,7 @@ func (msg Message) Encode() (Message, error) {
return msg, nil
}

// Decode decodes the Message using the CompressionCodec.
func (msg Message) Decode() (Message, error) {
c := msg.message().Attributes & compressionCodecMask
codec, ok := codecs[c]
Expand Down
35 changes: 35 additions & 0 deletions snappy/snappy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package snappy

import (
"bytes"
"testing"
)

func TestSnappy(t *testing.T) {
var r1, r2 []byte
var err error
payload := []byte("message")

t.Run("encode", func(t *testing.T) {
r1, err = Encode(payload, 1)
if err != nil {
t.Error(err)
}
if bytes.Equal(payload, r1) {
t.Error("failed to encode payload")
t.Log("got: ", r1)
}
})

t.Run("decode", func(t *testing.T) {
r2, err = Decode(r1)
if err != nil {
t.Error(err)
}
if !bytes.Equal(payload, r2) {
t.Error("failed to decode payload")
t.Log("expected: ", payload)
t.Log("got: ", r2)
}
})
}

0 comments on commit 1b8bebd

Please sign in to comment.