Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
| 34 | #include "src/core/surface/completion_queue.h" |
| 35 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 36 | #include "src/core/iomgr/iomgr.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | #include <grpc/support/alloc.h> |
| 38 | #include <grpc/support/log.h> |
| 39 | #include <grpc/support/thd.h> |
| 40 | #include <grpc/support/time.h> |
| 41 | #include <grpc/support/useful.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | #include "test/core/util/test_config.h" |
| 43 | |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 44 | #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 46 | static void *create_test_tag(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | static gpr_intptr i = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 48 | return (void *)(++i); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /* helper for tests to shutdown correctly and tersely */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 52 | static void shutdown_and_destroy(grpc_completion_queue *cc) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 53 | grpc_event ev; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 54 | grpc_completion_queue_shutdown(cc); |
| 55 | ev = grpc_completion_queue_next(cc, gpr_inf_past(GPR_CLOCK_REALTIME), NULL); |
| 56 | GPR_ASSERT(ev.type == GRPC_QUEUE_SHUTDOWN); |
| 57 | grpc_completion_queue_destroy(cc); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* ensure we can create and destroy a completion channel */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 61 | static void test_no_op(void) { |
| 62 | LOG_TEST("test_no_op"); |
| 63 | shutdown_and_destroy(grpc_completion_queue_create(NULL)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 66 | static void test_wait_empty(void) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | grpc_completion_queue *cc; |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 68 | grpc_event event; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 69 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 70 | LOG_TEST("test_wait_empty"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 71 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 72 | cc = grpc_completion_queue_create(NULL); |
| 73 | event = grpc_completion_queue_next(cc, gpr_now(GPR_CLOCK_REALTIME), NULL); |
| 74 | GPR_ASSERT(event.type == GRPC_QUEUE_TIMEOUT); |
| 75 | shutdown_and_destroy(cc); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 78 | static void do_nothing_end_completion(grpc_exec_ctx *exec_ctx, void *arg, |
| 79 | grpc_cq_completion *c) {} |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 80 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 81 | static void test_cq_end_op(void) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 82 | grpc_event ev; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | grpc_completion_queue *cc; |
Craig Tiller | 97fc6a3 | 2015-07-08 15:31:35 -0700 | [diff] [blame] | 84 | grpc_cq_completion completion; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 85 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 86 | void *tag = create_test_tag(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 87 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 88 | LOG_TEST("test_cq_end_op"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 89 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 90 | cc = grpc_completion_queue_create(NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 91 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 92 | grpc_cq_begin_op(cc); |
| 93 | grpc_cq_end_op(&exec_ctx, cc, tag, 1, do_nothing_end_completion, NULL, |
| 94 | &completion); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 95 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 96 | ev = grpc_completion_queue_next(cc, gpr_inf_past(GPR_CLOCK_REALTIME), NULL); |
| 97 | GPR_ASSERT(ev.type == GRPC_OP_COMPLETE); |
| 98 | GPR_ASSERT(ev.tag == tag); |
| 99 | GPR_ASSERT(ev.success); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 100 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 101 | shutdown_and_destroy(cc); |
Craig Tiller | b8b1a46 | 2015-09-22 12:50:03 -0700 | [diff] [blame] | 102 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 103 | } |
| 104 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 105 | static void test_shutdown_then_next_polling(void) { |
Craig Tiller | 59657db | 2015-06-01 20:01:15 -0700 | [diff] [blame] | 106 | grpc_completion_queue *cc; |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 107 | grpc_event event; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | LOG_TEST("test_shutdown_then_next_polling"); |
Craig Tiller | 59657db | 2015-06-01 20:01:15 -0700 | [diff] [blame] | 109 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 110 | cc = grpc_completion_queue_create(NULL); |
| 111 | grpc_completion_queue_shutdown(cc); |
| 112 | event = |
| 113 | grpc_completion_queue_next(cc, gpr_inf_past(GPR_CLOCK_REALTIME), NULL); |
| 114 | GPR_ASSERT(event.type == GRPC_QUEUE_SHUTDOWN); |
| 115 | grpc_completion_queue_destroy(cc); |
Craig Tiller | 59657db | 2015-06-01 20:01:15 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 118 | static void test_shutdown_then_next_with_timeout(void) { |
Craig Tiller | c0c4844 | 2015-06-01 20:04:36 -0700 | [diff] [blame] | 119 | grpc_completion_queue *cc; |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 120 | grpc_event event; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 121 | LOG_TEST("test_shutdown_then_next_with_timeout"); |
Craig Tiller | c0c4844 | 2015-06-01 20:04:36 -0700 | [diff] [blame] | 122 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | cc = grpc_completion_queue_create(NULL); |
| 124 | grpc_completion_queue_shutdown(cc); |
| 125 | event = |
| 126 | grpc_completion_queue_next(cc, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
| 127 | GPR_ASSERT(event.type == GRPC_QUEUE_SHUTDOWN); |
| 128 | grpc_completion_queue_destroy(cc); |
Craig Tiller | c0c4844 | 2015-06-01 20:04:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 131 | static void test_pluck(void) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 132 | grpc_event ev; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 133 | grpc_completion_queue *cc; |
| 134 | void *tags[128]; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 135 | grpc_cq_completion completions[GPR_ARRAY_SIZE(tags)]; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 136 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Nicolas "Pixel" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame] | 137 | unsigned i, j; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 138 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 139 | LOG_TEST("test_pluck"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 140 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 141 | for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) { |
| 142 | tags[i] = create_test_tag(); |
| 143 | for (j = 0; j < i; j++) { |
| 144 | GPR_ASSERT(tags[i] != tags[j]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 145 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 146 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 147 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 148 | cc = grpc_completion_queue_create(NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 149 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 150 | for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) { |
| 151 | grpc_cq_begin_op(cc); |
| 152 | grpc_cq_end_op(&exec_ctx, cc, tags[i], 1, do_nothing_end_completion, NULL, |
| 153 | &completions[i]); |
| 154 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 155 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 156 | for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) { |
| 157 | ev = grpc_completion_queue_pluck(cc, tags[i], |
| 158 | gpr_inf_past(GPR_CLOCK_REALTIME), NULL); |
| 159 | GPR_ASSERT(ev.tag == tags[i]); |
| 160 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 161 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 162 | for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) { |
| 163 | grpc_cq_begin_op(cc); |
| 164 | grpc_cq_end_op(&exec_ctx, cc, tags[i], 1, do_nothing_end_completion, NULL, |
| 165 | &completions[i]); |
| 166 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 167 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 168 | for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) { |
| 169 | ev = grpc_completion_queue_pluck(cc, tags[GPR_ARRAY_SIZE(tags) - i - 1], |
| 170 | gpr_inf_past(GPR_CLOCK_REALTIME), NULL); |
| 171 | GPR_ASSERT(ev.tag == tags[GPR_ARRAY_SIZE(tags) - i - 1]); |
| 172 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 173 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 174 | shutdown_and_destroy(cc); |
Craig Tiller | b8b1a46 | 2015-09-22 12:50:03 -0700 | [diff] [blame] | 175 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 176 | } |
| 177 | |
yang-g | 1ed62e4 | 2015-12-08 08:00:24 -0800 | [diff] [blame] | 178 | static void test_pluck_after_shutdown(void) { |
| 179 | grpc_event ev; |
| 180 | grpc_completion_queue *cc; |
| 181 | |
| 182 | LOG_TEST("test_pluck_after_shutdown"); |
| 183 | cc = grpc_completion_queue_create(NULL); |
| 184 | grpc_completion_queue_shutdown(cc); |
| 185 | ev = grpc_completion_queue_pluck(cc, NULL, gpr_inf_future(GPR_CLOCK_REALTIME), |
| 186 | NULL); |
| 187 | GPR_ASSERT(ev.type == GRPC_QUEUE_SHUTDOWN); |
| 188 | grpc_completion_queue_destroy(cc); |
| 189 | } |
| 190 | |
yang-g | 7c6d1ad | 2015-12-08 16:07:05 -0800 | [diff] [blame] | 191 | struct thread_state { |
| 192 | grpc_completion_queue *cc; |
| 193 | void *tag; |
| 194 | }; |
| 195 | |
| 196 | static void pluck_one(void *arg) { |
| 197 | struct thread_state *state = arg; |
| 198 | grpc_completion_queue_pluck(state->cc, state->tag, |
| 199 | gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
| 200 | } |
| 201 | |
| 202 | static void test_too_many_plucks(void) { |
| 203 | grpc_event ev; |
| 204 | grpc_completion_queue *cc; |
| 205 | void *tags[GRPC_MAX_COMPLETION_QUEUE_PLUCKERS]; |
| 206 | grpc_cq_completion completions[GPR_ARRAY_SIZE(tags)]; |
| 207 | gpr_thd_id thread_ids[GPR_ARRAY_SIZE(tags)]; |
| 208 | struct thread_state thread_states[GPR_ARRAY_SIZE(tags)]; |
| 209 | gpr_thd_options thread_options = gpr_thd_options_default(); |
| 210 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 211 | unsigned i, j; |
| 212 | |
| 213 | LOG_TEST("test_too_many_plucks"); |
| 214 | |
| 215 | cc = grpc_completion_queue_create(NULL); |
| 216 | gpr_thd_options_set_joinable(&thread_options); |
| 217 | |
| 218 | for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) { |
| 219 | tags[i] = create_test_tag(); |
| 220 | for (j = 0; j < i; j++) { |
| 221 | GPR_ASSERT(tags[i] != tags[j]); |
| 222 | } |
| 223 | thread_states[i].cc = cc; |
| 224 | thread_states[i].tag = tags[i]; |
| 225 | gpr_thd_new(thread_ids + i, pluck_one, thread_states + i, &thread_options); |
| 226 | } |
| 227 | |
| 228 | /* wait until all other threads are plucking */ |
| 229 | gpr_sleep_until(GRPC_TIMEOUT_MILLIS_TO_DEADLINE(100)); |
| 230 | |
| 231 | ev = grpc_completion_queue_pluck(cc, create_test_tag(), |
| 232 | gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
| 233 | GPR_ASSERT(ev.type == GRPC_QUEUE_TIMEOUT); |
| 234 | |
| 235 | for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) { |
| 236 | grpc_cq_begin_op(cc); |
| 237 | grpc_cq_end_op(&exec_ctx, cc, tags[i], 1, do_nothing_end_completion, NULL, |
| 238 | &completions[i]); |
| 239 | } |
| 240 | |
| 241 | for (i = 0; i < GPR_ARRAY_SIZE(tags); i++) { |
| 242 | gpr_thd_join(thread_ids[i]); |
| 243 | } |
| 244 | |
| 245 | shutdown_and_destroy(cc); |
| 246 | grpc_exec_ctx_finish(&exec_ctx); |
| 247 | } |
| 248 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 249 | #define TEST_THREAD_EVENTS 10000 |
| 250 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 251 | typedef struct test_thread_options { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 252 | gpr_event on_started; |
| 253 | gpr_event *phase1; |
| 254 | gpr_event on_phase1_done; |
| 255 | gpr_event *phase2; |
| 256 | gpr_event on_finished; |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 257 | size_t events_triggered; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 258 | int id; |
| 259 | grpc_completion_queue *cc; |
| 260 | } test_thread_options; |
| 261 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 262 | gpr_timespec ten_seconds_time(void) { |
| 263 | return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 266 | static void free_completion(grpc_exec_ctx *exec_ctx, void *arg, |
| 267 | grpc_cq_completion *completion) { |
| 268 | gpr_free(completion); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 271 | static void producer_thread(void *arg) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 272 | test_thread_options *opt = arg; |
| 273 | int i; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 274 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 275 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 276 | gpr_log(GPR_INFO, "producer %d started", opt->id); |
| 277 | gpr_event_set(&opt->on_started, (void *)(gpr_intptr)1); |
| 278 | GPR_ASSERT(gpr_event_wait(opt->phase1, ten_seconds_time())); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 279 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 280 | gpr_log(GPR_INFO, "producer %d phase 1", opt->id); |
| 281 | for (i = 0; i < TEST_THREAD_EVENTS; i++) { |
| 282 | grpc_cq_begin_op(opt->cc); |
| 283 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 284 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 285 | gpr_log(GPR_INFO, "producer %d phase 1 done", opt->id); |
| 286 | gpr_event_set(&opt->on_phase1_done, (void *)(gpr_intptr)1); |
| 287 | GPR_ASSERT(gpr_event_wait(opt->phase2, ten_seconds_time())); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 288 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 289 | gpr_log(GPR_INFO, "producer %d phase 2", opt->id); |
| 290 | for (i = 0; i < TEST_THREAD_EVENTS; i++) { |
Craig Tiller | b8b1a46 | 2015-09-22 12:50:03 -0700 | [diff] [blame] | 291 | grpc_cq_end_op(&exec_ctx, opt->cc, (void *)(gpr_intptr)1, 1, |
| 292 | free_completion, NULL, |
| 293 | gpr_malloc(sizeof(grpc_cq_completion))); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 294 | opt->events_triggered++; |
| 295 | grpc_exec_ctx_finish(&exec_ctx); |
| 296 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 297 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 298 | gpr_log(GPR_INFO, "producer %d phase 2 done", opt->id); |
| 299 | gpr_event_set(&opt->on_finished, (void *)(gpr_intptr)1); |
Craig Tiller | b8b1a46 | 2015-09-22 12:50:03 -0700 | [diff] [blame] | 300 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 303 | static void consumer_thread(void *arg) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 304 | test_thread_options *opt = arg; |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 305 | grpc_event ev; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 306 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 307 | gpr_log(GPR_INFO, "consumer %d started", opt->id); |
| 308 | gpr_event_set(&opt->on_started, (void *)(gpr_intptr)1); |
| 309 | GPR_ASSERT(gpr_event_wait(opt->phase1, ten_seconds_time())); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 310 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 311 | gpr_log(GPR_INFO, "consumer %d phase 1", opt->id); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 312 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 313 | gpr_log(GPR_INFO, "consumer %d phase 1 done", opt->id); |
| 314 | gpr_event_set(&opt->on_phase1_done, (void *)(gpr_intptr)1); |
| 315 | GPR_ASSERT(gpr_event_wait(opt->phase2, ten_seconds_time())); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 316 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 317 | gpr_log(GPR_INFO, "consumer %d phase 2", opt->id); |
| 318 | for (;;) { |
| 319 | ev = grpc_completion_queue_next(opt->cc, ten_seconds_time(), NULL); |
| 320 | switch (ev.type) { |
| 321 | case GRPC_OP_COMPLETE: |
| 322 | GPR_ASSERT(ev.success); |
| 323 | opt->events_triggered++; |
| 324 | break; |
| 325 | case GRPC_QUEUE_SHUTDOWN: |
| 326 | gpr_log(GPR_INFO, "consumer %d phase 2 done", opt->id); |
| 327 | gpr_event_set(&opt->on_finished, (void *)(gpr_intptr)1); |
| 328 | return; |
| 329 | case GRPC_QUEUE_TIMEOUT: |
| 330 | gpr_log(GPR_ERROR, "Invalid timeout received"); |
| 331 | abort(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 332 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 333 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 336 | static void test_threading(size_t producers, size_t consumers) { |
| 337 | test_thread_options *options = |
| 338 | gpr_malloc((producers + consumers) * sizeof(test_thread_options)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 339 | gpr_event phase1 = GPR_EVENT_INIT; |
| 340 | gpr_event phase2 = GPR_EVENT_INIT; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 341 | grpc_completion_queue *cc = grpc_completion_queue_create(NULL); |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 342 | size_t i; |
| 343 | size_t total_consumed = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 344 | static int optid = 101; |
| 345 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 346 | gpr_log(GPR_INFO, "%s: %d producers, %d consumers", "test_threading", |
| 347 | producers, consumers); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 348 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 349 | /* start all threads: they will wait for phase1 */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 350 | for (i = 0; i < producers + consumers; i++) { |
| 351 | gpr_thd_id id; |
| 352 | gpr_event_init(&options[i].on_started); |
| 353 | gpr_event_init(&options[i].on_phase1_done); |
| 354 | gpr_event_init(&options[i].on_finished); |
| 355 | options[i].phase1 = &phase1; |
| 356 | options[i].phase2 = &phase2; |
| 357 | options[i].events_triggered = 0; |
| 358 | options[i].cc = cc; |
| 359 | options[i].id = optid++; |
| 360 | GPR_ASSERT(gpr_thd_new(&id, |
| 361 | i < producers ? producer_thread : consumer_thread, |
| 362 | options + i, NULL)); |
| 363 | gpr_event_wait(&options[i].on_started, ten_seconds_time()); |
| 364 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 365 | |
| 366 | /* start phase1: producers will pre-declare all operations they will |
| 367 | complete */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 368 | gpr_log(GPR_INFO, "start phase 1"); |
| 369 | gpr_event_set(&phase1, (void *)(gpr_intptr)1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 370 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 371 | gpr_log(GPR_INFO, "wait phase 1"); |
| 372 | for (i = 0; i < producers + consumers; i++) { |
| 373 | GPR_ASSERT(gpr_event_wait(&options[i].on_phase1_done, ten_seconds_time())); |
| 374 | } |
| 375 | gpr_log(GPR_INFO, "done phase 1"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 376 | |
| 377 | /* start phase2: operations will complete, and consumers will consume them */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 378 | gpr_log(GPR_INFO, "start phase 2"); |
| 379 | gpr_event_set(&phase2, (void *)(gpr_intptr)1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 380 | |
| 381 | /* in parallel, we shutdown the completion channel - all events should still |
| 382 | be consumed */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 383 | grpc_completion_queue_shutdown(cc); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 384 | |
| 385 | /* join all threads */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 386 | gpr_log(GPR_INFO, "wait phase 2"); |
| 387 | for (i = 0; i < producers + consumers; i++) { |
| 388 | GPR_ASSERT(gpr_event_wait(&options[i].on_finished, ten_seconds_time())); |
| 389 | } |
| 390 | gpr_log(GPR_INFO, "done phase 2"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 391 | |
| 392 | /* destroy the completion channel */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 393 | grpc_completion_queue_destroy(cc); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 394 | |
| 395 | /* verify that everything was produced and consumed */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 396 | for (i = 0; i < producers + consumers; i++) { |
| 397 | if (i < producers) { |
| 398 | GPR_ASSERT(options[i].events_triggered == TEST_THREAD_EVENTS); |
| 399 | } else { |
| 400 | total_consumed += options[i].events_triggered; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 401 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 402 | } |
| 403 | GPR_ASSERT(total_consumed == producers * TEST_THREAD_EVENTS); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 404 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 405 | gpr_free(options); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 408 | int main(int argc, char **argv) { |
| 409 | grpc_test_init(argc, argv); |
| 410 | grpc_init(); |
| 411 | test_no_op(); |
| 412 | test_wait_empty(); |
| 413 | test_shutdown_then_next_polling(); |
| 414 | test_shutdown_then_next_with_timeout(); |
| 415 | test_cq_end_op(); |
| 416 | test_pluck(); |
yang-g | 1ed62e4 | 2015-12-08 08:00:24 -0800 | [diff] [blame] | 417 | test_pluck_after_shutdown(); |
yang-g | 7c6d1ad | 2015-12-08 16:07:05 -0800 | [diff] [blame] | 418 | test_too_many_plucks(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 419 | test_threading(1, 1); |
| 420 | test_threading(1, 10); |
| 421 | test_threading(10, 1); |
| 422 | test_threading(10, 10); |
| 423 | grpc_shutdown(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 424 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 425 | } |