Skip to content

Commit

Permalink
chore: Move test etcd helper to cluster to try to avoid dependency cr…
Browse files Browse the repository at this point in the history
…eep in projects that depend on the client
  • Loading branch information
felipejfc committed Sep 1, 2023
1 parent fe4ef7d commit 4b83bf9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func TestStartAndListenStandalone(t *testing.T) {
}

func TestStartAndListenCluster(t *testing.T) {
es, cli := helpers.GetTestEtcd(t)
es, cli := cluster.GetTestEtcd(t)
defer es.Terminate(t)

ns := helpers.GetTestNatsServer(t)
Expand Down
33 changes: 17 additions & 16 deletions cluster/etcd_service_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import (
"math"
"testing"
"time"
clientv3 "go.etcd.io/etcd/client/v3"

"github.com/stretchr/testify/assert"
"github.com/topfreegames/pitaya/v2/config"
"github.com/topfreegames/pitaya/v2/constants"
"github.com/topfreegames/pitaya/v2/helpers"
clientv3 "go.etcd.io/etcd/client/v3"
)

var etcdSDTables = []struct {
Expand Down Expand Up @@ -106,7 +107,7 @@ func TestNewEtcdServiceDiscovery(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := config.NewDefaultEtcdServiceDiscoveryConfig()
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, *config, table.server, cli)
assert.NotNil(t, e)
Expand All @@ -119,7 +120,7 @@ func TestEtcdSDBootstrapLease(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := config.NewDefaultEtcdServiceDiscoveryConfig()
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, *config, table.server, cli)
err := e.grantLease()
Expand All @@ -134,7 +135,7 @@ func TestEtcdSDBootstrapLeaseError(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := config.NewDefaultEtcdServiceDiscoveryConfig()
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, *config, table.server, cli)
err := e.grantLease()
Expand All @@ -148,7 +149,7 @@ func TestEtcdSDBootstrapServer(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := config.NewDefaultEtcdServiceDiscoveryConfig()
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, *config, table.server, cli)
e.Init()
Expand All @@ -173,7 +174,7 @@ func TestEtcdSDDeleteServer(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := config.NewDefaultEtcdServiceDiscoveryConfig()
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, *config, table.server, cli)
e.Init()
Expand Down Expand Up @@ -213,7 +214,7 @@ func TestEtcdSDDeleteLocalInvalidServers(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := config.NewDefaultEtcdServiceDiscoveryConfig()
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, *config, table.server, cli)
invalidServer := &Server{
Expand All @@ -234,7 +235,7 @@ func TestEtcdSDGetServer(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := config.NewDefaultEtcdServiceDiscoveryConfig()
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, *config, table.server, cli)
e.Init()
Expand All @@ -250,7 +251,7 @@ func TestEtcdSDGetServers(t *testing.T) {
t.Parallel()
for _, table := range etcdSDTablesMultipleServers {
config := config.NewDefaultEtcdServiceDiscoveryConfig()
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, *config, &Server{}, cli)
e.Init()
Expand All @@ -270,7 +271,7 @@ func TestEtcdSDInit(t *testing.T) {
t.Run(table.server.ID, func(t *testing.T) {
config := config.NewDefaultEtcdServiceDiscoveryConfig()
config.SyncServers.Interval = time.Duration(300 * time.Millisecond)
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, *config, table.server, cli)
e.Init()
Expand All @@ -297,7 +298,7 @@ func TestEtcdBeforeShutdown(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := config.NewDefaultEtcdServiceDiscoveryConfig()
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, *config, table.server, cli)
e.Init()
Expand All @@ -315,7 +316,7 @@ func TestEtcdShutdown(t *testing.T) {
for _, table := range etcdSDTables {
t.Run(table.server.ID, func(t *testing.T) {
config := config.NewDefaultEtcdServiceDiscoveryConfig()
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, *config, table.server, cli)
e.Init()
Expand All @@ -332,7 +333,7 @@ func TestEtcdWatchChangesAddNewServers(t *testing.T) {
t.Run(table.server.ID, func(t *testing.T) {
config := config.NewDefaultEtcdServiceDiscoveryConfig()
config.SyncServers.Interval = time.Duration(100 * time.Millisecond)
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, *config, table.server, cli)
e.Init()
Expand Down Expand Up @@ -364,7 +365,7 @@ func TestEtcdWatchChangesDeleteServers(t *testing.T) {
t.Run(table.server.ID, func(t *testing.T) {
config := config.NewDefaultEtcdServiceDiscoveryConfig()
config.SyncServers.Interval = 100 * time.Millisecond
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, *config, table.server, cli)
e.Init()
Expand Down Expand Up @@ -403,7 +404,7 @@ func TestEtcdWatchChangesWithBlacklist(t *testing.T) {
config := config.NewDefaultEtcdServiceDiscoveryConfig()
config.SyncServers.Interval = 100 * time.Millisecond
config.ServerTypesBlacklist = table.serverTypeBlacklist
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)
e := getEtcdSD(t, *config, table.server, cli)
e.Init()
Expand Down Expand Up @@ -455,7 +456,7 @@ func TestEtcdWatchChangesWithBlacklist(t *testing.T) {
}

func TestParallelGetter(t *testing.T) {
c, cli := helpers.GetTestEtcd(t)
c, cli := GetTestEtcd(t)
defer c.Terminate(t)

serversToAdd := []*Server{
Expand Down
17 changes: 17 additions & 0 deletions cluster/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cluster

import (
"testing"

clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/integration"
)

// GetTestEtcd gets a test in memory etcd server
func GetTestEtcd(t *testing.T) (*integration.ClusterV3, *clientv3.Client) {
t.Helper()
integration.BeforeTest(t)
c := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
cli := c.RandClient()
return c, cli
}
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncj
github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.etcd.io/gofail v0.1.0 h1:XItAMIhOojXFQMgrxjnd2EIIHun/d5qL0Pf7FzVTkFg=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/proto/otlp v0.10.0/go.mod h1:zG20xCK0szZ1xdokeSOwEcmlXu+x9kkdRe6N1DhKcfU=
go.opentelemetry.io/proto/otlp v0.15.0 h1:h0bKrvdrT/9sBwEJ6iWUqT/N/xPcS66bL4u3isneJ6w=
Expand Down
11 changes: 0 additions & 11 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"testing"
"time"

clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/tests/v3/integration"
"github.com/nats-io/nats-server/v2/server"
gnatsd "github.com/nats-io/nats-server/v2/test"
)
Expand Down Expand Up @@ -61,15 +59,6 @@ func GetTestNatsServer(t *testing.T) *server.Server {
return s
}

// GetTestEtcd gets a test in memory etcd server
func GetTestEtcd(t *testing.T) (*integration.ClusterV3, *clientv3.Client) {
t.Helper()
integration.BeforeTest(t)
c := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
cli := c.RandClient()
return c, cli
}

// WriteFile test helper
func WriteFile(t *testing.T, filepath string, bytes []byte) {
t.Helper()
Expand Down

0 comments on commit 4b83bf9

Please sign in to comment.