MCPcopy
hub / github.com/grpc/grpc-go / TestChainOnBaseUnaryServerInterceptor

Method TestChainOnBaseUnaryServerInterceptor

test/server_test.go:288–329  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

286}
287
288func (s) TestChainOnBaseUnaryServerInterceptor(t *testing.T) {
289 baseIntKey := ctxKey("baseIntKey")
290
291 baseInt := func(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
292 if ctx.Value(baseIntKey) != nil {
293 return nil, status.Errorf(codes.Internal, "base interceptor should not have %v in context", baseIntKey)
294 }
295
296 baseCtx := context.WithValue(ctx, baseIntKey, 1)
297 return handler(baseCtx, req)
298 }
299
300 chainInt := func(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
301 if ctx.Value(baseIntKey) == nil {
302 return nil, status.Errorf(codes.Internal, "chain interceptor should have %v in context", baseIntKey)
303 }
304
305 return handler(ctx, req)
306 }
307
308 sopts := []grpc.ServerOption{
309 grpc.UnaryInterceptor(baseInt),
310 grpc.ChainUnaryInterceptor(chainInt),
311 }
312
313 ss := &stubserver.StubServer{
314 EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
315 return &testpb.Empty{}, nil
316 },
317 }
318 if err := ss.Start(sopts); err != nil {
319 t.Fatalf("Error starting endpoint server: %v", err)
320 }
321 defer ss.Stop()
322
323 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
324 defer cancel()
325 resp, err := ss.Client.EmptyCall(ctx, &testpb.Empty{})
326 if s, ok := status.FromError(err); !ok || s.Code() != codes.OK {
327 t.Fatalf("ss.Client.EmptyCall(ctx, _) = %v, %v; want nil, <status with Code()=OK>", resp, err)
328 }
329}
330
331func (s) TestChainStreamServerInterceptor(t *testing.T) {
332 callCounts := make([]atomic.Int32, 4)

Callers

nothing calls this directly

Calls 12

StartMethod · 0.95
StopMethod · 0.95
ErrorfFunction · 0.92
UnaryInterceptorFunction · 0.92
ChainUnaryInterceptorFunction · 0.92
FromErrorFunction · 0.92
WithValueMethod · 0.80
CodeMethod · 0.80
ctxKeyTypeAlias · 0.70
FatalfMethod · 0.65
EmptyCallMethod · 0.65
ValueMethod · 0.45

Tested by

no test coverage detected