github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/test/goaway_test.go (about)

     1  /*
     2   *
     3   * Copyright 2019 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package test
    20  
    21  import (
    22  	"context"
    23  	"net"
    24  	"testing"
    25  	"time"
    26  
    27  	grpc "github.com/hxx258456/ccgo/grpc"
    28  	"github.com/hxx258456/ccgo/grpc/internal/stubserver"
    29  	"github.com/hxx258456/ccgo/grpc/keepalive"
    30  	testpb "github.com/hxx258456/ccgo/grpc/test/grpc_testing"
    31  )
    32  
    33  // TestGracefulClientOnGoAway attempts to ensure that when the server sends a
    34  // GOAWAY (in this test, by configuring max connection age on the server), a
    35  // client will never see an error.  This requires that the client is appraised
    36  // of the GOAWAY and updates its state accordingly before the transport stops
    37  // accepting new streams.  If a subconn is chosen by a picker and receives the
    38  // goaway before creating the stream, an error will occur, but upon transparent
    39  // retry, the clientconn will ensure a ready subconn is chosen.
    40  func (s) TestGracefulClientOnGoAway(t *testing.T) {
    41  	const maxConnAge = 100 * time.Millisecond
    42  	const testTime = maxConnAge * 10
    43  
    44  	ss := &stubserver.StubServer{
    45  		EmptyCallF: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
    46  			return &testpb.Empty{}, nil
    47  		},
    48  	}
    49  
    50  	s := grpc.NewServer(grpc.KeepaliveParams(keepalive.ServerParameters{MaxConnectionAge: maxConnAge}))
    51  	defer s.Stop()
    52  	testpb.RegisterTestServiceServer(s, ss)
    53  
    54  	lis, err := net.Listen("tcp", "localhost:0")
    55  	if err != nil {
    56  		t.Fatalf("Failed to create listener: %v", err)
    57  	}
    58  	go s.Serve(lis)
    59  
    60  	cc, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
    61  	if err != nil {
    62  		t.Fatalf("Failed to dial server: %v", err)
    63  	}
    64  	defer cc.Close()
    65  	c := testpb.NewTestServiceClient(cc)
    66  
    67  	endTime := time.Now().Add(testTime)
    68  	for time.Now().Before(endTime) {
    69  		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    70  		if _, err := c.EmptyCall(ctx, &testpb.Empty{}); err != nil {
    71  			t.Fatalf("EmptyCall(_, _) = _, %v; want _, <nil>", err)
    72  		}
    73  		cancel()
    74  	}
    75  }