You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

160 lines
4.8 KiB

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