Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement stream insert test case #1697

Merged
merged 13 commits into from
Jun 23, 2022
18 changes: 18 additions & 0 deletions internal/test/data/request/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,21 @@ func GenObjectLocations(num int, name string, ipAddr string) *payload.Object_Loc
}
return result
}

// GenObjectStreamLocation generate ObjectStreamLocations payload with multiple name and ip with generated uuid.
func GenObjectStreamLocation(num int, name string, ipAddr string) []*payload.Object_StreamLocation {
result := make([]*payload.Object_StreamLocation, num)

for i := 0; i < num; i++ {
result[i] = &payload.Object_StreamLocation{
Payload: &payload.Object_StreamLocation_Location{
Location: &payload.Object_Location{
Name: name,
Uuid: "uuid-" + strconv.Itoa(i+1),
Ips: []string{ipAddr},
},
},
}
}
return result
}
141 changes: 141 additions & 0 deletions internal/test/data/request/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package request

import (
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -151,3 +152,143 @@ func TestGenObjectLocations(t *testing.T) {
})
}
}

func TestGenObjectStreamLocation(t *testing.T) {
type args struct {
num int
name string
ipAddr string
}
type want struct {
want []*payload.Object_StreamLocation
}
type test struct {
name string
args args
want want
checkFunc func(want, []*payload.Object_StreamLocation) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got []*payload.Object_StreamLocation) error {
if !reflect.DeepEqual(got, w.want) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}
tests := []test{
{
name: "success to generate 1 object stream location",
args: args{
num: 1,
name: "vald-agent-01",
ipAddr: "127.0.0.1",
},
want: want{
want: []*payload.Object_StreamLocation{
{
Payload: &payload.Object_StreamLocation_Location{
Location: &payload.Object_Location{
Name: "vald-agent-01",
Uuid: "uuid-1",
Ips: []string{"127.0.0.1"},
},
},
},
},
},
},
{
name: "success to generate 5 object stream location",
args: args{
num: 5,
name: "vald-agent-01",
ipAddr: "127.0.0.1",
},
want: want{
want: []*payload.Object_StreamLocation{
{
Payload: &payload.Object_StreamLocation_Location{
Location: &payload.Object_Location{
Name: "vald-agent-01",
Uuid: "uuid-1",
Ips: []string{"127.0.0.1"},
},
},
},
{
Payload: &payload.Object_StreamLocation_Location{
Location: &payload.Object_Location{
Name: "vald-agent-01",
Uuid: "uuid-2",
Ips: []string{"127.0.0.1"},
},
},
},
{
Payload: &payload.Object_StreamLocation_Location{
Location: &payload.Object_Location{
Name: "vald-agent-01",
Uuid: "uuid-3",
Ips: []string{"127.0.0.1"},
},
},
},
{
Payload: &payload.Object_StreamLocation_Location{
Location: &payload.Object_Location{
Name: "vald-agent-01",
Uuid: "uuid-4",
Ips: []string{"127.0.0.1"},
},
},
},
{
Payload: &payload.Object_StreamLocation_Location{
Location: &payload.Object_Location{
Name: "vald-agent-01",
Uuid: "uuid-5",
Ips: []string{"127.0.0.1"},
},
},
},
},
},
},
{
name: "success to generate 0 object stream location",
args: args{
num: 0,
name: "vald-agent-01",
ipAddr: "127.0.0.1",
},
want: want{
want: []*payload.Object_StreamLocation{},
},
},
}

for _, tc := range tests {
test := tc
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}
checkFunc := test.checkFunc
if test.checkFunc == nil {
checkFunc = defaultCheckFunc
}

got := GenObjectStreamLocation(test.args.num, test.args.name, test.args.ipAddr)
if err := checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}

kevindiu marked this conversation as resolved.
Show resolved Hide resolved
})
}
}
18 changes: 18 additions & 0 deletions internal/test/mock/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Copyright (C) 2019-2022 vdaas.org vald team <vald@vdaas.org>
//
// 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 provides mock implementation for testing.
package mock
72 changes: 72 additions & 0 deletions internal/test/mock/server_stream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
kevindiu marked this conversation as resolved.
Show resolved Hide resolved
// Copyright (C) 2019-2022 vdaas.org vald team <vald@vdaas.org>
//
// 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
kevindiu marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"

"github.com/vdaas/vald/apis/grpc/v1/payload"
"github.com/vdaas/vald/internal/net/grpc"
"google.golang.org/grpc/metadata"
)

type StreamInsertServerMock struct {
SendFunc func(*payload.Object_StreamLocation) error
RecvFunc func() (*payload.Insert_Request, error)
grpc.ServerStream
}

func (m *StreamInsertServerMock) Send(l *payload.Object_StreamLocation) error {
return m.SendFunc(l)
}

func (m *StreamInsertServerMock) Recv() (*payload.Insert_Request, error) {
return m.RecvFunc()
}

// ServerStreamMock implements grpc.ServerStream mock implementation.
type ServerStreamMock struct {
SetHeaderFunc func(metadata.MD) error
SendHeaderFunc func(metadata.MD) error
SetTrailerFunc func(metadata.MD)
ContextFunc func() context.Context
SendMsgFunc func(interface{}) error
RecvMsgFunc func(interface{}) error
}

func (m *ServerStreamMock) SetHeader(md metadata.MD) error {
return m.SetHeaderFunc(md)
}

func (m *ServerStreamMock) SendHeader(md metadata.MD) error {
return m.SendHeaderFunc(md)
}

func (m *ServerStreamMock) SetTrailer(md metadata.MD) {
m.SetTrailerFunc(md)
}

func (m *ServerStreamMock) Context() context.Context {
return m.ContextFunc()
}

func (m *ServerStreamMock) SendMsg(msg interface{}) error {
return m.SendMsgFunc(msg)
}

func (m *ServerStreamMock) RecvMsg(msg interface{}) error {
return m.RecvMsgFunc(msg)
}
Loading