combiner_test.cc
Go to the documentation of this file.
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 
20 
21 #include <grpc/grpc.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/log.h>
24 
26 #include "src/core/lib/gprpp/thd.h"
28 
29 static void test_no_op(void) {
30  gpr_log(GPR_DEBUG, "test_no_op");
33 }
34 
35 static void set_event_to_true(void* value, grpc_error_handle /*error*/) {
36  gpr_event_set(static_cast<gpr_event*>(value), reinterpret_cast<void*>(1));
37 }
38 
39 static void test_execute_one(void) {
40  gpr_log(GPR_DEBUG, "test_execute_one");
41 
50  nullptr);
51  GRPC_COMBINER_UNREF(lock, "test_execute_one");
52 }
53 
54 typedef struct {
55  size_t ctr;
58 } thd_args;
59 
60 typedef struct {
61  size_t* ctr;
62  size_t value;
63 } ex_args;
64 
65 static void check_one(void* a, grpc_error_handle /*error*/) {
66  ex_args* args = static_cast<ex_args*>(a);
67  GPR_ASSERT(*args->ctr == args->value - 1);
68  *args->ctr = args->value;
69  gpr_free(a);
70 }
71 
72 static void execute_many_loop(void* a) {
73  thd_args* args = static_cast<thd_args*>(a);
75  size_t n = 1;
76  for (size_t i = 0; i < 10; i++) {
77  for (size_t j = 0; j < 10000; j++) {
78  ex_args* c = static_cast<ex_args*>(gpr_malloc(sizeof(*c)));
79  c->ctr = &args->ctr;
80  c->value = n++;
81  args->lock->Run(GRPC_CLOSURE_CREATE(check_one, c, nullptr),
84  }
85  // sleep for a little bit, to test a combiner draining and another thread
86  // picking it up
88  }
89  args->lock->Run(GRPC_CLOSURE_CREATE(set_event_to_true, &args->done, nullptr),
91 }
92 
93 static void test_execute_many(void) {
94  gpr_log(GPR_DEBUG, "test_execute_many");
95 
97  grpc_core::Thread thds[100];
98  thd_args ta[GPR_ARRAY_SIZE(thds)];
99  for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) {
100  ta[i].ctr = 0;
101  ta[i].lock = lock;
102  gpr_event_init(&ta[i].done);
103  thds[i] = grpc_core::Thread("grpc_execute_many", execute_many_loop, &ta[i]);
104  thds[i].Start();
105  }
106  for (size_t i = 0; i < GPR_ARRAY_SIZE(thds); i++) {
108  gpr_inf_future(GPR_CLOCK_REALTIME)) != nullptr);
109  thds[i].Join();
110  }
112  GRPC_COMBINER_UNREF(lock, "test_execute_many");
113 }
114 
116 
117 static void in_finally(void* /*arg*/, grpc_error_handle /*error*/) {
118  gpr_event_set(&got_in_finally, reinterpret_cast<void*>(1));
119 }
120 
121 static void add_finally(void* arg, grpc_error_handle /*error*/) {
122  static_cast<grpc_core::Combiner*>(arg)->Run(
124 }
125 
126 static void test_execute_finally(void) {
127  gpr_log(GPR_DEBUG, "test_execute_finally");
128 
132  lock->Run(GRPC_CLOSURE_CREATE(add_finally, lock, nullptr), GRPC_ERROR_NONE);
135  grpc_timeout_seconds_to_deadline(5)) != nullptr);
136  GRPC_COMBINER_UNREF(lock, "test_execute_finally");
137 }
138 
139 int main(int argc, char** argv) {
140  grpc::testing::TestEnvironment env(&argc, argv);
141  grpc_init();
142  test_no_op();
146  grpc_shutdown();
147 
148  return 0;
149 }
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
grpc_timeout_seconds_to_deadline
gpr_timespec grpc_timeout_seconds_to_deadline(int64_t time_s)
Definition: test/core/util/test_config.cc:81
log.h
generate.env
env
Definition: generate.py:37
test_execute_many
static void test_execute_many(void)
Definition: combiner_test.cc:93
thd_args::done
gpr_event done
Definition: combiner_test.cc:57
in_finally
static void in_finally(void *, grpc_error_handle)
Definition: combiner_test.cc:117
test_execute_one
static void test_execute_one(void)
Definition: combiner_test.cc:39
gpr_event_set
GPRAPI void gpr_event_set(gpr_event *ev, void *value)
Definition: sync.cc:59
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
useful.h
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
GRPC_CLOSURE_CREATE
#define GRPC_CLOSURE_CREATE(cb, cb_arg, scheduler)
Definition: closure.h:160
main
int main(int argc, char **argv)
Definition: combiner_test.cc:139
execute_many_loop
static void execute_many_loop(void *a)
Definition: combiner_test.cc:72
thd_args::lock
grpc_core::Combiner * lock
Definition: combiner_test.cc:56
grpc_combiner_create
grpc_core::Combiner * grpc_combiner_create(void)
Definition: combiner.cc:54
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_core::ExecCtx::Flush
bool Flush()
Definition: exec_ctx.cc:69
grpc_timeout_milliseconds_to_deadline
gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms)
Definition: test/core/util/test_config.cc:89
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
gpr_sleep_until
GPRAPI void gpr_sleep_until(gpr_timespec until)
grpc.h
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
arg
Definition: cmdline.cc:40
grpc_core::Combiner::Run
void Run(grpc_closure *closure, grpc_error_handle error)
Definition: combiner.cc:343
grpc_core::Thread::Join
void Join()
Definition: thd.h:141
gpr_event_init
GPRAPI void gpr_event_init(gpr_event *ev)
Definition: sync.cc:54
ex_args::ctr
size_t * ctr
Definition: combiner_test.cc:61
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_core::Thread::Start
void Start()
Definition: thd.h:125
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
gpr_event_wait
GPRAPI void * gpr_event_wait(gpr_event *ev, gpr_timespec abs_deadline)
Definition: sync.cc:73
test_config.h
value
const char * value
Definition: hpack_parser_table.cc:165
GPR_ARRAY_SIZE
#define GPR_ARRAY_SIZE(array)
Definition: useful.h:129
gpr_event
Definition: impl/codegen/sync_generic.h:31
ex_args::value
size_t value
Definition: combiner_test.cc:62
set_event_to_true
static void set_event_to_true(void *value, grpc_error_handle)
Definition: combiner_test.cc:35
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
thd.h
combiner.h
ex_args
Definition: combiner_test.cc:60
arg
struct arg arg
grpc_core::Thread
Definition: thd.h:43
got_in_finally
static gpr_event got_in_finally
Definition: combiner_test.cc:115
grpc_core::Combiner
Definition: combiner.h:34
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
check_one
static void check_one(void *a, grpc_error_handle)
Definition: combiner_test.cc:65
googletest-break-on-failure-unittest.Run
def Run(command)
Definition: bloaty/third_party/googletest/googletest/test/googletest-break-on-failure-unittest.py:76
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_error
Definition: error_internal.h:42
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
test_no_op
static void test_no_op(void)
Definition: combiner_test.cc:29
thd_args
Definition: bad_client.cc:44
GRPC_COMBINER_UNREF
#define GRPC_COMBINER_UNREF(combiner, reason)
Definition: combiner.h:71
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
add_finally
static void add_finally(void *arg, grpc_error_handle)
Definition: combiner_test.cc:121
thd_args::ctr
size_t ctr
Definition: mpscq_test.cc:62
test_execute_finally
static void test_execute_finally(void)
Definition: combiner_test.cc:126


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:50