Skip to content

Commit

Permalink
fix: test case name and test case order
Browse files Browse the repository at this point in the history
Signed-off-by: hlts2 <[email protected]>
  • Loading branch information
hlts2 committed Oct 10, 2023
1 parent bed0a98 commit 06731a7
Showing 1 changed file with 75 additions and 77 deletions.
152 changes: 75 additions & 77 deletions pkg/gateway/mirror/handler/grpc/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,23 +744,35 @@ func Test_server_Update(t *testing.T) {
eg, egctx := errgroup.New(ctx)

uuid := "test"
loc := &payload.Object_Location{
Uuid: uuid,
Ips: []string{"127.0.0.1"},
}
targets := []string{
"vald-01", "vald-02",
"vald-01", "vald-02", "vald-03",
}
cmap := map[string]vald.ClientWithMirror{
targets[0]: &mockClient{
UpdateFunc: func(_ context.Context, _ *payload.Update_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return nil, status.Error(codes.NotFound, errors.ErrObjectIDNotFound(uuid).Error())
return loc, nil
},
},
targets[1]: &mockClient{
UpdateFunc: func(_ context.Context, _ *payload.Update_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return nil, status.Error(codes.NotFound, errors.ErrObjectIDNotFound(uuid).Error())
},
InsertFunc: func(_ context.Context, _ *payload.Insert_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return loc, nil
},
},
targets[2]: &mockClient{
UpdateFunc: func(_ context.Context, _ *payload.Update_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return loc, nil
},
},
}
return test{
name: "Fail: when the status codes are (NotFound, NotFound)",
name: "Success: when the last status codes are (OK, OK, OK) after inserting the target that returned NotFound",
args: args{
ctx: egctx,
req: &payload.Update_Request{
Expand All @@ -783,18 +795,32 @@ func Test_server_Update(t *testing.T) {
}
return nil
},
DoMultiFunc: func(ctx context.Context, targets []string, f func(ctx context.Context, target string, vc vald.ClientWithMirror, copts ...grpc.CallOption) error) error {
for _, target := range targets {
if c, ok := cmap[target]; !ok {
return errors.ErrTargetNotFound
} else {
f(ctx, target, c)
}
}
return nil
},
},
},
want: want{
err: status.Error(codes.NotFound, vald.UpdateRPCName+" API id "+uuid+" not found"),
wantLoc: &payload.Object_Location{
Uuid: uuid,
Ips: []string{
"127.0.0.1", "127.0.0.1", "127.0.0.1",
},
},
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
cancel()
},
}
}(),

