combiner_test.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "src/core/lib/iomgr/combiner.h"
  34. #include <grpc/grpc.h>
  35. #include <grpc/support/alloc.h>
  36. #include <grpc/support/log.h>
  37. #include <grpc/support/thd.h>
  38. #include <grpc/support/useful.h>
  39. #include "test/core/util/test_config.h"
  40. static void test_no_op(void) {
  41. gpr_log(GPR_DEBUG, "test_no_op");
  42. grpc_combiner_destroy(grpc_combiner_create(NULL));
  43. }
  44. static void set_bool_to_true(grpc_exec_ctx *exec_ctx, void *value,
  45. grpc_error *error) {
  46. *(bool *)value = true;
  47. }
  48. static void test_execute_one(void) {
  49. gpr_log(GPR_DEBUG, "test_execute_one");
  50. grpc_combiner *lock = grpc_combiner_create(NULL);
  51. bool done = false;
  52. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  53. grpc_combiner_execute(&exec_ctx, lock,
  54. grpc_closure_create(set_bool_to_true, &done),
  55. GRPC_ERROR_NONE);
  56. grpc_exec_ctx_finish(&exec_ctx);
  57. GPR_ASSERT(done);
  58. grpc_combiner_destroy(lock);
  59. }
  60. typedef struct {
  61. size_t ctr;
  62. grpc_combiner *lock;
  63. } thd_args;
  64. typedef struct {
  65. size_t *ctr;
  66. size_t value;
  67. } ex_args;
  68. static void check_one(grpc_exec_ctx *exec_ctx, void *a, grpc_error *error) {
  69. ex_args *args = a;
  70. // gpr_log(GPR_DEBUG, "*%p=%d; step %d", args->ctr, *args->ctr, args->value);
  71. GPR_ASSERT(*args->ctr == args->value - 1);
  72. *args->ctr = args->value;
  73. gpr_free(a);
  74. }
  75. static void execute_many_loop(void *a) {
  76. thd_args *args = a;
  77. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  78. size_t n = 1;
  79. for (size_t i = 0; i < 10; i++) {
  80. for (size_t j = 0; j < 10000; j++) {
  81. ex_args *c = gpr_malloc(sizeof(*a));
  82. c->ctr = &args->ctr;
  83. c->value = n++;
  84. grpc_combiner_execute(&exec_ctx, args->lock,
  85. grpc_closure_create(check_one, c), GRPC_ERROR_NONE);
  86. grpc_exec_ctx_flush(&exec_ctx);
  87. }
  88. gpr_sleep_until(GRPC_TIMEOUT_MILLIS_TO_DEADLINE(100));
  89. }
  90. grpc_exec_ctx_finish(&exec_ctx);
  91. }
  92. static void test_execute_many(void) {
  93. gpr_log(GPR_DEBUG, "test_execute_many");
  94. grpc_combiner *lock = grpc_combiner_create(NULL);
  95. gpr_thd_id thds[100];
  96. thd_args ta[GPR_ARRAY_SIZE(thds)];
  97. for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) {
  98. gpr_thd_options options = gpr_thd_options_default();
  99. gpr_thd_options_set_joinable(&options);
  100. ta[i].ctr = 0;
  101. ta[i].lock = lock;
  102. GPR_ASSERT(gpr_thd_new(&thds[i], execute_many_loop, &ta[i], &options));
  103. }
  104. for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) {
  105. gpr_thd_join(thds[i]);
  106. }
  107. grpc_combiner_destroy(lock);
  108. }
  109. static bool got_in_finally = false;
  110. static void in_finally(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
  111. got_in_finally = true;
  112. }
  113. static void add_finally(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
  114. grpc_combiner_execute_finally(exec_ctx, arg,
  115. grpc_closure_create(in_finally, NULL),
  116. GRPC_ERROR_NONE, false);
  117. }
  118. static void test_execute_finally(void) {
  119. gpr_log(GPR_DEBUG, "test_execute_finally");
  120. grpc_combiner *lock = grpc_combiner_create(NULL);
  121. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  122. grpc_combiner_execute(&exec_ctx, lock, grpc_closure_create(add_finally, lock),
  123. GRPC_ERROR_NONE);
  124. grpc_exec_ctx_finish(&exec_ctx);
  125. GPR_ASSERT(got_in_finally);
  126. grpc_combiner_destroy(lock);
  127. }
  128. int main(int argc, char **argv) {
  129. grpc_test_init(argc, argv);
  130. grpc_init();
  131. test_no_op();
  132. test_execute_one();
  133. test_execute_finally();
  134. test_execute_many();
  135. grpc_shutdown();
  136. return 0;
  137. }