Welcome to mirror list, hosted at ThFree Co, Russian Federation.

statushandler_test.go « statushandler « middleware « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c3b183c8ab800a478c931a1b5e66803c10493a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package statushandler

import (
	"context"
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
	"gitlab.com/gitlab-org/gitaly/v16/internal/structerr"
	"gitlab.com/gitlab-org/gitaly/v16/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.Internal, assert.AnError.Error()),
		},
		"wrapped error": {
			ctx:         ctx,
			err:         structerr.NewInvalidArgument("%w", assert.AnError),
			expectedErr: status.Error(codes.InvalidArgument, assert.AnError.Error()),
		},
		"formatted wrapped error": {
			ctx:         ctx,
			err:         fmt.Errorf("cause: %w", structerr.NewInvalidArgument("%w", assert.AnError)),
			expectedErr: status.Error(codes.InvalidArgument, "cause: "+assert.AnError.Error()),
		},
		"cancelled error": {
			ctx:         ctx,
			err:         context.Canceled,
			expectedErr: status.Error(codes.Internal, context.Canceled.Error()),
		},
		"timeout error": {
			ctx:         ctx,
			err:         context.DeadlineExceeded,
			expectedErr: status.Error(codes.Internal, 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.Internal, assert.AnError.Error()),
		},
		"wrapped error": {
			ctx:         ctx,
			err:         structerr.NewInvalidArgument("%w", assert.AnError),
			expectedErr: status.Error(codes.InvalidArgument, assert.AnError.Error()),
		},
		"formatted wrapped error": {
			ctx:         ctx,
			err:         fmt.Errorf("cause: %w", structerr.NewInvalidArgument("%w", assert.AnError)),
			expectedErr: status.Error(codes.InvalidArgument, "cause: "+assert.AnError.Error()),
		},
		"cancelled error": {
			ctx:         ctx,
			err:         context.Canceled,
			expectedErr: status.Error(codes.Internal, context.Canceled.Error()),
		},
		"timeout error": {
			ctx:         ctx,
			err:         context.DeadlineExceeded,
			expectedErr: status.Error(codes.Internal, 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
}