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