Skip to content

Commit

Permalink
add zstd option
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindiu authored and actions-user committed Aug 12, 2020
1 parent 57d657e commit 376278a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 169 deletions.
3 changes: 3 additions & 0 deletions internal/compress/zstd_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/klauspost/compress/zstd"
)

// ZstdOption represents the functional option for zstdCompressor
type ZstdOption func(c *zstdCompressor) error

var (
Expand All @@ -30,6 +31,7 @@ var (
}
)

// WithZstdGob represents the option to set the GobOption to initialize Gob.
func WithZstdGob(opts ...GobOption) ZstdOption {
return func(c *zstdCompressor) error {
gobc, err := NewGob(opts...)
Expand All @@ -41,6 +43,7 @@ func WithZstdGob(opts ...GobOption) ZstdOption {
}
}

// WithZstdCompressionLevel represents the option to set the compress level for zstd.
func WithZstdCompressionLevel(level int) ZstdOption {
return func(c *zstdCompressor) error {
c.eoptions = append(c.eoptions, zstd.WithEncoderLevel(zstd.EncoderLevelFromZstd(level)))
Expand Down
259 changes: 90 additions & 169 deletions internal/compress/zstd_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,233 +18,154 @@
package compress

import (
"reflect"
"testing"

"github.com/klauspost/compress/zstd"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/test/comparator"
"go.uber.org/goleak"
)

func TestWithZstdGob(t *testing.T) {
type T = interface{}
type T = zstdCompressor
type args struct {
opts []GobOption
}
type want struct {
obj *T
// Uncomment this line if the option returns an error, otherwise delete it
// err error
err error
}
type test struct {
name string
args args
want want
// Use the first line if the option returns an error. otherwise use the second line
// checkFunc func(want, *T, error) error
// checkFunc func(want, *T) error
name string
args args
want want
checkFunc func(want, *T, error) error
beforeFunc func(args)
afterFunc func(args)
}

// Uncomment this block if the option returns an error, otherwise delete it
/*
defaultCheckFunc := func(w want, obj *T, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got = %v, want %v", obj, w.obj)
}
return nil
}
*/

// Uncomment this block if the option do not returns an error, otherwise delete it
/*
defaultCheckFunc := func(w want, obj *T) error {
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got = %v, want %v", obj, w.c)
}
return nil
}
*/
defaultCheckFunc := func(w want, obj *T, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got = %v, want %v", obj, w.obj)
}
return nil
}

tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
args: args {
opts: nil,
},
want: want {
obj: new(T),
},
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
args: args {
opts: nil,
},
want: want {
obj: new(T),
},
}
}(),
*/
{
name: "set zdtd gob option success",
args: args{
opts: nil,
},
want: want{
obj: func() *zstdCompressor {
c := new(T)
c.gobc = new(gobCompressor)
return c
}(),
},
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(t)
defer goleak.VerifyNone(tt)
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}

// Uncomment this block if the option returns an error, otherwise delete it
/*
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}
got := WithZstdGob(test.args.opts...)
obj := new(T)
if err := test.checkFunc(test.want, obj, got(obj)); err != nil {
tt.Errorf("error = %v", err)
}
*/

// Uncomment this block if the option returns an error, otherwise delete it
/*
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}
got := WithZstdGob(test.args.opts...)
obj := new(T)
got(obj)
if err := test.checkFunc(tt.want, obj); err != nil {
tt.Errorf("error = %v", err)
}
*/
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := WithZstdGob(test.args.opts...)
obj := new(T)
if err := test.checkFunc(test.want, obj, got(obj)); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestWithZstdCompressionLevel(t *testing.T) {
type T = interface{}
type T = zstdCompressor
type args struct {
level int
}
type want struct {
obj *T
// Uncomment this line if the option returns an error, otherwise delete it
// err error
err error
}
type test struct {
name string
args args
want want
// Use the first line if the option returns an error. otherwise use the second line
// checkFunc func(want, *T, error) error
// checkFunc func(want, *T) error
name string
args args
want want
checkFunc func(want, *T, error) error
beforeFunc func(args)
afterFunc func(args)
}

// Uncomment this block if the option returns an error, otherwise delete it
/*
defaultCheckFunc := func(w want, obj *T, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got = %v, want %v", obj, w.obj)
}
return nil
}
*/

// Uncomment this block if the option do not returns an error, otherwise delete it
/*
defaultCheckFunc := func(w want, obj *T) error {
if !reflect.DeepEqual(obj, w.obj) {
return errors.Errorf("got = %v, want %v", obj, w.c)
}
return nil
}
*/
defaultCheckFunc := func(w want, obj *T, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got error = %v, want %v", err, w.err)
}

zstdComparator := []comparator.Option{
comparator.AllowUnexported(*obj),
comparator.Comparer(func(x, y zstd.EOption) bool {
return !(x == nil && y != nil) || !(x != nil && y == nil)
}),
}

if diff := comparator.Diff(w.obj, obj, zstdComparator...); diff != "" {
return errors.New(diff)
}
return nil
}

tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
args: args {
level: 0,
},
want: want {
obj: new(T),
},
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
args: args {
level: 0,
},
want: want {
obj: new(T),
},
}
}(),
*/
{
name: "set compression level success",
args: args{
level: 1,
},
want: want{
obj: func() *zstdCompressor {
c := new(zstdCompressor)
c.eoptions = []zstd.EOption{
zstd.WithEncoderLevel(zstd.SpeedFastest),
}
return c
}(),
},
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(t)
defer goleak.VerifyNone(tt)
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}

// Uncomment this block if the option returns an error, otherwise delete it
/*
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}
got := WithZstdCompressionLevel(test.args.level)
obj := new(T)
if err := test.checkFunc(test.want, obj, got(obj)); err != nil {
tt.Errorf("error = %v", err)
}
*/

// Uncomment this block if the option returns an error, otherwise delete it
/*
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}
got := WithZstdCompressionLevel(test.args.level)
obj := new(T)
got(obj)
if err := test.checkFunc(tt.want, obj); err != nil {
tt.Errorf("error = %v", err)
}
*/
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

got := WithZstdCompressionLevel(test.args.level)
obj := new(T)
if err := test.checkFunc(test.want, obj, got(obj)); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

0 comments on commit 376278a

Please sign in to comment.