Skip to content

Commit

Permalink
Refactor discovererOpts function to return discoverer client
Browse files Browse the repository at this point in the history
  • Loading branch information
ykadowak committed Jan 19, 2024
1 parent 1f78faf commit bf3d7a6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
11 changes: 4 additions & 7 deletions pkg/gateway/lb/usecase/vald.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ type run struct {
gateway service.Gateway
}

func discovererOpts(cfg *config.Data, dopts []grpc.Option, aopts []grpc.Option, eg errgroup.Group) ([]discoverer.Option, error) {
// FIXME: optsを返すよりdiscoverer clientを生成してそれを返せば良い
func discovererClient(cfg *config.Data, dopts []grpc.Option, aopts []grpc.Option, eg errgroup.Group) (discoverer.Client, error) {
var discovererOpts []discoverer.Option
discovererOpts = append(discovererOpts,
discoverer.WithAutoConnect(true),
Expand Down Expand Up @@ -74,7 +75,7 @@ func discovererOpts(cfg *config.Data, dopts []grpc.Option, aopts []grpc.Option,
discovererOpts = append(discovererOpts, discoverer.WithReadReplicaClient(grpc.New(rrOpts...)))
}

return discovererOpts, nil
return discoverer.New(discovererOpts...)
}

func New(cfg *config.Data) (r runner.Runner, err error) {
Expand All @@ -100,15 +101,11 @@ func New(cfg *config.Data) (r runner.Runner, err error) {
acOpts,
grpc.WithErrGroup(eg))

discovererOpts, err := discovererOpts(cfg, dopts, aopts, eg)
client, err := discovererClient(cfg, dopts, aopts, eg)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L104 was not covered by tests
if err != nil {
return nil, err
}

client, err := discoverer.New(discovererOpts...)
if err != nil {
return nil, err
}
gateway, err = service.NewGateway(
service.WithErrGroup(eg),
service.WithDiscoverer(client),
Expand Down
18 changes: 6 additions & 12 deletions pkg/gateway/lb/usecase/vald_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ import (
"github.com/vdaas/vald/internal/net/grpc"
)

func Test_discovererOpts(t *testing.T) {
func Test_discovererClient(t *testing.T) {
tests := []struct {
name string
cfg *config.Data
dopts []grpc.Option
aopts []grpc.Option
assert func(*testing.T, []discoverer.Option, error)
assert func(*testing.T, discoverer.Client, error)
}{
{
name: "Not create read replica client when read replica client option is not set",
Expand All @@ -53,10 +53,7 @@ func Test_discovererOpts(t *testing.T) {
},
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...)
assert: func(t *testing.T, client discoverer.Client, err error) {
require.NoError(t, err)

// check multiple times to ensure that the client is not a read replica client
Expand Down Expand Up @@ -86,10 +83,7 @@ func Test_discovererOpts(t *testing.T) {
},
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...)
assert: func(t *testing.T, client discoverer.Client, err error) {
require.NoError(t, err)

// ensure that GetReadClient() returns a read replica client by calling it multiple times beforehand
Expand All @@ -105,8 +99,8 @@ func Test_discovererOpts(t *testing.T) {

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)
client, err := discovererClient(tt.cfg, tt.dopts, tt.aopts, errgroup.Get())
tt.assert(t, client, err)
})
}
}
Expand Down

0 comments on commit bf3d7a6

Please sign in to comment.