From 43aaad1399a6a146e889cc98e9cea18b0a38e419 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Fri, 19 Jan 2024 01:32:25 +0000 Subject: [PATCH] Refactor discovererOpts function to return discoverer client --- pkg/gateway/lb/usecase/vald.go | 10 +++------- pkg/gateway/lb/usecase/vald_test.go | 18 ++++++------------ 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/pkg/gateway/lb/usecase/vald.go b/pkg/gateway/lb/usecase/vald.go index dfc47400f2f..b8dc5e39484 100644 --- a/pkg/gateway/lb/usecase/vald.go +++ b/pkg/gateway/lb/usecase/vald.go @@ -46,7 +46,7 @@ type run struct { gateway service.Gateway } -func discovererOpts(cfg *config.Data, dopts []grpc.Option, aopts []grpc.Option, eg errgroup.Group) ([]discoverer.Option, error) { +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), @@ -74,7 +74,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) { @@ -100,15 +100,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) 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), diff --git a/pkg/gateway/lb/usecase/vald_test.go b/pkg/gateway/lb/usecase/vald_test.go index be890d2ae56..68889af47b1 100644 --- a/pkg/gateway/lb/usecase/vald_test.go +++ b/pkg/gateway/lb/usecase/vald_test.go @@ -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", @@ -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 @@ -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 @@ -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) }) } }