diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2022-11-04 09:35:02 +0300 |
---|---|---|
committer | Pavlo Strokov <pstrokov@gitlab.com> | 2022-11-08 13:45:57 +0300 |
commit | eb305642899d59cb5e58cd888047eedd06c05dce (patch) | |
tree | 704745e333f08584d832830986a11ac8dc9bb958 /internal/middleware | |
parent | 7a8f7c377bd013483aba14ced8eafd073c631d4a (diff) |
cancelhandler: Extend test coverage
The package doesn't have any tests. As it is going to be
changed we extend test coverage to make sure nothing is
broken.
Part of: https://gitlab.com/gitlab-org/gitaly/-/issues/4587
Diffstat (limited to 'internal/middleware')
-rw-r--r-- | internal/middleware/cancelhandler/cancelhandler_test.go | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/internal/middleware/cancelhandler/cancelhandler_test.go b/internal/middleware/cancelhandler/cancelhandler_test.go new file mode 100644 index 000000000..9bd701930 --- /dev/null +++ b/internal/middleware/cancelhandler/cancelhandler_test.go @@ -0,0 +1,159 @@ +package cancelhandler + +import ( + "context" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "gitlab.com/gitlab-org/gitaly/v15/internal/helper" + "gitlab.com/gitlab-org/gitaly/v15/internal/testhelper" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +func TestUnary(t *testing.T) { + t.Parallel() + ctx := testhelper.Context(t) + cancelledCtx, cancel := context.WithCancel(ctx) + cancel() + timeoutCtx, timeout := context.WithTimeout(ctx, 0) //nolint:forbidigo + timeout() + + for desc, tc := range map[string]struct { + ctx context.Context + err error + expectedErr error + }{ + "context cancelled": { + ctx: cancelledCtx, + }, + "context timeout": { + ctx: timeoutCtx, + }, + "context cancelled with an error returned": { + ctx: cancelledCtx, + err: assert.AnError, + expectedErr: status.Error(codes.Canceled, assert.AnError.Error()), + }, + "context timed out with an error returned": { + ctx: timeoutCtx, + err: assert.AnError, + expectedErr: status.Error(codes.DeadlineExceeded, assert.AnError.Error()), + }, + "bare error": { + ctx: ctx, + err: assert.AnError, + expectedErr: status.Error(codes.Unknown, assert.AnError.Error()), + }, + "wrapped error": { + ctx: ctx, + err: helper.ErrInvalidArgument(assert.AnError), + expectedErr: status.Error(codes.InvalidArgument, assert.AnError.Error()), + }, + "formatted wrapped error": { + ctx: ctx, + err: fmt.Errorf("cause: %w", helper.ErrInvalidArgument(assert.AnError)), + expectedErr: status.Error(codes.Unknown, "cause: "+assert.AnError.Error()), + }, + "cancelled error": { + ctx: ctx, + err: context.Canceled, + expectedErr: status.Error(codes.Unknown, context.Canceled.Error()), + }, + "timeout error": { + ctx: ctx, + err: context.DeadlineExceeded, + expectedErr: status.Error(codes.Unknown, context.DeadlineExceeded.Error()), + }, + "no errors": { + ctx: ctx, + expectedErr: status.New(codes.OK, "").Err(), + }, + } { + t.Run(desc, func(t *testing.T) { + _, err := Unary(tc.ctx, nil, nil, func(context.Context, interface{}) (interface{}, error) { + return nil, tc.err + }) + testhelper.RequireGrpcError(t, tc.expectedErr, err) + }) + } +} + +func TestStream(t *testing.T) { + t.Parallel() + ctx := testhelper.Context(t) + cancelledCtx, cancel := context.WithCancel(ctx) + cancel() + timedoutCtx, timeout := context.WithTimeout(ctx, 0) //nolint:forbidigo + timeout() + + for desc, tc := range map[string]struct { + ctx context.Context + err error + expectedErr error + }{ + "context cancelled": { + ctx: cancelledCtx, + }, + "context timed out": { + ctx: timedoutCtx, + }, + "context cancelled with an error returned": { + ctx: cancelledCtx, + err: assert.AnError, + expectedErr: status.Error(codes.Canceled, assert.AnError.Error()), + }, + "context timed out with an error returned": { + ctx: timedoutCtx, + err: assert.AnError, + expectedErr: status.Error(codes.DeadlineExceeded, assert.AnError.Error()), + }, + "bare error": { + ctx: ctx, + err: assert.AnError, + expectedErr: status.Error(codes.Unknown, assert.AnError.Error()), + }, + "wrapped error": { + ctx: ctx, + err: helper.ErrInvalidArgument(assert.AnError), + expectedErr: status.Error(codes.InvalidArgument, assert.AnError.Error()), + }, + "formatted wrapped error": { + ctx: ctx, + err: fmt.Errorf("cause: %w", helper.ErrInvalidArgument(assert.AnError)), + expectedErr: status.Error(codes.Unknown, "cause: "+assert.AnError.Error()), + }, + "cancelled error": { + ctx: ctx, + err: context.Canceled, + expectedErr: status.Error(codes.Unknown, context.Canceled.Error()), + }, + "timeout error": { + ctx: ctx, + err: context.DeadlineExceeded, + expectedErr: status.Error(codes.Unknown, context.DeadlineExceeded.Error()), + }, + "no errors": { + ctx: ctx, + expectedErr: status.New(codes.OK, "").Err(), + }, + } { + t.Run(desc, func(t *testing.T) { + err := Stream(nil, serverStream{ctx: tc.ctx}, nil, func(srv interface{}, stream grpc.ServerStream) error { + return tc.err + }) + testhelper.RequireGrpcError(t, tc.expectedErr, err) + }) + } +} + +type serverStream struct { + ctx context.Context + grpc.ServerStream +} + +func (ss serverStream) Context() context.Context { + return ss.ctx +} |