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