Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Commit

Permalink
feature/tests 1 (#19)
Browse files Browse the repository at this point in the history
* add more logs for provider

* refactor static provider/integration tests & add tests & fix makefile tests

* add tests for viper provider close

---------

Co-authored-by: maxim-konovalov <maksim.konovalov@vk.team>
  • Loading branch information
KaymeKaydex and maxim-konovalov authored Apr 25, 2024
1 parent e02e856 commit aa80045
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
*.log

# System
*.pid
*.pid

# Go output
*.out
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TEST_TIMEOUT?=20s

test:
go test $(go list ./... | grep -v /tests/) -parallel=10 -timeout=$(TEST_TIMEOUT) -coverprofile=coverage.out
go test ./... -parallel=10 -timeout=$(TEST_TIMEOUT) -coverprofile=coverage.out

test/integration:
@$(MAKE) -C ./tests/integration test
1 change: 1 addition & 0 deletions providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (e *StdoutLogger) Warn(ctx context.Context, msg string) {

// Metrics

// MetricsProvider is an interface for passing library metrics to your prometheus/graphite and other metrics
type MetricsProvider interface {
CronDiscoveryEvent(ok bool, duration time.Duration, reason string)
RetryOnCall(reason string)
Expand Down
4 changes: 0 additions & 4 deletions providers/static/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ func NewProvider(rs map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo)
}

func (p *Provider) Validate() error {
if len(p.rs) < 1 {
return fmt.Errorf("replicasets are empty")
}

for rs := range p.rs {
// check replicaset name
if rs.Name == "" {
Expand Down
65 changes: 65 additions & 0 deletions providers/static/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package static

import (
"fmt"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

vshardrouter "github.com/KaymeKaydex/go-vshard-router"
Expand All @@ -14,6 +16,12 @@ func TestNewProvider(t *testing.T) {
}{
{nil},
{make(map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo)},
{map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo{
vshardrouter.ReplicasetInfo{}: {
vshardrouter.InstanceInfo{},
vshardrouter.InstanceInfo{},
},
}},
}

for _, tc := range tCases {
Expand All @@ -22,8 +30,65 @@ func TestNewProvider(t *testing.T) {
require.Panics(t, func() {
NewProvider(tc.Source)
})
return
}

require.NotPanics(t, func() {
provider := NewProvider(tc.Source)
require.NotNil(t, provider)
})

})
}
}

func TestProvider_Validate(t *testing.T) {
tCases := []struct {
Name string
Source map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo
IsErr bool
}{
{
Name: "empty name",
Source: map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo{
vshardrouter.ReplicasetInfo{}: {
vshardrouter.InstanceInfo{},
vshardrouter.InstanceInfo{},
},
},
IsErr: true,
},
{
Name: "no uuid",
Source: map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo{
vshardrouter.ReplicasetInfo{Name: "rs_1"}: {
vshardrouter.InstanceInfo{},
vshardrouter.InstanceInfo{},
},
},
IsErr: true,
},
{
Name: "valid",
Source: map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo{
vshardrouter.ReplicasetInfo{Name: "rs_1", UUID: uuid.New()}: {
vshardrouter.InstanceInfo{},
vshardrouter.InstanceInfo{},
},
},
IsErr: false,
},
}

for _, tc := range tCases {
t.Run(fmt.Sprintf("is err: %v", tc.IsErr), func(t *testing.T) {
provider := NewProvider(tc.Source)
if tc.IsErr {
require.Error(t, provider.Validate())
return
}

require.NoError(t, provider.Validate())
})
}
}
4 changes: 4 additions & 0 deletions providers/viper/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
vprovider "github.com/KaymeKaydex/go-vshard-router/providers/viper"
)

func TestProvider_Close(t *testing.T) {
require.NotPanics(t, (&vprovider.Provider{}).Close)
}

func TestNewProviderNilPanic(t *testing.T) {
require.Panics(t, func() {
vprovider.NewProvider(nil)
Expand Down
51 changes: 51 additions & 0 deletions providers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package vshard_router_test

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/require"

vshardrouter "github.com/KaymeKaydex/go-vshard-router"
)

var (
emptyMetrics = vshardrouter.EmptyMetrics{}
stdoutLogger = vshardrouter.StdoutLogger{}
)

func TestEmptyMetrics_RetryOnCall(t *testing.T) {
require.NotPanics(t, func() {
emptyMetrics.RetryOnCall("")
})
}

func TestEmptyMetrics_RequestDuration(t *testing.T) {
require.NotPanics(t, func() {
emptyMetrics.RequestDuration(time.Second, false, false)
})
}

func TestEmptyMetrics_CronDiscoveryEvent(t *testing.T) {
require.NotPanics(t, func() {
emptyMetrics.CronDiscoveryEvent(false, time.Second, "")
})
}

func TestStdoutLogger(t *testing.T) {
ctx := context.TODO()

require.NotPanics(t, func() {
stdoutLogger.Error(ctx, "")
})
require.NotPanics(t, func() {
stdoutLogger.Info(ctx, "")
})
require.NotPanics(t, func() {
stdoutLogger.Warn(ctx, "")
})
require.NotPanics(t, func() {
stdoutLogger.Debug(ctx, "")
})
}
8 changes: 6 additions & 2 deletions tests/integration/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build integration
// +build integration

package integration

import (
Expand All @@ -8,6 +11,7 @@ import (
"time"

vshard_router "github.com/KaymeKaydex/go-vshard-router"
"github.com/KaymeKaydex/go-vshard-router/providers/static"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
Expand All @@ -33,14 +37,14 @@ func TestReplicasetCall(t *testing.T) {
User: "test",
Password: "test",
TotalBucketCount: 300,
Replicasets: map[vshard_router.ReplicasetInfo][]vshard_router.InstanceInfo{
TopologyProvider: static.NewProvider(map[vshard_router.ReplicasetInfo][]vshard_router.InstanceInfo{
{
Name: "replicaset-1",
UUID: uuid.New(),
}: {
{Addr: server, UUID: uuid.New()},
},
},
}),
})
if err != nil {
log.Fatalf("create router err: %s", err.Error())
Expand Down
2 changes: 1 addition & 1 deletion vshard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestNewRouter_InvalidReplicasetUUID(t *testing.T) {
TopologyProvider: static.NewProvider(map[vshard_router.ReplicasetInfo][]vshard_router.InstanceInfo{
vshard_router.ReplicasetInfo{
Name: "123",
}: []vshard_router.InstanceInfo{
}: {
{Addr: "first.internal:1212"},
},
}),
Expand Down

0 comments on commit aa80045

Please sign in to comment.