Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add internal/db/cassandra/conviction test #799

Merged
merged 3 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion internal/db/nosql/cassandra/conviction.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ import (
type convictionPolicy struct {
}

// NewConvictionPolicy returns the implementation of gocql.ConvictionPolicy.
func NewConvictionPolicy() gocql.ConvictionPolicy {
return &convictionPolicy{}
return new(convictionPolicy)
}

// AddFailure implements gocql.ConvictionPolicy interface to handle failure and convicts all hosts.
func (c *convictionPolicy) AddFailure(err error, host *gocql.HostInfo) bool {
log.Warn(errors.ErrCassandraHostDownDetected(err, host.String()))
return true
}

// Reset clears the conviction state.
func (c *convictionPolicy) Reset(host *gocql.HostInfo) {
log.Info("cassandra host %s reset detected\t%s", host.HostnameAndPort(), host.String())
}
115 changes: 41 additions & 74 deletions internal/db/nosql/cassandra/conviction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@
package cassandra

import (
"net"
"os"
"reflect"
"testing"

"github.com/gocql/gocql"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/log"

"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
log.Init()
os.Exit(m.Run())
}

func TestNewConvictionPolicy(t *testing.T) {
type want struct {
want gocql.ConvictionPolicy
Expand All @@ -44,30 +52,17 @@ func TestNewConvictionPolicy(t *testing.T) {
return nil
}
tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
want: want{},
checkFunc: defaultCheckFunc,
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
want: want{},
checkFunc: defaultCheckFunc,
}
}(),
*/
{
name: "return conviction policy success",
want: want{
want: new(convictionPolicy),
},
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(t)
defer goleak.VerifyNone(tt, goleakIgnoreOptions...)
if test.beforeFunc != nil {
test.beforeFunc()
}
Expand Down Expand Up @@ -111,38 +106,25 @@ func Test_convictionPolicy_AddFailure(t *testing.T) {
return nil
}
tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
args: args {
err: nil,
host: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
args: args {
err: nil,
host: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
}
}(),
*/
{
name: "AddFailure successfully log the error",
args: args{
err: errors.New("dummy"),
host: func() *gocql.HostInfo {
h := &gocql.HostInfo{}
h.SetConnectAddress(net.IPv4(127, 0, 0, 1))
return h
}(),
},
want: want{
want: true,
},
},
}

for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(t)
defer goleak.VerifyNone(tt, goleakIgnoreOptions...)
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
Expand Down Expand Up @@ -182,36 +164,21 @@ func Test_convictionPolicy_Reset(t *testing.T) {
return nil
}
tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
args: args {
host: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
args: args {
host: nil,
},
want: want{},
checkFunc: defaultCheckFunc,
}
}(),
*/
{
name: "reset execute success",
args: args{
host: func() *gocql.HostInfo {
h := &gocql.HostInfo{}
h.SetConnectAddress(net.IPv4(127, 0, 0, 1))
return h
}(),
},
},
}

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