func() test {
ctx, cancel := context.WithCancel(context.Background())
eg, egctx := errgroup.New(ctx)
Expand All @@ -805,7 +831,7 @@ func Test_server_Update(t *testing.T) {
Ips: []string{"127.0.0.1"},
}
targets := []string{
"vald-01", "vald-02",
"vald-01", "vald-02", "vald-03",
}
cmap := map[string]vald.ClientWithMirror{
targets[0]: &mockClient{
Expand All @@ -815,12 +841,20 @@ func Test_server_Update(t *testing.T) {
},
targets[1]: &mockClient{
UpdateFunc: func(_ context.Context, _ *payload.Update_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return nil, status.Error(codes.Internal, errors.ErrCircuitBreakerHalfOpenFlowLimitation.Error())
return nil, status.Error(codes.NotFound, errors.ErrObjectIDNotFound(uuid).Error())
},
InsertFunc: func(_ context.Context, _ *payload.Insert_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return loc, nil
},
},
targets[2]: &mockClient{
UpdateFunc: func(_ context.Context, _ *payload.Update_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return nil, status.Error(codes.AlreadyExists, errors.ErrMetaDataAlreadyExists(uuid).Error())
},
},
}
return test{
name: "Fail: when the status codes are (Internal, OK)",
name: "Success: when the last status codes are (OK, OK, AlreadyExists) after inserting the target that returned NotFound",
args: args{
ctx: egctx,
req: &payload.Update_Request{
Expand All @@ -843,10 +877,25 @@ func Test_server_Update(t *testing.T) {
}
return nil
},
DoMultiFunc: func(ctx context.Context, targets []string, f func(ctx context.Context, target string, vc vald.ClientWithMirror, copts ...grpc.CallOption) error) error {
for _, target := range targets {
if c, ok := cmap[target]; !ok {
return errors.ErrTargetNotFound
} else {
f(ctx, target, c)
}
}
return nil
},
},
},
want: want{
err: status.Error(codes.Internal, errors.ErrCircuitBreakerHalfOpenFlowLimitation.Error()),
wantLoc: &payload.Object_Location{
Uuid: uuid,
Ips: []string{
"127.0.0.1", "127.0.0.1",
},
},
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
Expand All @@ -865,17 +914,17 @@ func Test_server_Update(t *testing.T) {
cmap := map[string]vald.ClientWithMirror{
targets[0]: &mockClient{
UpdateFunc: func(_ context.Context, _ *payload.Update_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return nil, status.Error(codes.Internal, errors.ErrCircuitBreakerHalfOpenFlowLimitation.Error())
return nil, status.Error(codes.NotFound, errors.ErrObjectIDNotFound(uuid).Error())
},
},
targets[1]: &mockClient{
UpdateFunc: func(_ context.Context, _ *payload.Update_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return nil, status.Error(codes.AlreadyExists, errors.ErrMetaDataAlreadyExists(uuid).Error())
return nil, status.Error(codes.NotFound, errors.ErrObjectIDNotFound(uuid).Error())
},
},
}
return test{
name: "Fail: when the status codes are (Internal, AlreadyExists)",
name: "Fail: when the status codes are (NotFound, NotFound)",
args: args{
ctx: egctx,
req: &payload.Update_Request{
Expand All @@ -901,10 +950,7 @@ func Test_server_Update(t *testing.T) {
},
},
want: want{
err: status.Error(codes.Internal, errors.Join(
status.Error(codes.Internal, errors.ErrCircuitBreakerHalfOpenFlowLimitation.Error()),
status.Error(codes.AlreadyExists, errors.ErrMetaDataAlreadyExists(uuid).Error()),
).Error()),
err: status.Error(codes.NotFound, vald.UpdateRPCName+" API id "+uuid+" not found"),
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
Expand All @@ -922,7 +968,7 @@ func Test_server_Update(t *testing.T) {
Ips: []string{"127.0.0.1"},
}
targets := []string{
"vald-01", "vald-02", "vald-03",
"vald-01", "vald-02",
}
cmap := map[string]vald.ClientWithMirror{
targets[0]: &mockClient{
Expand All @@ -932,20 +978,12 @@ func Test_server_Update(t *testing.T) {
},
targets[1]: &mockClient{
UpdateFunc: func(_ context.Context, _ *payload.Update_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return nil, status.Error(codes.NotFound, errors.ErrObjectIDNotFound(uuid).Error())
},
InsertFunc: func(_ context.Context, _ *payload.Insert_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return loc, nil
},
},
targets[2]: &mockClient{
UpdateFunc: func(_ context.Context, _ *payload.Update_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return loc, nil
return nil, status.Error(codes.Internal, errors.ErrCircuitBreakerHalfOpenFlowLimitation.Error())
},
},
}
return test{
name: "Success: when the last status codes are (OK, OK, OK) after inserting the target that returned NotFound",
name: "Fail: when the status codes are (Internal, OK)",
args: args{
ctx: egctx,
req: &payload.Update_Request{
Expand All @@ -968,25 +1006,10 @@ func Test_server_Update(t *testing.T) {
}
return nil
},
DoMultiFunc: func(ctx context.Context, targets []string, f func(ctx context.Context, target string, vc vald.ClientWithMirror, copts ...grpc.CallOption) error) error {
for _, target := range targets {
if c, ok := cmap[target]; !ok {
return errors.ErrTargetNotFound
} else {
f(ctx, target, c)
}
}
return nil
},
},
},
want: want{
wantLoc: &payload.Object_Location{
Uuid: uuid,
Ips: []string{
"127.0.0.1", "127.0.0.1", "127.0.0.1",
},
},
err: status.Error(codes.Internal, errors.ErrCircuitBreakerHalfOpenFlowLimitation.Error()),
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
Expand All @@ -999,35 +1022,23 @@ func Test_server_Update(t *testing.T) {
eg, egctx := errgroup.New(ctx)

uuid := "test"
loc := &payload.Object_Location{
Uuid: uuid,
Ips: []string{"127.0.0.1"},
}
targets := []string{
"vald-01", "vald-02", "vald-03",
"vald-01", "vald-02",
}
cmap := map[string]vald.ClientWithMirror{
targets[0]: &mockClient{
UpdateFunc: func(_ context.Context, _ *payload.Update_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return loc, nil
return nil, status.Error(codes.Internal, errors.ErrCircuitBreakerHalfOpenFlowLimitation.Error())
},
},
targets[1]: &mockClient{
UpdateFunc: func(_ context.Context, _ *payload.Update_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return nil, status.Error(codes.NotFound, errors.ErrObjectIDNotFound(uuid).Error())
},
InsertFunc: func(_ context.Context, _ *payload.Insert_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return loc, nil
},
},
targets[2]: &mockClient{
UpdateFunc: func(_ context.Context, _ *payload.Update_Request, _ ...grpc.CallOption) (*payload.Object_Location, error) {
return nil, status.Error(codes.AlreadyExists, errors.ErrMetaDataAlreadyExists(uuid).Error())
},
},
}
return test{
name: "Success: when the last status codes are (OK, OK, AlreadyExists) after inserting the target that returned NotFound",
name: "Fail: when the status codes are (Internal, AlreadyExists)",
args: args{
ctx: egctx,
req: &payload.Update_Request{
Expand All @@ -1050,25 +1061,13 @@ func Test_server_Update(t *testing.T) {
}
return nil
},
DoMultiFunc: func(ctx context.Context, targets []string, f func(ctx context.Context, target string, vc vald.ClientWithMirror, copts ...grpc.CallOption) error) error {
for _, target := range targets {
if c, ok := cmap[target]; !ok {
return errors.ErrTargetNotFound
} else {
f(ctx, target, c)
}
}
return nil
},
},
},
want: want{
wantLoc: &payload.Object_Location{
Uuid: uuid,
Ips: []string{
"127.0.0.1", "127.0.0.1",
},
},
err: status.Error(codes.Internal, errors.Join(
status.Error(codes.Internal, errors.ErrCircuitBreakerHalfOpenFlowLimitation.Error()),
status.Error(codes.AlreadyExists, errors.ErrMetaDataAlreadyExists(uuid).Error()),
).Error()),
},
afterFunc: func(t *testing.T, args args) {
t.Helper()
Expand Down Expand Up @@ -1149,7 +1148,6 @@ func Test_server_Update(t *testing.T) {
},
}
}(),

func() test {
ctx, cancel := context.WithCancel(context.Background())
eg, egctx := errgroup.New(ctx)
Expand Down Expand Up @@ -2207,7 +2205,7 @@ func Test_server_RemoveByTimestamp(t *testing.T) {
},
}
return test{
name: "Success: when the status codes are (Internal, Internal)",
name: "Fail: when the status codes are (Internal, Internal)",
args: args{
ctx: egctx,
req: defaultRemoveByTimestampReq,
Expand Down Expand Up @@ -2260,7 +2258,7 @@ func Test_server_RemoveByTimestamp(t *testing.T) {
},
}
return test{
name: "Success: when the status codes are (NotFound, NotFound)",
name: "Fail: when the status codes are (NotFound, NotFound)",
args: args{
ctx: egctx,
req: defaultRemoveByTimestampReq,
Expand Down

0 comments on commit 06731a7

Please sign in to comment.