combiner_test.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "src/core/lib/iomgr/combiner.h"
  19. #include <grpc/grpc.h>
  20. #include <grpc/support/alloc.h>
  21. #include <grpc/support/log.h>
  22. #include "src/core/lib/gpr/useful.h"
  23. #include "src/core/lib/gprpp/thd.h"
  24. #include "test/core/util/test_config.h"
  25. static void test_no_op(void) {
  26. gpr_log(GPR_DEBUG, "test_no_op");
  27. grpc_core::ExecCtx exec_ctx;
  28. GRPC_COMBINER_UNREF(grpc_combiner_create(), "test_no_op");
  29. }
  30. static void set_event_to_true(void* value, grpc_error_handle /*error*/) {
  31. gpr_event_set(static_cast<gpr_event*>(value), reinterpret_cast<void*>(1));
  32. }
  33. static void test_execute_one(void) {
  34. gpr_log(GPR_DEBUG, "test_execute_one");
  35. grpc_core::Combiner* lock = grpc_combiner_create();
  36. gpr_event done;
  37. gpr_event_init(&done);
  38. grpc_core::ExecCtx exec_ctx;
  39. lock->Run(GRPC_CLOSURE_CREATE(set_event_to_true, &done, nullptr),
  40. GRPC_ERROR_NONE);
  41. grpc_core::ExecCtx::Get()->Flush();
  42. GPR_ASSERT(gpr_event_wait(&done, grpc_timeout_seconds_to_deadline(5)) !=
  43. nullptr);
  44. GRPC_COMBINER_UNREF(lock, "test_execute_one");
  45. }
  46. typedef struct {
  47. size_t ctr;
  48. grpc_core::Combiner* lock;
  49. gpr_event done;
  50. } thd_args;
  51. typedef struct {
  52. size_t* ctr;
  53. size_t value;
  54. } ex_args;
  55. static void check_one(void* a, grpc_error_handle /*error*/) {
  56. ex_args* args = static_cast<ex_args*>(a);
  57. GPR_ASSERT(*args->ctr == args->value - 1);
  58. *args->ctr = args->value;
  59. gpr_free(a);
  60. }
  61. static void execute_many_loop(void* a) {
  62. thd_args* args = static_cast<thd_args*>(a);
  63. grpc_core::ExecCtx exec_ctx;
  64. size_t n = 1;
  65. for (size_t i = 0; i < 10; i++) {
  66. for (size_t j = 0; j < 10000; j++) {
  67. ex_args* c = static_cast<ex_args*>(gpr_malloc(sizeof(*c)));
  68. c->ctr = &args->ctr;
  69. c->value = n++;
  70. args->lock->Run(GRPC_CLOSURE_CREATE(check_one, c, nullptr),
  71. GRPC_ERROR_NONE);
  72. grpc_core::ExecCtx::Get()->Flush();
  73. }
  74. // sleep for a little bit, to test a combiner draining and another thread
  75. // picking it up
  76. gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
  77. }
  78. args->lock->Run(GRPC_CLOSURE_CREATE(set_event_to_true, &args->done, nullptr),
  79. GRPC_ERROR_NONE);
  80. }
  81. static void test_execute_many(void) {
  82. gpr_log(GPR_DEBUG, "test_execute_many");
  83. grpc_core::Combiner* lock = grpc_combiner_create();
  84. grpc_core::Thread thds[100];
  85. thd_args ta[GPR_ARRAY_SIZE(thds)];
  86. for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) {
  87. ta[i].ctr = 0;
  88. ta[i].lock = lock;
  89. gpr_event_init(&ta[i].done);
  90. thds[i] = grpc_core::Thread("grpc_execute_many", execute_many_loop, &ta[i]);
  91. thds[i].Start();
  92. }
  93. for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) {
  94. GPR_ASSERT(gpr_event_wait(&ta[i].done,
  95. gpr_inf_future(GPR_CLOCK_REALTIME)) != nullptr);
  96. thds[i].Join();
  97. }
  98. grpc_core::ExecCtx exec_ctx;
  99. GRPC_COMBINER_UNREF(lock, "test_execute_many");
  100. }
  101. static gpr_event got_in_finally;
  102. static void in_finally(void* /*arg*/, grpc_error_handle /*error*/) {
  103. gpr_event_set(&got_in_finally, reinterpret_cast<void*>(1));
  104. }
  105. static void add_finally(void* arg, grpc_error_handle /*error*/) {
  106. static_cast<grpc_core::Combiner*>(arg)->Run(
  107. GRPC_CLOSURE_CREATE(in_finally, arg, nullptr), GRPC_ERROR_NONE);
  108. }
  109. static void test_execute_finally(void) {
  110. gpr_log(GPR_DEBUG, "test_execute_finally");
  111. grpc_core::Combiner* lock = grpc_combiner_create();
  112. grpc_core::ExecCtx exec_ctx;
  113. gpr_event_init(&got_in_finally);
  114. lock->Run(GRPC_CLOSURE_CREATE(add_finally, lock, nullptr), GRPC_ERROR_NONE);
  115. grpc_core::ExecCtx::Get()->Flush();
  116. GPR_ASSERT(gpr_event_wait(&got_in_finally,
  117. grpc_timeout_seconds_to_deadline(5)) != nullptr);
  118. GRPC_COMBINER_UNREF(lock, "test_execute_finally");
  119. }
  120. int main(int argc, char** argv) {
  121. grpc::testing::TestEnvironment env(argc, argv);
  122. grpc_init();
  123. test_no_op();
  124. test_execute_one();
  125. test_execute_finally();
  126. test_execute_many();
  127. grpc_shutdown();
  128. return 0;
  129. }