Skip to content

Commit

Permalink
feat: add mock for discoverer client and grpc client
Browse files Browse the repository at this point in the history
Signed-off-by: hlts2 <[email protected]>
  • Loading branch information
hlts2 committed Nov 8, 2023
1 parent 8f49b2c commit f2dc953
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
38 changes: 38 additions & 0 deletions internal/test/mock/discoverer_client_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (C) 2019-2023 vdaas.org vald team <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mock

import (
"context"

"github.com/vdaas/vald/internal/client/v1/client/discoverer"
"github.com/vdaas/vald/internal/net/grpc"
)

// DiscovererClientMock is the mock for discoverer client.
type DiscovererClientMock struct {
discoverer.Client
GetAddrsFunc func(ctx context.Context) []string
GetClientFunc func() grpc.Client
}

// GetAddrs calls the GetAddrsFunc object.
func (dc *DiscovererClientMock) GetAddrs(ctx context.Context) []string {
return dc.GetAddrsFunc(ctx)
}

// GetClient calls GetClientFunc object.
func (dc *DiscovererClientMock) GetClient() grpc.Client {
return dc.GetClientFunc()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,16 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package service
package mock

import (
"context"

"github.com/vdaas/vald/internal/client/v1/client/discoverer"
"github.com/vdaas/vald/internal/net/grpc"
)

type mockDiscovererClient struct {
discoverer.Client
GetAddrsFunc func(ctx context.Context) []string
GetClientFunc func() grpc.Client
}

func (mc *mockDiscovererClient) GetAddrs(ctx context.Context) []string {
return mc.GetAddrsFunc(ctx)
}

func (mc *mockDiscovererClient) GetClient() grpc.Client {
return mc.GetClientFunc()
}

type mockGrpcClient struct {
// GRPCClientMock is the mock for gRPC client.
type GRPCClientMock struct {
grpc.Client
OrderedRangeConcurrentFunc func(ctx context.Context,
order []string,
Expand All @@ -45,13 +31,14 @@ type mockGrpcClient struct {
copts ...grpc.CallOption) error) error
}

func (mc *mockGrpcClient) OrderedRangeConcurrent(ctx context.Context,
// OrderedRangeConcurrent calls the OrderedRangeConcurrentFunc object.
func (gc *GRPCClientMock) OrderedRangeConcurrent(ctx context.Context,
order []string,
concurrency int,
f func(ctx context.Context,
addr string,
conn *grpc.ClientConn,
copts ...grpc.CallOption) error,
) error {
return mc.OrderedRangeConcurrentFunc(ctx, order, concurrency, f)
return gc.OrderedRangeConcurrentFunc(ctx, order, concurrency, f)
}
15 changes: 8 additions & 7 deletions pkg/index/job/save/service/indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/vdaas/vald/internal/net/grpc/codes"
"github.com/vdaas/vald/internal/net/grpc/status"
"github.com/vdaas/vald/internal/test/goleak"
"github.com/vdaas/vald/internal/test/mock"
)

func Test_index_Start(t *testing.T) {
Expand Down Expand Up @@ -66,12 +67,12 @@ func Test_index_Start(t *testing.T) {
},

fields: fields{
client: &mockDiscovererClient{
client: &mock.DiscovererClientMock{
GetAddrsFunc: func(_ context.Context) []string {
return addrs
},
GetClientFunc: func() grpc.Client {
return &mockGrpcClient{
return &mock.GRPCClientMock{
OrderedRangeConcurrentFunc: func(_ context.Context, _ []string, _ int,
_ func(_ context.Context, _ string, _ *grpc.ClientConn, _ ...grpc.CallOption) error,
) error {
Expand All @@ -93,12 +94,12 @@ func Test_index_Start(t *testing.T) {
ctx: context.Background(),
},
fields: fields{
client: &mockDiscovererClient{
client: &mock.DiscovererClientMock{
GetAddrsFunc: func(_ context.Context) []string {
return addrs
},
GetClientFunc: func() grpc.Client {
return &mockGrpcClient{
return &mock.GRPCClientMock{
OrderedRangeConcurrentFunc: func(_ context.Context, _ []string, _ int,
_ func(_ context.Context, _ string, _ *grpc.ClientConn, _ ...grpc.CallOption) error,
) error {
Expand Down Expand Up @@ -128,12 +129,12 @@ func Test_index_Start(t *testing.T) {
},

fields: fields{
client: &mockDiscovererClient{
client: &mock.DiscovererClientMock{
GetAddrsFunc: func(_ context.Context) []string {
return addrs
},
GetClientFunc: func() grpc.Client {
return &mockGrpcClient{
return &mock.GRPCClientMock{
OrderedRangeConcurrentFunc: func(_ context.Context, _ []string, _ int,
_ func(_ context.Context, _ string, _ *grpc.ClientConn, _ ...grpc.CallOption) error,
) error {
Expand Down Expand Up @@ -162,7 +163,7 @@ func Test_index_Start(t *testing.T) {
ctx: context.Background(),
},
fields: fields{
client: &mockDiscovererClient{
client: &mock.DiscovererClientMock{
GetAddrsFunc: func(_ context.Context) []string {
// NOTE: This function returns nil, meaning that the targetAddrs stored in the field are invalid values.
return nil
Expand Down

0 comments on commit f2dc953

Please sign in to comment.