no_logging.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. *
  3. * Copyright 2016 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 <stdio.h>
  19. #include <string.h>
  20. #include <string>
  21. #include "absl/strings/str_cat.h"
  22. #include <grpc/byte_buffer.h>
  23. #include <grpc/grpc.h>
  24. #include <grpc/support/alloc.h>
  25. #include <grpc/support/log.h>
  26. #include <grpc/support/time.h>
  27. #include "src/core/lib/gpr/string.h"
  28. #include "src/core/lib/iomgr/error.h"
  29. #include "test/core/end2end/cq_verifier.h"
  30. #include "test/core/end2end/end2end_tests.h"
  31. enum { TIMEOUT = 200000 };
  32. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  33. void gpr_default_log(gpr_log_func_args* args);
  34. static void test_no_log(gpr_log_func_args* args) {
  35. std::string message = absl::StrCat("Unwanted log: ", args->message);
  36. args->message = message.c_str();
  37. gpr_default_log(args);
  38. abort();
  39. }
  40. static void test_no_error_log(gpr_log_func_args* args) {
  41. if (args->severity == GPR_LOG_SEVERITY_ERROR) {
  42. test_no_log(args);
  43. }
  44. }
  45. static gpr_atm g_log_func = reinterpret_cast<gpr_atm>(gpr_default_log);
  46. static void log_dispatcher_func(gpr_log_func_args* args) {
  47. gpr_log_func log_func =
  48. reinterpret_cast<gpr_log_func>(gpr_atm_no_barrier_load(&g_log_func));
  49. log_func(args);
  50. }
  51. static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
  52. const char* test_name,
  53. grpc_channel_args* client_args,
  54. grpc_channel_args* server_args) {
  55. grpc_end2end_test_fixture f;
  56. gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
  57. f = config.create_fixture(client_args, server_args);
  58. config.init_server(&f, server_args);
  59. config.init_client(&f, client_args);
  60. return f;
  61. }
  62. static gpr_timespec n_seconds_from_now(int n) {
  63. return grpc_timeout_seconds_to_deadline(n);
  64. }
  65. static gpr_timespec five_seconds_from_now(void) {
  66. return n_seconds_from_now(5);
  67. }
  68. static void drain_cq(grpc_completion_queue* cq) {
  69. grpc_event ev;
  70. do {
  71. ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
  72. } while (ev.type != GRPC_QUEUE_SHUTDOWN);
  73. }
  74. static void shutdown_server(grpc_end2end_test_fixture* f) {
  75. if (!f->server) return;
  76. grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
  77. GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
  78. grpc_timeout_seconds_to_deadline(5),
  79. nullptr)
  80. .type == GRPC_OP_COMPLETE);
  81. grpc_server_destroy(f->server);
  82. f->server = nullptr;
  83. }
  84. static void shutdown_client(grpc_end2end_test_fixture* f) {
  85. if (!f->client) return;
  86. grpc_channel_destroy(f->client);
  87. f->client = nullptr;
  88. }
  89. static void end_test(grpc_end2end_test_fixture* f) {
  90. shutdown_server(f);
  91. shutdown_client(f);
  92. grpc_completion_queue_shutdown(f->cq);
  93. drain_cq(f->cq);
  94. grpc_completion_queue_destroy(f->cq);
  95. grpc_completion_queue_destroy(f->shutdown_cq);
  96. }
  97. static void simple_request_body(grpc_end2end_test_config /*config*/,
  98. grpc_end2end_test_fixture f) {
  99. grpc_call* c;
  100. grpc_call* s;
  101. cq_verifier* cqv = cq_verifier_create(f.cq);
  102. grpc_op ops[6];
  103. grpc_op* op;
  104. grpc_metadata_array initial_metadata_recv;
  105. grpc_metadata_array trailing_metadata_recv;
  106. grpc_metadata_array request_metadata_recv;
  107. grpc_call_details call_details;
  108. grpc_status_code status;
  109. grpc_call_error error;
  110. grpc_slice details;
  111. int was_cancelled = 2;
  112. char* peer;
  113. gpr_timespec deadline = five_seconds_from_now();
  114. c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
  115. grpc_slice_from_static_string("/foo"), nullptr,
  116. deadline, nullptr);
  117. GPR_ASSERT(c);
  118. peer = grpc_call_get_peer(c);
  119. GPR_ASSERT(peer != nullptr);
  120. gpr_free(peer);
  121. grpc_metadata_array_init(&initial_metadata_recv);
  122. grpc_metadata_array_init(&trailing_metadata_recv);
  123. grpc_metadata_array_init(&request_metadata_recv);
  124. grpc_call_details_init(&call_details);
  125. memset(ops, 0, sizeof(ops));
  126. op = ops;
  127. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  128. op->data.send_initial_metadata.count = 0;
  129. op->flags = 0;
  130. op->reserved = nullptr;
  131. op++;
  132. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  133. op->flags = 0;
  134. op->reserved = nullptr;
  135. op++;
  136. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  137. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  138. op->flags = 0;
  139. op->reserved = nullptr;
  140. op++;
  141. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  142. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  143. op->data.recv_status_on_client.status = &status;
  144. op->data.recv_status_on_client.status_details = &details;
  145. op->flags = 0;
  146. op->reserved = nullptr;
  147. op++;
  148. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
  149. nullptr);
  150. GPR_ASSERT(GRPC_CALL_OK == error);
  151. error =
  152. grpc_server_request_call(f.server, &s, &call_details,
  153. &request_metadata_recv, f.cq, f.cq, tag(101));
  154. GPR_ASSERT(GRPC_CALL_OK == error);
  155. CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
  156. cq_verify(cqv);
  157. peer = grpc_call_get_peer(s);
  158. GPR_ASSERT(peer != nullptr);
  159. gpr_free(peer);
  160. peer = grpc_call_get_peer(c);
  161. GPR_ASSERT(peer != nullptr);
  162. gpr_free(peer);
  163. memset(ops, 0, sizeof(ops));
  164. op = ops;
  165. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  166. op->data.send_initial_metadata.count = 0;
  167. op->flags = 0;
  168. op->reserved = nullptr;
  169. op++;
  170. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  171. op->data.send_status_from_server.trailing_metadata_count = 0;
  172. op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
  173. grpc_slice status_details = grpc_slice_from_static_string("xyz");
  174. op->data.send_status_from_server.status_details = &status_details;
  175. op->flags = 0;
  176. op->reserved = nullptr;
  177. op++;
  178. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  179. op->data.recv_close_on_server.cancelled = &was_cancelled;
  180. op->flags = 0;
  181. op->reserved = nullptr;
  182. op++;
  183. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(102),
  184. nullptr);
  185. GPR_ASSERT(GRPC_CALL_OK == error);
  186. CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
  187. CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
  188. cq_verify(cqv);
  189. GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
  190. GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
  191. GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/foo"));
  192. GPR_ASSERT(0 == call_details.flags);
  193. GPR_ASSERT(was_cancelled == 0);
  194. grpc_slice_unref(details);
  195. grpc_metadata_array_destroy(&initial_metadata_recv);
  196. grpc_metadata_array_destroy(&trailing_metadata_recv);
  197. grpc_metadata_array_destroy(&request_metadata_recv);
  198. grpc_call_details_destroy(&call_details);
  199. grpc_call_unref(c);
  200. grpc_call_unref(s);
  201. cq_verifier_destroy(cqv);
  202. }
  203. static void test_invoke_simple_request(grpc_end2end_test_config config) {
  204. grpc_end2end_test_fixture f;
  205. f = begin_test(config, "test_invoke_simple_request_with_no_error_logging",
  206. nullptr, nullptr);
  207. simple_request_body(config, f);
  208. end_test(&f);
  209. config.tear_down_data(&f);
  210. }
  211. static void test_invoke_10_simple_requests(grpc_end2end_test_config config) {
  212. int i;
  213. grpc_end2end_test_fixture f =
  214. begin_test(config, "test_invoke_10_simple_requests_with_no_error_logging",
  215. nullptr, nullptr);
  216. for (i = 0; i < 10; i++) {
  217. simple_request_body(config, f);
  218. gpr_log(GPR_INFO, "Passed simple request %d", i);
  219. }
  220. simple_request_body(config, f);
  221. end_test(&f);
  222. config.tear_down_data(&f);
  223. }
  224. static void test_no_error_logging_in_entire_process(
  225. grpc_end2end_test_config config) {
  226. int i;
  227. gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)test_no_error_log);
  228. for (i = 0; i < 10; i++) {
  229. test_invoke_simple_request(config);
  230. }
  231. test_invoke_10_simple_requests(config);
  232. gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)gpr_default_log);
  233. }
  234. static void test_no_logging_in_one_request(grpc_end2end_test_config config) {
  235. int i;
  236. grpc_end2end_test_fixture f =
  237. begin_test(config, "test_no_logging_in_last_request", nullptr, nullptr);
  238. for (i = 0; i < 10; i++) {
  239. simple_request_body(config, f);
  240. }
  241. gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)test_no_log);
  242. simple_request_body(config, f);
  243. gpr_atm_no_barrier_store(&g_log_func, (gpr_atm)gpr_default_log);
  244. end_test(&f);
  245. config.tear_down_data(&f);
  246. }
  247. void no_logging(grpc_end2end_test_config config) {
  248. gpr_set_log_verbosity(GPR_LOG_SEVERITY_DEBUG);
  249. grpc_tracer_set_enabled("all", 0);
  250. gpr_set_log_function(log_dispatcher_func);
  251. test_no_logging_in_one_request(config);
  252. test_no_error_logging_in_entire_process(config);
  253. gpr_set_log_function(gpr_default_log);
  254. }
  255. void no_logging_pre_init(void) {}