OSDN Git Service

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