Skip to content

Commit

Permalink
Refactor discovererOpts function and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ykadowak committed Jan 18, 2024
1 parent d35ae2c commit 1f78faf
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 23 deletions.
55 changes: 32 additions & 23 deletions pkg/gateway/lb/usecase/vald.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,7 @@ type run struct {
gateway service.Gateway
}

func New(cfg *config.Data) (r runner.Runner, err error) {
eg := errgroup.Get()

var gateway service.Gateway

cOpts, err := cfg.Gateway.Discoverer.Client.Opts()
if err != nil {
return nil, err
}

// skipcq: CRT-D0001
dopts := append(
cOpts,
grpc.WithErrGroup(eg))
acOpts, err := cfg.Gateway.Discoverer.AgentClientOptions.Opts()
if err != nil {
return nil, err
}
// skipcq: CRT-D0001
aopts := append(
acOpts,
grpc.WithErrGroup(eg))

func discovererOpts(cfg *config.Data, dopts []grpc.Option, aopts []grpc.Option, eg errgroup.Group) ([]discoverer.Option, error) {
var discovererOpts []discoverer.Option
discovererOpts = append(discovererOpts,
discoverer.WithAutoConnect(true),
Expand Down Expand Up @@ -96,6 +74,37 @@ func New(cfg *config.Data) (r runner.Runner, err error) {
discovererOpts = append(discovererOpts, discoverer.WithReadReplicaClient(grpc.New(rrOpts...)))
}

return discovererOpts, nil
}

func New(cfg *config.Data) (r runner.Runner, err error) {
eg := errgroup.Get()

var gateway service.Gateway

cOpts, err := cfg.Gateway.Discoverer.Client.Opts()
if err != nil {
return nil, err
}

// skipcq: CRT-D0001
dopts := append(
cOpts,
grpc.WithErrGroup(eg))
acOpts, err := cfg.Gateway.Discoverer.AgentClientOptions.Opts()
if err != nil {
return nil, err
}
// skipcq: CRT-D0001
aopts := append(
acOpts,
grpc.WithErrGroup(eg))

discovererOpts, err := discovererOpts(cfg, dopts, aopts, eg)
if err != nil {
return nil, err
}

Check warning on line 106 in pkg/gateway/lb/usecase/vald.go

View check run for this annotation

Codecov / codecov/patch

pkg/gateway/lb/usecase/vald.go#L103-L106

Added lines #L103 - L106 were not covered by tests

client, err := discoverer.New(discovererOpts...)

Check warning on line 108 in pkg/gateway/lb/usecase/vald.go

View check run for this annotation

Codecov / codecov/patch

pkg/gateway/lb/usecase/vald.go#L108

Added line #L108 was not covered by tests
if err != nil {
return nil, err
Expand Down
94 changes: 94 additions & 0 deletions pkg/gateway/lb/usecase/vald_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,100 @@
// Package usecase represents gateways usecase layer
package usecase

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/vdaas/vald/internal/client/v1/client/discoverer"
iconfig "github.com/vdaas/vald/internal/config"
"github.com/vdaas/vald/internal/sync/errgroup"
"github.com/vdaas/vald/pkg/gateway/lb/config"

"github.com/vdaas/vald/internal/net/grpc"
)

func Test_discovererOpts(t *testing.T) {
tests := []struct {
name string
cfg *config.Data
dopts []grpc.Option
aopts []grpc.Option
assert func(*testing.T, []discoverer.Option, error)
}{
{
name: "Not create read replica client when read replica client option is not set",
cfg: &config.Data{
Gateway: &iconfig.LB{
AgentName: "agent",
AgentNamespace: "agent-ns",
AgentPort: 8081,
AgentDNS: "agent-dns",
Discoverer: &iconfig.DiscovererClient{
Duration: "1m",
},
NodeName: "node",
},
},
dopts: []grpc.Option{},
aopts: []grpc.Option{},
assert: func(t *testing.T, opts []discoverer.Option, err error) {
require.NoError(t, err)

client, err := discoverer.New(opts...)
require.NoError(t, err)

// check multiple times to ensure that the client is not a read replica client
require.Equal(t, client.GetClient(), client.GetReadClient())
require.Equal(t, client.GetClient(), client.GetReadClient())
require.Equal(t, client.GetClient(), client.GetReadClient())
},
},
{
name: "create read replica client when read replica client option is set",
cfg: &config.Data{
Gateway: &iconfig.LB{
AgentName: "agent",
AgentNamespace: "agent-ns",
AgentPort: 8081,
AgentDNS: "agent-dns",
Discoverer: &iconfig.DiscovererClient{
Duration: "1m",
},
NodeName: "node",
ReadReplicaClient: iconfig.ReadReplicaClient{
Client: &iconfig.GRPCClient{},
},
// set this to big enough value to ensure that the round robin counter won't reset to 0
ReadReplicaReplicas: 100,
},
},
dopts: []grpc.Option{},
aopts: []grpc.Option{},
assert: func(t *testing.T, opts []discoverer.Option, err error) {
require.NoError(t, err)

client, err := discoverer.New(opts...)
require.NoError(t, err)

// ensure that GetReadClient() returns a read replica client by calling it multiple times beforehand
// and increments the round robin counter
client.GetReadClient()
client.GetReadClient()
client.GetReadClient()

require.NotEqual(t, client.GetClient(), client.GetReadClient())
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts, err := discovererOpts(tt.cfg, tt.dopts, tt.aopts, errgroup.Get())
tt.assert(t, opts, err)
})
}
}

// NOT IMPLEMENTED BELOW
//
// func TestNew(t *testing.T) {
Expand Down

0 comments on commit 1f78faf

Please sign in to comment.