negative_http2_client.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. *
  3. * Copyright 2016 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. // Binary http2 is used to test http2 error edge cases like GOAWAYs and
  19. // RST_STREAMs
  20. //
  21. // Documentation:
  22. // https://github.com/grpc/grpc/blob/master/doc/negative-http2-interop-test-descriptions.md
  23. package main
  24. import (
  25. "context"
  26. "flag"
  27. "net"
  28. "strconv"
  29. "sync"
  30. "time"
  31. "google.golang.org/grpc"
  32. "google.golang.org/grpc/codes"
  33. "google.golang.org/grpc/credentials/insecure"
  34. "google.golang.org/grpc/grpclog"
  35. "google.golang.org/grpc/interop"
  36. "google.golang.org/grpc/status"
  37. testgrpc "google.golang.org/grpc/interop/grpc_testing"
  38. testpb "google.golang.org/grpc/interop/grpc_testing"
  39. )
  40. var (
  41. serverHost = flag.String("server_host", "localhost", "The server host name")
  42. serverPort = flag.Int("server_port", 8080, "The server port number")
  43. testCase = flag.String("test_case", "goaway",
  44. `Configure different test cases. Valid options are:
  45. goaway : client sends two requests, the server will send a goaway in between;
  46. rst_after_header : server will send rst_stream after it sends headers;
  47. rst_during_data : server will send rst_stream while sending data;
  48. rst_after_data : server will send rst_stream after sending data;
  49. ping : server will send pings between each http2 frame;
  50. max_streams : server will ensure that the max_concurrent_streams limit is upheld;`)
  51. largeReqSize = 271828
  52. largeRespSize = 314159
  53. logger = grpclog.Component("interop")
  54. )
  55. func largeSimpleRequest() *testpb.SimpleRequest {
  56. pl := interop.ClientNewPayload(testpb.PayloadType_COMPRESSABLE, largeReqSize)
  57. return &testpb.SimpleRequest{
  58. ResponseType: testpb.PayloadType_COMPRESSABLE,
  59. ResponseSize: int32(largeRespSize),
  60. Payload: pl,
  61. }
  62. }
  63. // sends two unary calls. The server asserts that the calls use different connections.
  64. func goaway(tc testgrpc.TestServiceClient) {
  65. interop.DoLargeUnaryCall(tc)
  66. // sleep to ensure that the client has time to recv the GOAWAY.
  67. // TODO(ncteisen): make this less hacky.
  68. time.Sleep(1 * time.Second)
  69. interop.DoLargeUnaryCall(tc)
  70. }
  71. func rstAfterHeader(tc testgrpc.TestServiceClient) {
  72. req := largeSimpleRequest()
  73. reply, err := tc.UnaryCall(context.Background(), req)
  74. if reply != nil {
  75. logger.Fatal("Client received reply despite server sending rst stream after header")
  76. }
  77. if status.Code(err) != codes.Internal {
  78. logger.Fatalf("%v.UnaryCall() = _, %v, want _, %v", tc, status.Code(err), codes.Internal)
  79. }
  80. }
  81. func rstDuringData(tc testgrpc.TestServiceClient) {
  82. req := largeSimpleRequest()
  83. reply, err := tc.UnaryCall(context.Background(), req)
  84. if reply != nil {
  85. logger.Fatal("Client received reply despite server sending rst stream during data")
  86. }
  87. if status.Code(err) != codes.Unknown {
  88. logger.Fatalf("%v.UnaryCall() = _, %v, want _, %v", tc, status.Code(err), codes.Unknown)
  89. }
  90. }
  91. func rstAfterData(tc testgrpc.TestServiceClient) {
  92. req := largeSimpleRequest()
  93. reply, err := tc.UnaryCall(context.Background(), req)
  94. if reply != nil {
  95. logger.Fatal("Client received reply despite server sending rst stream after data")
  96. }
  97. if status.Code(err) != codes.Internal {
  98. logger.Fatalf("%v.UnaryCall() = _, %v, want _, %v", tc, status.Code(err), codes.Internal)
  99. }
  100. }
  101. func ping(tc testgrpc.TestServiceClient) {
  102. // The server will assert that every ping it sends was ACK-ed by the client.
  103. interop.DoLargeUnaryCall(tc)
  104. }
  105. func maxStreams(tc testgrpc.TestServiceClient) {
  106. interop.DoLargeUnaryCall(tc)
  107. var wg sync.WaitGroup
  108. for i := 0; i < 15; i++ {
  109. wg.Add(1)
  110. go func() {
  111. defer wg.Done()
  112. interop.DoLargeUnaryCall(tc)
  113. }()
  114. }
  115. wg.Wait()
  116. }
  117. func main() {
  118. flag.Parse()
  119. serverAddr := net.JoinHostPort(*serverHost, strconv.Itoa(*serverPort))
  120. var opts []grpc.DialOption
  121. opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
  122. conn, err := grpc.Dial(serverAddr, opts...)
  123. if err != nil {
  124. logger.Fatalf("Fail to dial: %v", err)
  125. }
  126. defer conn.Close()
  127. tc := testgrpc.NewTestServiceClient(conn)
  128. switch *testCase {
  129. case "goaway":
  130. goaway(tc)
  131. logger.Infoln("goaway done")
  132. case "rst_after_header":
  133. rstAfterHeader(tc)
  134. logger.Infoln("rst_after_header done")
  135. case "rst_during_data":
  136. rstDuringData(tc)
  137. logger.Infoln("rst_during_data done")
  138. case "rst_after_data":
  139. rstAfterData(tc)
  140. logger.Infoln("rst_after_data done")
  141. case "ping":
  142. ping(tc)
  143. logger.Infoln("ping done")
  144. case "max_streams":
  145. maxStreams(tc)
  146. logger.Infoln("max_streams done")
  147. default:
  148. logger.Fatal("Unsupported test case: ", *testCase)
  149. }
  150. }