...

Source file src/google.golang.org/grpc/test/invoke_test.go

Documentation: google.golang.org/grpc/test

     1  /*
     2   *
     3   * Copyright 2022 gRPC authors.
     4  
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   *     https://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   *
    18   */
    19  
    20  package test
    21  
    22  import (
    23  	"context"
    24  	"strings"
    25  	"testing"
    26  
    27  	"google.golang.org/grpc"
    28  	"google.golang.org/grpc/codes"
    29  	"google.golang.org/grpc/internal/stubserver"
    30  	testpb "google.golang.org/grpc/interop/grpc_testing"
    31  	"google.golang.org/grpc/status"
    32  )
    33  
    34  // TestInvoke verifies a straightforward invocation of ClientConn.Invoke().
    35  func (s) TestInvoke(t *testing.T) {
    36  	ss := &stubserver.StubServer{
    37  		EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
    38  	}
    39  	if err := ss.Start(nil); err != nil {
    40  		t.Fatalf("Failed to start stub server: %v", err)
    41  	}
    42  	defer ss.Stop()
    43  
    44  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
    45  	defer cancel()
    46  	if err := ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{}); err != nil {
    47  		t.Fatalf("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") failed: %v", err)
    48  	}
    49  }
    50  
    51  // TestInvokeLargeErr verifies an invocation of ClientConn.Invoke() where the
    52  // server returns a really large error message.
    53  func (s) TestInvokeLargeErr(t *testing.T) {
    54  	largeErrorStr := strings.Repeat("A", 1024*1024)
    55  	ss := &stubserver.StubServer{
    56  		EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
    57  			return &testpb.Empty{}, status.Error(codes.Internal, largeErrorStr)
    58  		},
    59  	}
    60  	if err := ss.Start(nil); err != nil {
    61  		t.Fatalf("Failed to start stub server: %v", err)
    62  	}
    63  	defer ss.Stop()
    64  
    65  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
    66  	defer cancel()
    67  	err := ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{})
    68  	if err == nil {
    69  		t.Fatal("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") succeeded when expected to fail")
    70  	}
    71  	st, ok := status.FromError(err)
    72  	if !ok {
    73  		t.Fatal("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") received non-status error")
    74  	}
    75  	if status.Code(err) != codes.Internal || st.Message() != largeErrorStr {
    76  		t.Fatalf("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") failed with error: %v, want an error of code %d and desc size %d", err, codes.Internal, len(largeErrorStr))
    77  	}
    78  }
    79  
    80  // TestInvokeErrorSpecialChars tests an invocation of ClientConn.Invoke() and
    81  // verifies that error messages don't get mangled.
    82  func (s) TestInvokeErrorSpecialChars(t *testing.T) {
    83  	const weirdError = "format verbs: %v%s"
    84  	ss := &stubserver.StubServer{
    85  		EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
    86  			return &testpb.Empty{}, status.Error(codes.Internal, weirdError)
    87  		},
    88  	}
    89  	if err := ss.Start(nil); err != nil {
    90  		t.Fatalf("Failed to start stub server: %v", err)
    91  	}
    92  	defer ss.Stop()
    93  
    94  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
    95  	defer cancel()
    96  	err := ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{})
    97  	if err == nil {
    98  		t.Fatal("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") succeeded when expected to fail")
    99  	}
   100  	st, ok := status.FromError(err)
   101  	if !ok {
   102  		t.Fatal("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") received non-status error")
   103  	}
   104  	if status.Code(err) != codes.Internal || st.Message() != weirdError {
   105  		t.Fatalf("grpc.Invoke(\"/grpc.testing.TestService/EmptyCall\") failed with error: %v, want %v", err, weirdError)
   106  	}
   107  }
   108  
   109  // TestInvokeCancel tests an invocation of ClientConn.Invoke() with a cancelled
   110  // context and verifies that the request is not actually sent to the server.
   111  func (s) TestInvokeCancel(t *testing.T) {
   112  	cancelled := 0
   113  	ss := &stubserver.StubServer{
   114  		EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
   115  			cancelled++
   116  			return &testpb.Empty{}, nil
   117  		},
   118  	}
   119  	if err := ss.Start(nil); err != nil {
   120  		t.Fatalf("Failed to start stub server: %v", err)
   121  	}
   122  	defer ss.Stop()
   123  
   124  	for i := 0; i < 100; i++ {
   125  		ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
   126  		cancel()
   127  		ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{})
   128  	}
   129  	if cancelled != 0 {
   130  		t.Fatalf("server received %d of 100 cancelled requests", cancelled)
   131  	}
   132  }
   133  
   134  // TestInvokeCancelClosedNonFail tests an invocation of ClientConn.Invoke() with
   135  // a cancelled non-failfast RPC on a closed ClientConn and verifies that the
   136  // call terminates with an error.
   137  func (s) TestInvokeCancelClosedNonFailFast(t *testing.T) {
   138  	ss := &stubserver.StubServer{
   139  		EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) { return &testpb.Empty{}, nil },
   140  	}
   141  	if err := ss.Start(nil); err != nil {
   142  		t.Fatalf("Failed to start stub server: %v", err)
   143  	}
   144  	defer ss.Stop()
   145  
   146  	ss.CC.Close()
   147  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
   148  	cancel()
   149  	if err := ss.CC.Invoke(ctx, "/grpc.testing.TestService/EmptyCall", &testpb.Empty{}, &testpb.Empty{}, grpc.WaitForReady(true)); err == nil {
   150  		t.Fatal("ClientConn.Invoke() on closed connection succeeded when expected to fail")
   151  	}
   152  }
   153  

View as plain text