Imported Upstream version 1.31.0
[platform/upstream/grpc.git] / test / core / transport / chttp2 / too_many_pings_test.cc
1 /*
2  *
3  * Copyright 2020 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
19 #include <grpc/support/port_platform.h>
20
21 #include <gmock/gmock.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <functional>
25 #include <set>
26 #include <thread>
27
28 #include <grpc/grpc.h>
29 #include <grpc/grpc_security.h>
30 #include <grpc/impl/codegen/grpc_types.h>
31 #include <grpc/slice.h>
32 #include <grpc/support/alloc.h>
33 #include <grpc/support/log.h>
34 #include <grpc/support/string_util.h>
35 #include <grpc/support/time.h>
36
37 #include <grpcpp/impl/codegen/service_type.h>
38 #include <grpcpp/server_builder.h>
39
40 #include "src/core/lib/gpr/useful.h"
41 #include "src/core/lib/gprpp/host_port.h"
42 #include "src/core/lib/gprpp/thd.h"
43 #include "src/core/lib/iomgr/error.h"
44 #include "src/core/lib/security/credentials/alts/alts_credentials.h"
45 #include "src/core/lib/security/credentials/credentials.h"
46 #include "src/core/lib/security/security_connector/alts/alts_security_connector.h"
47 #include "src/core/lib/slice/slice_string_helpers.h"
48
49 #include "test/core/util/memory_counters.h"
50 #include "test/core/util/port.h"
51 #include "test/core/util/test_config.h"
52
53 #include "test/core/end2end/cq_verifier.h"
54
55 namespace {
56
57 void* tag(int i) { return (void*)static_cast<intptr_t>(i); }
58
59 // Perform a simple RPC where the server cancels the request with
60 // grpc_call_cancel_with_status
61 grpc_status_code PerformCall(grpc_channel* channel, grpc_server* server,
62                              grpc_completion_queue* cq) {
63   grpc_call* c;
64   grpc_call* s;
65   cq_verifier* cqv = cq_verifier_create(cq);
66   grpc_op ops[6];
67   grpc_op* op;
68   grpc_metadata_array initial_metadata_recv;
69   grpc_metadata_array trailing_metadata_recv;
70   grpc_metadata_array request_metadata_recv;
71   grpc_call_details call_details;
72   grpc_status_code status;
73   grpc_call_error error;
74   grpc_slice details;
75   gpr_timespec deadline = grpc_timeout_seconds_to_deadline(5);
76   // Start a call
77   c = grpc_channel_create_call(channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
78                                grpc_slice_from_static_string("/foo"), nullptr,
79                                deadline, nullptr);
80   GPR_ASSERT(c);
81   grpc_metadata_array_init(&initial_metadata_recv);
82   grpc_metadata_array_init(&trailing_metadata_recv);
83   grpc_metadata_array_init(&request_metadata_recv);
84   grpc_call_details_init(&call_details);
85   memset(ops, 0, sizeof(ops));
86   op = ops;
87   op->op = GRPC_OP_SEND_INITIAL_METADATA;
88   op->data.send_initial_metadata.count = 0;
89   op->flags = 0;
90   op->reserved = nullptr;
91   op++;
92   op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
93   op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
94   op->data.recv_status_on_client.status = &status;
95   op->data.recv_status_on_client.status_details = &details;
96   op->flags = 0;
97   op->reserved = nullptr;
98   op++;
99   error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
100                                 nullptr);
101   GPR_ASSERT(GRPC_CALL_OK == error);
102   // Request a call on the server
103   error = grpc_server_request_call(server, &s, &call_details,
104                                    &request_metadata_recv, cq, cq, tag(101));
105   GPR_ASSERT(GRPC_CALL_OK == error);
106   CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
107   cq_verify(cqv);
108   grpc_call_cancel_with_status(s, GRPC_STATUS_PERMISSION_DENIED, "test status",
109                                nullptr);
110   CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
111   cq_verify(cqv);
112   // cleanup
113   grpc_slice_unref(details);
114   grpc_metadata_array_destroy(&initial_metadata_recv);
115   grpc_metadata_array_destroy(&trailing_metadata_recv);
116   grpc_metadata_array_destroy(&request_metadata_recv);
117   grpc_call_details_destroy(&call_details);
118   grpc_call_unref(c);
119   grpc_call_unref(s);
120   cq_verifier_destroy(cqv);
121   return status;
122 }
123
124 // Test that sending a lot of RPCs that are cancelled by the server doesn't
125 // result in too many pings due to the pings sent by BDP.
126 TEST(TooManyPings, TestLotsOfServerCancelledRpcsDoesntGiveTooManyPings) {
127   grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
128   // create the server
129   grpc_server* server = grpc_server_create(nullptr, nullptr);
130   std::string server_address =
131       grpc_core::JoinHostPort("localhost", grpc_pick_unused_port_or_die());
132   grpc_server_register_completion_queue(server, cq, nullptr);
133   GPR_ASSERT(
134       grpc_server_add_insecure_http2_port(server, server_address.c_str()));
135   grpc_server_start(server);
136   // create the channel (bdp pings are enabled by default)
137   grpc_channel* channel = grpc_insecure_channel_create(
138       server_address.c_str(), nullptr /* channel args */, nullptr);
139   std::map<grpc_status_code, int> statuses_and_counts;
140   const int kNumTotalRpcs = 1e5;
141   // perform an RPC
142   gpr_log(GPR_INFO,
143           "Performing %d total RPCs and expecting them all to receive status "
144           "PERMISSION_DENIED (%d)",
145           kNumTotalRpcs, GRPC_STATUS_PERMISSION_DENIED);
146   for (int i = 0; i < kNumTotalRpcs; i++) {
147     grpc_status_code status = PerformCall(channel, server, cq);
148     statuses_and_counts[status] += 1;
149   }
150   int num_not_cancelled = 0;
151   for (auto itr = statuses_and_counts.begin(); itr != statuses_and_counts.end();
152        itr++) {
153     if (itr->first != GRPC_STATUS_PERMISSION_DENIED) {
154       num_not_cancelled += itr->second;
155     }
156     gpr_log(GPR_INFO, "%d / %d RPCs received status code: %d", itr->second,
157             kNumTotalRpcs, itr->first);
158   }
159   if (num_not_cancelled > 0) {
160     gpr_log(GPR_ERROR,
161             "Expected all RPCs to receive status PERMISSION_DENIED (%d) but %d "
162             "received other status codes",
163             GRPC_STATUS_PERMISSION_DENIED, num_not_cancelled);
164     FAIL();
165   }
166   // shutdown and destroy the client and server
167   grpc_channel_destroy(channel);
168   grpc_server_shutdown_and_notify(server, cq, nullptr);
169   grpc_completion_queue_shutdown(cq);
170   while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
171                                     nullptr)
172              .type != GRPC_QUEUE_SHUTDOWN)
173     ;
174   grpc_server_destroy(server);
175   grpc_completion_queue_destroy(cq);
176 }
177
178 }  // namespace
179
180 int main(int argc, char** argv) {
181   ::testing::InitGoogleTest(&argc, argv);
182   grpc::testing::TestEnvironment env(argc, argv);
183   grpc_init();
184   auto result = RUN_ALL_TESTS();
185   grpc_shutdown();
186   return result;
187 }