stream_cleanup_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. package test
  19. import (
  20. "context"
  21. "io"
  22. "testing"
  23. "time"
  24. "google.golang.org/grpc"
  25. "google.golang.org/grpc/codes"
  26. "google.golang.org/grpc/status"
  27. testpb "google.golang.org/grpc/test/grpc_testing"
  28. )
  29. func (s) TestStreamCleanup(t *testing.T) {
  30. const initialWindowSize uint = 70 * 1024 // Must be higher than default 64K, ignored otherwise
  31. const bodySize = 2 * initialWindowSize // Something that is not going to fit in a single window
  32. const callRecvMsgSize uint = 1 // The maximum message size the client can receive
  33. ss := &stubServer{
  34. unaryCall: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
  35. return &testpb.SimpleResponse{Payload: &testpb.Payload{
  36. Body: make([]byte, bodySize),
  37. }}, nil
  38. },
  39. emptyCall: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
  40. return &testpb.Empty{}, nil
  41. },
  42. }
  43. if err := ss.Start([]grpc.ServerOption{grpc.MaxConcurrentStreams(1)}, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(int(callRecvMsgSize))), grpc.WithInitialWindowSize(int32(initialWindowSize))); err != nil {
  44. t.Fatalf("Error starting endpoint server: %v", err)
  45. }
  46. defer ss.Stop()
  47. if _, err := ss.client.UnaryCall(context.Background(), &testpb.SimpleRequest{}); status.Code(err) != codes.ResourceExhausted {
  48. t.Fatalf("should fail with ResourceExhausted, message's body size: %v, maximum message size the client can receive: %v", bodySize, callRecvMsgSize)
  49. }
  50. if _, err := ss.client.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
  51. t.Fatalf("should succeed, err: %v", err)
  52. }
  53. }
  54. func (s) TestStreamCleanupAfterSendStatus(t *testing.T) {
  55. const initialWindowSize uint = 70 * 1024 // Must be higher than default 64K, ignored otherwise
  56. const bodySize = 2 * initialWindowSize // Something that is not going to fit in a single window
  57. serverReturnedStatus := make(chan struct{})
  58. ss := &stubServer{
  59. fullDuplexCall: func(stream testpb.TestService_FullDuplexCallServer) error {
  60. defer func() {
  61. close(serverReturnedStatus)
  62. }()
  63. return stream.Send(&testpb.StreamingOutputCallResponse{
  64. Payload: &testpb.Payload{
  65. Body: make([]byte, bodySize),
  66. },
  67. })
  68. },
  69. }
  70. if err := ss.Start([]grpc.ServerOption{grpc.MaxConcurrentStreams(1)}, grpc.WithInitialWindowSize(int32(initialWindowSize))); err != nil {
  71. t.Fatalf("Error starting endpoint server: %v", err)
  72. }
  73. defer ss.Stop()
  74. // This test makes sure we don't delete stream from server transport's
  75. // activeStreams list too aggressively.
  76. // 1. Make a long living stream RPC. So server's activeStream list is not
  77. // empty.
  78. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  79. defer cancel()
  80. stream, err := ss.client.FullDuplexCall(ctx)
  81. if err != nil {
  82. t.Fatalf("FullDuplexCall= _, %v; want _, <nil>", err)
  83. }
  84. // 2. Wait for service handler to return status.
  85. //
  86. // This will trigger a stream cleanup code, which will eventually remove
  87. // this stream from activeStream.
  88. //
  89. // But the stream removal won't happen because it's supposed to be done
  90. // after the status is sent by loopyWriter, and the status send is blocked
  91. // by flow control.
  92. <-serverReturnedStatus
  93. // 3. GracefulStop (besides sending goaway) checks the number of
  94. // activeStreams.
  95. //
  96. // It will close the connection if there's no active streams. This won't
  97. // happen because of the pending stream. But if there's a bug in stream
  98. // cleanup that causes stream to be removed too aggressively, the connection
  99. // will be closd and the stream will be broken.
  100. gracefulStopDone := make(chan struct{})
  101. go func() {
  102. defer close(gracefulStopDone)
  103. ss.s.GracefulStop()
  104. }()
  105. // 4. Make sure the stream is not broken.
  106. if _, err := stream.Recv(); err != nil {
  107. t.Fatalf("stream.Recv() = _, %v, want _, <nil>", err)
  108. }
  109. if _, err := stream.Recv(); err != io.EOF {
  110. t.Fatalf("stream.Recv() = _, %v, want _, io.EOF", err)
  111. }
  112. timer := time.NewTimer(time.Second)
  113. select {
  114. case <-gracefulStopDone:
  115. timer.Stop()
  116. case <-timer.C:
  117. t.Fatalf("s.GracefulStop() didn't finish without 1 second after the last RPC")
  118. }
  119. }