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