h2_sockpair_1byte.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <string.h>
  19. #include <grpc/support/alloc.h>
  20. #include <grpc/support/log.h>
  21. #include <grpc/support/sync.h>
  22. #include "src/core/ext/filters/client_channel/client_channel.h"
  23. #include "src/core/ext/filters/http/client/http_client_filter.h"
  24. #include "src/core/ext/filters/http/message_compress/message_compress_filter.h"
  25. #include "src/core/ext/filters/http/server/http_server_filter.h"
  26. #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
  27. #include "src/core/lib/channel/connected_channel.h"
  28. #include "src/core/lib/iomgr/endpoint_pair.h"
  29. #include "src/core/lib/iomgr/iomgr.h"
  30. #include "src/core/lib/resource_quota/api.h"
  31. #include "src/core/lib/surface/channel.h"
  32. #include "src/core/lib/surface/completion_queue.h"
  33. #include "src/core/lib/surface/server.h"
  34. #include "test/core/end2end/end2end_tests.h"
  35. #include "test/core/util/port.h"
  36. #include "test/core/util/test_config.h"
  37. /* chttp2 transport that is immediately available (used for testing
  38. connected_channel without a client_channel */
  39. struct custom_fixture_data {
  40. grpc_endpoint_pair ep;
  41. };
  42. static void server_setup_transport(void* ts, grpc_transport* transport) {
  43. grpc_end2end_test_fixture* f = static_cast<grpc_end2end_test_fixture*>(ts);
  44. grpc_core::ExecCtx exec_ctx;
  45. custom_fixture_data* fixture_data =
  46. static_cast<custom_fixture_data*>(f->fixture_data);
  47. grpc_endpoint_add_to_pollset(fixture_data->ep.server, grpc_cq_pollset(f->cq));
  48. grpc_core::Server* core_server = grpc_core::Server::FromC(f->server);
  49. grpc_error_handle error = core_server->SetupTransport(
  50. transport, nullptr, core_server->channel_args(), nullptr);
  51. if (error == GRPC_ERROR_NONE) {
  52. grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
  53. } else {
  54. GRPC_ERROR_UNREF(error);
  55. grpc_transport_destroy(transport);
  56. }
  57. }
  58. typedef struct {
  59. grpc_end2end_test_fixture* f;
  60. const grpc_channel_args* client_args;
  61. } sp_client_setup;
  62. static void client_setup_transport(void* ts, grpc_transport* transport) {
  63. sp_client_setup* cs = static_cast<sp_client_setup*>(ts);
  64. grpc_arg authority_arg = grpc_channel_arg_string_create(
  65. const_cast<char*>(GRPC_ARG_DEFAULT_AUTHORITY),
  66. const_cast<char*>("test-authority"));
  67. const grpc_channel_args* args =
  68. grpc_channel_args_copy_and_add(cs->client_args, &authority_arg, 1);
  69. grpc_error_handle error = GRPC_ERROR_NONE;
  70. cs->f->client = grpc_channel_create_internal(
  71. "socketpair-target", args, GRPC_CLIENT_DIRECT_CHANNEL, transport, &error);
  72. grpc_channel_args_destroy(args);
  73. if (cs->f->client != nullptr) {
  74. grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
  75. } else {
  76. intptr_t integer;
  77. grpc_status_code status = GRPC_STATUS_INTERNAL;
  78. if (grpc_error_get_int(error, GRPC_ERROR_INT_GRPC_STATUS, &integer)) {
  79. status = static_cast<grpc_status_code>(integer);
  80. }
  81. GRPC_ERROR_UNREF(error);
  82. cs->f->client =
  83. grpc_lame_client_channel_create(nullptr, status, "lame channel");
  84. grpc_transport_destroy(transport);
  85. }
  86. }
  87. static grpc_end2end_test_fixture chttp2_create_fixture_socketpair(
  88. const grpc_channel_args* /*client_args*/,
  89. const grpc_channel_args* /*server_args*/) {
  90. custom_fixture_data* fixture_data = static_cast<custom_fixture_data*>(
  91. gpr_malloc(sizeof(custom_fixture_data)));
  92. grpc_end2end_test_fixture f;
  93. memset(&f, 0, sizeof(f));
  94. f.fixture_data = fixture_data;
  95. f.cq = grpc_completion_queue_create_for_next(nullptr);
  96. f.shutdown_cq = grpc_completion_queue_create_for_pluck(nullptr);
  97. grpc_arg a[3];
  98. a[0].key = const_cast<char*>(GRPC_ARG_TCP_READ_CHUNK_SIZE);
  99. a[0].type = GRPC_ARG_INTEGER;
  100. a[0].value.integer = 1;
  101. a[1].key = const_cast<char*>(GRPC_ARG_TCP_MIN_READ_CHUNK_SIZE);
  102. a[1].type = GRPC_ARG_INTEGER;
  103. a[1].value.integer = 1;
  104. a[2].key = const_cast<char*>(GRPC_ARG_TCP_MAX_READ_CHUNK_SIZE);
  105. a[2].type = GRPC_ARG_INTEGER;
  106. a[2].value.integer = 1;
  107. grpc_channel_args args = {GPR_ARRAY_SIZE(a), a};
  108. fixture_data->ep = grpc_iomgr_create_endpoint_pair("fixture", &args);
  109. return f;
  110. }
  111. static void chttp2_init_client_socketpair(
  112. grpc_end2end_test_fixture* f, const grpc_channel_args* client_args) {
  113. grpc_core::ExecCtx exec_ctx;
  114. auto* fixture_data = static_cast<custom_fixture_data*>(f->fixture_data);
  115. grpc_transport* transport;
  116. sp_client_setup cs;
  117. client_args = grpc_core::CoreConfiguration::Get()
  118. .channel_args_preconditioning()
  119. .PreconditionChannelArgs(client_args);
  120. cs.client_args = client_args;
  121. cs.f = f;
  122. transport =
  123. grpc_create_chttp2_transport(client_args, fixture_data->ep.client, true);
  124. client_setup_transport(&cs, transport);
  125. grpc_channel_args_destroy(client_args);
  126. GPR_ASSERT(f->client);
  127. }
  128. static void chttp2_init_server_socketpair(
  129. grpc_end2end_test_fixture* f, const grpc_channel_args* server_args) {
  130. grpc_core::ExecCtx exec_ctx;
  131. auto* fixture_data = static_cast<custom_fixture_data*>(f->fixture_data);
  132. grpc_transport* transport;
  133. GPR_ASSERT(!f->server);
  134. f->server = grpc_server_create(server_args, nullptr);
  135. grpc_server_register_completion_queue(f->server, f->cq, nullptr);
  136. grpc_server_start(f->server);
  137. server_args = grpc_core::CoreConfiguration::Get()
  138. .channel_args_preconditioning()
  139. .PreconditionChannelArgs(server_args);
  140. transport =
  141. grpc_create_chttp2_transport(server_args, fixture_data->ep.server, false);
  142. grpc_channel_args_destroy(server_args);
  143. server_setup_transport(f, transport);
  144. }
  145. static void chttp2_tear_down_socketpair(grpc_end2end_test_fixture* f) {
  146. grpc_core::ExecCtx exec_ctx;
  147. gpr_free(f->fixture_data);
  148. }
  149. /* All test configurations */
  150. static grpc_end2end_test_config configs[] = {
  151. {"chttp2/socketpair_one_byte_at_a_time",
  152. FEATURE_MASK_SUPPORTS_AUTHORITY_HEADER, nullptr,
  153. chttp2_create_fixture_socketpair, chttp2_init_client_socketpair,
  154. chttp2_init_server_socketpair, chttp2_tear_down_socketpair},
  155. };
  156. int main(int argc, char** argv) {
  157. size_t i;
  158. g_fixture_slowdown_factor = 2;
  159. grpc::testing::TestEnvironment env(argc, argv);
  160. grpc_end2end_tests_pre_init();
  161. grpc_init();
  162. for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
  163. grpc_end2end_tests(argc, argv, configs[i]);
  164. }
  165. grpc_shutdown();
  166. return 0;
  167. }