-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver_expectation_bidirectional_stream.go
323 lines (283 loc) · 10.5 KB
/
server_expectation_bidirectional_stream.go
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package grpcmock
import (
"context"
"fmt"
"sync"
"time"
"github.com/spf13/afero"
"go.nhat.io/matcher/v2"
"go.nhat.io/wait"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
xerrors "go.nhat.io/grpcmock/errors"
xmatcher "go.nhat.io/grpcmock/matcher"
"go.nhat.io/grpcmock/planner"
"go.nhat.io/grpcmock/service"
"go.nhat.io/grpcmock/streamer"
)
// BidirectionalStreamExpectation represents the expectation for a client-stream request.
//
// nolint: interfacebloat
type BidirectionalStreamExpectation interface {
// WithHeader sets an expected header of the given request.
//
// Server.ExpectBidirectionalStream("grpctest.Service/TransformItems").
// WithHeader("Locale", "en-US")
//
// See: BidirectionalStreamExpectation.WithHeaders().
WithHeader(header string, value any) BidirectionalStreamExpectation
// WithHeaders sets a list of expected headers of the given request.
//
// Server.ExpectBidirectionalStream("grpctest.Service/TransformItems").
// WithHeaders(map[string]any{"Locale": "en-US"})
//
// See: BidirectionalStreamExpectation.WithHeader().
WithHeaders(headers map[string]any) BidirectionalStreamExpectation
// ReturnCode sets the response code.
//
// Server.ExpectBidirectionalStream("grpc.Service/TransformItems").
// ReturnCode(codes.OK)
//
// See: BidirectionalStreamExpectation.ReturnErrorMessage(), BidirectionalStreamExpectation.ReturnError(), BidirectionalStreamExpectation.ReturnErrorf().
ReturnCode(code codes.Code)
// ReturnErrorMessage sets the response error message.
//
// Server.ExpectBidirectionalStream("grpc.Service/TransformItems").
// ReturnErrorMessage("Internal Server Error")
//
// See: BidirectionalStreamExpectation.ReturnCode(), BidirectionalStreamExpectation.ReturnError(), BidirectionalStreamExpectation.ReturnErrorf().
ReturnErrorMessage(msg string)
// ReturnError sets the response error.
//
// Server.ExpectBidirectionalStream("grpc.Service/TransformItems").
// ReturnError(codes.Internal, "Internal Server Error")
//
// See: BidirectionalStreamExpectation.ReturnCode(), BidirectionalStreamExpectation.ReturnErrorMessage(), BidirectionalStreamExpectation.ReturnErrorf().
ReturnError(code codes.Code, msg string)
// ReturnErrorf sets the response error.
//
// Server.ExpectBidirectionalStream("grpc.Service/TransformItems").
// ReturnErrorf(codes.NotFound, "Item %d not found", 42)
//
// See: BidirectionalStreamExpectation.ReturnCode(), BidirectionalStreamExpectation.ReturnErrorMessage(), BidirectionalStreamExpectation.ReturnError().
ReturnErrorf(code codes.Code, format string, args ...any)
// Run sets a custom handler to handle the given request.
//
// Server.ExpectBidirectionalStream("grpc.Service/TransformItems").
// Run(func(context.Context, grpc.ServerStream) error {
// return nil
// })
Run(handler func(ctx context.Context, s grpc.ServerStream) error)
// Once indicates that the mock should only return the value once.
//
// Server.ExpectBidirectionalStream("grpctest.Service/TransformItems").
// Once().
// Run(func(context.Context, grpc.ServerStream) error {
// return nil
// })
//
// See: BidirectionalStreamExpectation.Twice(), BidirectionalStreamExpectation.UnlimitedTimes(), BidirectionalStreamExpectation.Times().
Once() BidirectionalStreamExpectation
// Twice indicates that the mock should only return the value twice.
//
// Server.ExpectBidirectionalStream("grpctest.Service/TransformItems").
// Twice().
// Run(func(context.Context, grpc.ServerStream) error {
// return nil
// })
//
// See: BidirectionalStreamExpectation.Once(), BidirectionalStreamExpectation.UnlimitedTimes(), BidirectionalStreamExpectation.Times().
Twice() BidirectionalStreamExpectation
// UnlimitedTimes indicates that the mock should return the value at least once and there is no max limit in the number
// of return.
//
// Server.ExpectBidirectionalStream("grpctest.Service/TransformItems").
// UnlimitedTimes().
// Run(func(context.Context, grpc.ServerStream) error {
// return nil
// })
//
// See: BidirectionalStreamExpectation.Once(), BidirectionalStreamExpectation.Twice(), BidirectionalStreamExpectation.Times().
UnlimitedTimes() BidirectionalStreamExpectation
// Times indicates that the mock should only return the indicated number of times.
//
// Server.ExpectBidirectionalStream("grpctest.Service/TransformItems").
// Times(5).
// Run(func(context.Context, grpc.ServerStream) error {
// return nil
// })
//
// See: BidirectionalStreamExpectation.Once(), BidirectionalStreamExpectation.Twice(), BidirectionalStreamExpectation.UnlimitedTimes().
Times(i uint) BidirectionalStreamExpectation
// WaitUntil sets the channel that will block the mocked return until its closed
// or a message is received.
//
// Server.ExpectBidirectionalStream("grpctest.Service/TransformItems").
// WaitUntil(time.After(time.Second)).
// Run(func(context.Context, grpc.ServerStream) error {
// return nil
// })
//
// See: BidirectionalStreamExpectation.After()
WaitUntil(w <-chan time.Time) BidirectionalStreamExpectation
// After sets how long to block until the call returns.
//
// Server.ExpectBidirectionalStream("grpctest.Service/TransformItems").
// After(time.Second).
// Run(func(context.Context, grpc.ServerStream) error {
// return nil
// })
//
// See: BidirectionalStreamExpectation.WaitUntil()
After(d time.Duration) BidirectionalStreamExpectation
}
type bidirectionalStreamExpectation struct {
*baseExpectation
// Request handler.
run func(ctx context.Context, s grpc.ServerStream) error
}
func (e *bidirectionalStreamExpectation) WithHeader(header string, value any) BidirectionalStreamExpectation {
e.lock()
defer e.unlock()
if e.requestHeader == nil {
e.requestHeader = xmatcher.HeaderMatcher{}
}
e.requestHeader[header] = matcher.Match(value)
return e
}
func (e *bidirectionalStreamExpectation) WithHeaders(headers map[string]any) BidirectionalStreamExpectation {
for header, value := range headers {
e.WithHeader(header, value)
}
return e
}
func (e *bidirectionalStreamExpectation) ReturnCode(code codes.Code) {
e.lock()
defer e.unlock()
e.statusCode = code
if code == codes.OK {
e.statusMessage = ""
}
}
func (e *bidirectionalStreamExpectation) ReturnErrorMessage(msg string) {
e.lock()
defer e.unlock()
e.statusMessage = msg
if e.statusCode == codes.OK {
e.statusCode = codes.Internal
}
}
func (e *bidirectionalStreamExpectation) ReturnError(code codes.Code, msg string) {
e.ReturnErrorMessage(msg)
e.ReturnCode(code)
}
func (e *bidirectionalStreamExpectation) ReturnErrorf(code codes.Code, format string, args ...any) {
e.ReturnErrorMessage(fmt.Sprintf(format, args...))
e.ReturnCode(code)
}
func (e *bidirectionalStreamExpectation) Run(handler func(ctx context.Context, s grpc.ServerStream) error) {
e.lock()
defer e.unlock()
e.run = handler
}
func (e *bidirectionalStreamExpectation) Handle(ctx context.Context, in any, _ any) error {
if err := e.waiter.Wait(ctx); err != nil {
return xerrors.StatusError(err)
}
if e.statusCode != codes.OK {
return status.Error(e.statusCode, e.statusMessage)
}
return xerrors.StatusError(e.run(ctx, in.(*streamer.BidirectionalStreamer)))
}
// Once indicates that the mock should only return the value once.
//
// Server.ExpectBidirectionalStream("grpctest.Service/TransformItems").
// Once().
// Run(func(context.Context, grpc.ServerStream) error {
// return nil
// })
//
// See: bidirectionalStreamExpectation.Twice(), BidirectionalStreamRequest.UnlimitedTimes(), bidirectionalStreamExpectation.Times().
func (e *bidirectionalStreamExpectation) Once() BidirectionalStreamExpectation {
return e.Times(1)
}
// Twice indicates that the mock should only return the value twice.
//
// Server.ExpectBidirectionalStream("grpctest.Service/TransformItems").
// Twice().
// Run(func(context.Context, grpc.ServerStream) error {
// return nil
// })
//
// See: BidirectionalStreamRequest.Once(), bidirectionalStreamExpectation.UnlimitedTimes(), BidirectionalStreamRequest.Times().
func (e *bidirectionalStreamExpectation) Twice() BidirectionalStreamExpectation {
return e.Times(2)
}
// UnlimitedTimes indicates that the mock should return the value at least once and there is no max limit in the number
// of return.
//
// Server.ExpectBidirectionalStream("grpctest.Service/TransformItems").
// UnlimitedTimes().
// Run(func(context.Context, grpc.ServerStream) error {
// return nil
// })
//
// See: bidirectionalStreamExpectation.Once(), BidirectionalStreamRequest.Twice(), BidirectionalStreamRequest.Times().
func (e *bidirectionalStreamExpectation) UnlimitedTimes() BidirectionalStreamExpectation {
return e.Times(planner.UnlimitedTimes)
}
// Times indicates that the mock should only return the indicated number of times.
//
// Server.ExpectBidirectionalStream("grpctest.Service/TransformItems").
// Times(5).
// Run(func(context.Context, grpc.ServerStream) error {
// return nil
// })
//
// See: bidirectionalStreamExpectation.Once(), BidirectionalStreamRequest.Twice(), BidirectionalStreamRequest.UnlimitedTimes().
func (e *bidirectionalStreamExpectation) Times(i uint) BidirectionalStreamExpectation {
e.withTimes(i)
return e
}
// WaitUntil sets the channel that will block the mocked return until its closed
// or a message is received.
//
// Server.ExpectBidirectionalStream("grpctest.Service/TransformItems").
// WaitUntil(time.After(time.Second)).
// Run(func(context.Context, grpc.ServerStream) error {
// return nil
// })
func (e *bidirectionalStreamExpectation) WaitUntil(w <-chan time.Time) BidirectionalStreamExpectation {
e.lock()
defer e.unlock()
e.waiter = wait.ForSignal(w)
return e
}
// After sets how long to block until the call returns.
//
// Server.ExpectBidirectionalStream("grpctest.Service/TransformItems").
// After(time.Second).
// Run(func(context.Context, grpc.ServerStream) error {
// return nil
// })
func (e *bidirectionalStreamExpectation) After(d time.Duration) BidirectionalStreamExpectation {
e.lock()
defer e.unlock()
e.waiter = wait.ForDuration(d)
return e
}
// newBidirectionalStreamExpectation creates a new client-stream expectation.
func newBidirectionalStreamExpectation(svc *service.Method) *bidirectionalStreamExpectation {
return &bidirectionalStreamExpectation{
baseExpectation: &baseExpectation{
locker: &sync.Mutex{},
fs: afero.NewOsFs(),
waiter: wait.NoWait,
serviceDesc: svc,
},
run: func(context.Context, grpc.ServerStream) error {
return status.Error(codes.Unimplemented, "not implemented")
},
}
}