3 * Copyright 2020 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include <gmock/gmock.h>
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>
37 #include <grpcpp/impl/codegen/service_type.h>
38 #include <grpcpp/server_builder.h>
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"
49 #include "test/core/util/memory_counters.h"
50 #include "test/core/util/port.h"
51 #include "test/core/util/test_config.h"
53 #include "test/core/end2end/cq_verifier.h"
57 void* tag(int i) { return (void*)static_cast<intptr_t>(i); }
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) {
65 cq_verifier* cqv = cq_verifier_create(cq);
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;
75 gpr_timespec deadline = grpc_timeout_seconds_to_deadline(5);
77 c = grpc_channel_create_call(channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq,
78 grpc_slice_from_static_string("/foo"), nullptr,
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));
87 op->op = GRPC_OP_SEND_INITIAL_METADATA;
88 op->data.send_initial_metadata.count = 0;
90 op->reserved = nullptr;
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;
97 op->reserved = nullptr;
99 error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
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);
108 grpc_call_cancel_with_status(s, GRPC_STATUS_PERMISSION_DENIED, "test status",
110 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
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);
120 cq_verifier_destroy(cqv);
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);
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);
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;
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;
150 int num_not_cancelled = 0;
151 for (auto itr = statuses_and_counts.begin(); itr != statuses_and_counts.end();
153 if (itr->first != GRPC_STATUS_PERMISSION_DENIED) {
154 num_not_cancelled += itr->second;
156 gpr_log(GPR_INFO, "%d / %d RPCs received status code: %d", itr->second,
157 kNumTotalRpcs, itr->first);
159 if (num_not_cancelled > 0) {
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);
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),
172 .type != GRPC_QUEUE_SHUTDOWN)
174 grpc_server_destroy(server);
175 grpc_completion_queue_destroy(cq);
180 int main(int argc, char** argv) {
181 ::testing::InitGoogleTest(&argc, argv);
182 grpc::testing::TestEnvironment env(argc, argv);
184 auto result = RUN_ALL_TESTS();