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

Code before:
		t.Fatalf("stream.RecvMsg(_) = %v returned unexpectedly", err)
	case <-time.After(defaultTestShortTimeout):
	}

	close(waitForReturn)

	// Recv() should return after the server handler returns.
	select {
	case err := <-errCh:
		if err != nil {
			t.Fatalf("stream.RecvMsg(_) = %v, want <nil>", err)
		}
	case <-time.After(defaultTestTimeout):
		t.Fatal("Timed out waiting for stream.RecvMsg(_) to return")
	}
}

// Tests the behavior where server handler returns an error after calling
// SendAndClose. It verifies the behavior that client receives nil message and
// non-nil error.
func (s) TestClientStreaming_ReturnErrorAfterSendAndClose(t *testing.T) {
	wantError := status.Error(codes.Internal, "error for testing")
	ss := stubserver.StubServer{
		StreamingInputCallF: func(stream testgrpc.TestService_StreamingInputCallServer) error {
			if err := stream.SendAndClose(&testpb.StreamingInputCallResponse{}); err != nil {
				t.Fatalf("stream.SendAndClose(_) = %v, want <nil>", err)
			}
			return wantError
		},
	}
	if err := ss.Start(nil); err != nil {
		t.Fatal("Error starting server:", err)
	}
	defer ss.Stop()

	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	stream, err := ss.Client.StreamingInputCall(ctx)
	if err != nil {
		t.Fatalf(".StreamingInputCall(_) = _, %v, want <nil>", err)
	}
	if resp, err := stream.CloseAndRecv(); status.Code(err) != codes.Internal || resp != nil {
		t.Fatalf("stream.CloseSend() = %v , %v, want <nil>, %s", resp, err, wantError)
	}
}

func (s) TestExceedMaxStreamsLimit(t *testing.T) {
	for _, e := range listTestEnv() {
		testExceedMaxStreamsLimit(t, e)
	}
}

Review comment (suggestion):
This needs to be `Errorf` since it's not in the main test goroutine.  Otherwise it just kills the current goroutine, and keeps the test running.  (I see the same thing above.)

Code after:
	case err := <-errCh:
		t.Fatalf("stream.RecvMsg(_) = %v returned unexpectedly", err)
	case <-time.After(defaultTestShortTimeout):
	}

	close(waitForReturn)

	// Recv() should return after the server handler returns.
	select {
	case err := <-errCh:
		if err != nil {
			t.Fatalf("stream.RecvMsg(_) = %v, want <nil>", err)
		}
	case <-time.After(defaultTestTimeout):
		t.Fatal("Timed out waiting for stream.RecvMsg(_) to return")
	}
}

// Tests the behavior where server handler returns an error after calling
// SendAndClose. It verifies the that client receives nil message and
// non-nil error.
func (s) TestClientStreaming_ReturnErrorAfterSendAndClose(t *testing.T) {
	wantError := status.Error(codes.Internal, "error for testing")
	ss := stubserver.StubServer{
		StreamingInputCallF: func(stream testgrpc.TestService_StreamingInputCallServer) error {
			if err := stream.SendAndClose(&testpb.StreamingInputCallResponse{}); err != nil {
				t.Errorf("stream.SendAndClose(_) = %v, want <nil>", err)
			}
			return wantError
		},
	}
	if err := ss.Start(nil); err != nil {
		t.Fatal("Error starting server:", err)
	}
	defer ss.Stop()

	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()
	stream, err := ss.Client.StreamingInputCall(ctx)
	if err != nil {
		t.Fatalf(".StreamingInputCall(_) = _, %v, want <nil>", err)
	}
	resp, err := stream.CloseAndRecv()

	wantStatus, _ := status.FromError(wantError)
	gotStatus, _ := status.FromError(err)

	if gotStatus.Code() != wantStatus.Code() || gotStatus.Message() != wantStatus.Message() || resp != nil {
		t.Fatalf("stream.CloseSend() = %v , %v, want <nil>, %s", resp, err, wantError)
	}
}