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 <grpc/grpc.h>
22 #include <grpc/support/alloc.h>
23 #include <gtest/gtest.h>
24
25 #include <thread>
26
27 #include "src/core/util/crash.h"
28 #include "src/core/util/notification.h"
29 #include "src/core/util/thd.h"
30 #include "src/core/util/useful.h"
31 #include "test/core/test_util/test_config.h"
32
TEST(CombinerTest,TestNoOp)33 TEST(CombinerTest, TestNoOp) {
34 grpc_core::ExecCtx exec_ctx;
35 GRPC_COMBINER_UNREF(grpc_combiner_create(
36 grpc_event_engine::experimental::CreateEventEngine()),
37 "test_no_op");
38 }
39
set_event_to_true(void * value,grpc_error_handle)40 static void set_event_to_true(void* value, grpc_error_handle /*error*/) {
41 gpr_event_set(static_cast<gpr_event*>(value), reinterpret_cast<void*>(1));
42 }
43
TEST(CombinerTest,TestExecuteOne)44 TEST(CombinerTest, TestExecuteOne) {
45 grpc_core::Combiner* lock = grpc_combiner_create(
46 grpc_event_engine::experimental::CreateEventEngine());
47 gpr_event done;
48 gpr_event_init(&done);
49 grpc_core::ExecCtx exec_ctx;
50 lock->Run(GRPC_CLOSURE_CREATE(set_event_to_true, &done, nullptr),
51 absl::OkStatus());
52 grpc_core::ExecCtx::Get()->Flush();
53 ASSERT_NE(gpr_event_wait(&done, grpc_timeout_seconds_to_deadline(5)),
54 nullptr);
55 GRPC_COMBINER_UNREF(lock, "test_execute_one");
56 }
57
58 typedef struct {
59 size_t ctr;
60 grpc_core::Combiner* lock;
61 gpr_event done;
62 } thd_args;
63
64 typedef struct {
65 size_t* ctr;
66 size_t value;
67 } ex_args;
68
check_one(void * a,grpc_error_handle)69 static void check_one(void* a, grpc_error_handle /*error*/) {
70 ex_args* args = static_cast<ex_args*>(a);
71 ASSERT_EQ(*args->ctr, args->value - 1);
72 *args->ctr = args->value;
73 gpr_free(a);
74 }
75
execute_many_loop(void * a)76 static void execute_many_loop(void* a) {
77 thd_args* args = static_cast<thd_args*>(a);
78 grpc_core::ExecCtx exec_ctx;
79 size_t n = 1;
80 for (size_t i = 0; i < 10; i++) {
81 for (size_t j = 0; j < 10000; j++) {
82 ex_args* c = static_cast<ex_args*>(gpr_malloc(sizeof(*c)));
83 c->ctr = &args->ctr;
84 c->value = n++;
85 args->lock->Run(GRPC_CLOSURE_CREATE(check_one, c, nullptr),
86 absl::OkStatus());
87 grpc_core::ExecCtx::Get()->Flush();
88 }
89 // sleep for a little bit, to test a combiner draining and another thread
90 // picking it up
91 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(100));
92 }
93 args->lock->Run(GRPC_CLOSURE_CREATE(set_event_to_true, &args->done, nullptr),
94 absl::OkStatus());
95 }
96
TEST(CombinerTest,TestExecuteMany)97 TEST(CombinerTest, TestExecuteMany) {
98 grpc_core::Combiner* lock = grpc_combiner_create(
99 grpc_event_engine::experimental::CreateEventEngine());
100 grpc_core::Thread thds[10];
101 thd_args ta[GPR_ARRAY_SIZE(thds)];
102 for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) {
103 ta[i].ctr = 0;
104 ta[i].lock = lock;
105 gpr_event_init(&ta[i].done);
106 thds[i] = grpc_core::Thread("grpc_execute_many", execute_many_loop, &ta[i]);
107 thds[i].Start();
108 }
109 for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) {
110 ASSERT_NE(gpr_event_wait(&ta[i].done, gpr_inf_future(GPR_CLOCK_REALTIME)),
111 nullptr);
112 thds[i].Join();
113 }
114 grpc_core::ExecCtx exec_ctx;
115 GRPC_COMBINER_UNREF(lock, "test_execute_many");
116 }
117
118 static gpr_event got_in_finally;
119
in_finally(void *,grpc_error_handle)120 static void in_finally(void* /*arg*/, grpc_error_handle /*error*/) {
121 gpr_event_set(&got_in_finally, reinterpret_cast<void*>(1));
122 }
123
add_finally(void * arg,grpc_error_handle)124 static void add_finally(void* arg, grpc_error_handle /*error*/) {
125 static_cast<grpc_core::Combiner*>(arg)->FinallyRun(
126 GRPC_CLOSURE_CREATE(in_finally, arg, nullptr), absl::OkStatus());
127 }
128
TEST(CombinerTest,TestExecuteFinally)129 TEST(CombinerTest, TestExecuteFinally) {
130 grpc_core::Combiner* lock = grpc_combiner_create(
131 grpc_event_engine::experimental::CreateEventEngine());
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
TEST(CombinerTest,TestForceOffload)142 TEST(CombinerTest, TestForceOffload) {
143 grpc_core::Combiner* lock = grpc_combiner_create(
144 grpc_event_engine::experimental::CreateEventEngine());
145 grpc_core::ExecCtx exec_ctx;
146 grpc_core::Notification done;
147 const auto start_thread = std::this_thread::get_id();
148 lock->Run(grpc_core::NewClosure([&](grpc_error_handle) {
149 // Initial execution should get done in the exec ctx flush below,
150 // so thread stays the same.
151 EXPECT_EQ(start_thread, std::this_thread::get_id());
152 lock->Run(grpc_core::NewClosure([&](grpc_error_handle) {
153 // Next one should stick to the same thread too
154 // (proves we're not offloading all the time).
155 EXPECT_EQ(start_thread, std::this_thread::get_id());
156 // Force the offload.
157 lock->ForceOffload();
158 lock->Run(
159 grpc_core::NewClosure([&](grpc_error_handle) {
160 // We should see *not* the starting thread being
161 // the executor now.
162 EXPECT_NE(start_thread,
163 std::this_thread::get_id());
164 done.Notify();
165 }),
166 absl::OkStatus());
167 }),
168 absl::OkStatus());
169 }),
170 absl::OkStatus());
171 exec_ctx.Flush();
172 done.WaitForNotification();
173 GRPC_COMBINER_UNREF(lock, "test_force_offload");
174 }
175
main(int argc,char ** argv)176 int main(int argc, char** argv) {
177 grpc::testing::TestEnvironment env(&argc, argv);
178 ::testing::InitGoogleTest(&argc, argv);
179 grpc::testing::TestGrpcScope grpc_scope;
180 return RUN_ALL_TESTS();
181 }
182