ping.cc 3.8 KB

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