1 /*
2  *
3  * Copyright 2015 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/log.h>
20 #include <grpc/support/sync.h>
21 #include <grpc/support/time.h>
22 
23 #include "src/core/lib/channel/channel_args.h"
24 #include "src/core/lib/gpr/useful.h"
25 #include "src/core/lib/surface/channel.h"
26 #include "test/core/end2end/cq_verifier.h"
27 #include "test/core/end2end/end2end_tests.h"
28 
29 #define PING_NUM 5
30 
tag(intptr_t t)31 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
32 
test_ping(grpc_end2end_test_config config,int min_time_between_pings_ms)33 static void test_ping(grpc_end2end_test_config config,
34                       int min_time_between_pings_ms) {
35   grpc_end2end_test_fixture f = config.create_fixture(nullptr, nullptr);
36   cq_verifier* cqv = cq_verifier_create(f.cq);
37   grpc_connectivity_state state = GRPC_CHANNEL_IDLE;
38   int i;
39 
40   grpc_arg client_a[] = {
41       grpc_channel_arg_integer_create(
42           const_cast<char*>(GRPC_ARG_HTTP2_MAX_PINGS_WITHOUT_DATA), 0),
43       grpc_channel_arg_integer_create(
44           const_cast<char*>(GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS), 1)};
45   grpc_arg server_a[] = {
46       grpc_channel_arg_integer_create(
47           const_cast<char*>(
48               GRPC_ARG_HTTP2_MIN_RECV_PING_INTERVAL_WITHOUT_DATA_MS),
49           0),
50       grpc_channel_arg_integer_create(
51           const_cast<char*>(GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS), 1)};
52   grpc_channel_args client_args = {GPR_ARRAY_SIZE(client_a), client_a};
53   grpc_channel_args server_args = {GPR_ARRAY_SIZE(server_a), server_a};
54 
55   config.init_client(&f, &client_args);
56   config.init_server(&f, &server_args);
57 
58   grpc_channel_ping(f.client, f.cq, tag(0), nullptr);
59   CQ_EXPECT_COMPLETION(cqv, tag(0), 0);
60 
61   /* check that we're still in idle, and start connecting */
62   GPR_ASSERT(grpc_channel_check_connectivity_state(f.client, 1) ==
63              GRPC_CHANNEL_IDLE);
64   /* we'll go through some set of transitions (some might be missed), until
65      READY is reached */
66   while (state != GRPC_CHANNEL_READY) {
67     grpc_channel_watch_connectivity_state(
68         f.client, state,
69         gpr_time_add(grpc_timeout_seconds_to_deadline(3),
70                      gpr_time_from_millis(min_time_between_pings_ms * PING_NUM,
71                                           GPR_TIMESPAN)),
72         f.cq, tag(99));
73     CQ_EXPECT_COMPLETION(cqv, tag(99), 1);
74     cq_verify(cqv);
75     state = grpc_channel_check_connectivity_state(f.client, 0);
76     GPR_ASSERT(state == GRPC_CHANNEL_READY ||
77                state == GRPC_CHANNEL_CONNECTING ||
78                state == GRPC_CHANNEL_TRANSIENT_FAILURE);
79   }
80 
81   for (i = 1; i <= PING_NUM; i++) {
82     grpc_channel_ping(f.client, f.cq, tag(i), nullptr);
83     CQ_EXPECT_COMPLETION(cqv, tag(i), 1);
84     cq_verify(cqv);
85   }
86 
87   grpc_server_shutdown_and_notify(f.server, f.cq, tag(0xdead));
88   CQ_EXPECT_COMPLETION(cqv, tag(0xdead), 1);
89   cq_verify(cqv);
90 
91   /* cleanup server */
92   grpc_server_destroy(f.server);
93 
94   grpc_channel_destroy(f.client);
95   grpc_completion_queue_shutdown(f.cq);
96   grpc_completion_queue_destroy(f.cq);
97 
98   /* f.shutdown_cq is not used in this test */
99   grpc_completion_queue_destroy(f.shutdown_cq);
100   config.tear_down_data(&f);
101 
102   cq_verifier_destroy(cqv);
103 }
104 
ping(grpc_end2end_test_config config)105 void ping(grpc_end2end_test_config config) {
106   GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION);
107   test_ping(config, 0);
108   test_ping(config, 100);
109 }
110 
ping_pre_init(void)111 void ping_pre_init(void) {}
112