Repository: grpc/grpc-go
File: test/goaway_test.go (Go)

Code before:
		t.Fatalf("%v.Recv() = _, %v, want _, <non-nil, non-EOF>", stream, err)
	}
	<-ch
	awaitNewConnLogOutput()
}

// Proxies typically send GO_AWAY followed by connection closure a minute or so later. This
// test ensures that the connection is re-created after GO_AWAY and not affected by the
// subsequent (old) connection closure.
func (s) TestGoAwayThenClose(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()

	lis1, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		t.Fatalf("Error while listening. Err: %v", err)
	}

	ss1 := &stubserver.StubServer{
		Listener: lis1,
		UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
			return &testpb.SimpleResponse{}, nil
		},
		FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error {
			if err := stream.Send(&testpb.StreamingOutputCallResponse{}); err != nil {
				t.Errorf("unexpected error from send: %v", err)
				return err
			}
			// Wait until a message is received from client
			_, err := stream.Recv()
			if err == nil {
				t.Error("expected to never receive any message")
			}
			return err
		},
		S: grpc.NewServer(),
	}
	stubserver.StartTestService(t, ss1)
	defer ss1.S.Stop()

	conn2Established := grpcsync.NewEvent()
	lis2, err := listenWithNotifyingListener("tcp", "localhost:0", conn2Established)
	if err != nil {
		t.Fatalf("Error while listening. Err: %v", err)
	}
	ss2 := &stubserver.StubServer{
		Listener: lis2,
		UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
			return &testpb.SimpleResponse{}, nil
		},
		FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error {

Review comment (suggestion):
Nit: error strings as part of calls to t.Errorf and t.Fatalf don't have to follow the usual recommendation for error strings (of not starting with an upper letter). So, the error strings in this function *can* start with an upper case letter.

Code after:
	<-ch
	awaitNewConnLogOutput()
}

// Proxies typically send GO_AWAY followed by connection closure a minute or so later. This
// test ensures that the connection is re-created after GO_AWAY and not affected by the
// subsequent (old) connection closure.
func (s) TestGoAwayThenClose(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()

	lis1, err := testutils.LocalTCPListener()
	if err != nil {
		t.Fatalf("Error while listening. Err: %v", err)
	}

	unaryCallF := func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
		return &testpb.SimpleResponse{}, nil
	}
	fullDuplexCallF := func(stream testgrpc.TestService_FullDuplexCallServer) error {
		if err := stream.Send(&testpb.StreamingOutputCallResponse{}); err != nil {
			t.Errorf("Unexpected error from send: %v", err)
			return err
		}
		// Wait until a message is received from client
		_, err := stream.Recv()
		if err == nil {
			t.Error("Expected to never receive any message")
		}
		return err
	}
	ss1 := &stubserver.StubServer{
		Listener:        lis1,
		UnaryCallF:      unaryCallF,
		FullDuplexCallF: fullDuplexCallF,
		S:               grpc.NewServer(),
	}
	stubserver.StartTestService(t, ss1)
	defer ss1.S.Stop()

	conn2Established := grpcsync.NewEvent()
	lis2, err := listenWithNotifyingListener("tcp", "localhost:0", conn2Established)
	if err != nil {
		t.Fatalf("Error while listening. Err: %v", err)
	}
	ss2 := &stubserver.StubServer{
		Listener:        lis2,
		UnaryCallF:      unaryCallF,
		FullDuplexCallF: fullDuplexCallF,
		S:               grpc.NewServer(),
	}