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 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 191 | #define TEST_THREAD_EVENTS 10000 |
| 192 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 193 | typedef struct test_thread_options { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 194 | gpr_event on_started; |
| 195 | gpr_event *phase1; |
| 196 | gpr_event on_phase1_done; |
| 197 | gpr_event *phase2; |
| 198 | gpr_event on_finished; |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 199 | size_t events_triggered; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 200 | int id; |
| 201 | grpc_completion_queue *cc; |
| 202 | } test_thread_options; |
| 203 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 204 | gpr_timespec ten_seconds_time(void) { |
| 205 | return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 208 | static void free_completion(grpc_exec_ctx *exec_ctx, void *arg, |
| 209 | grpc_cq_completion *completion) { |
| 210 | gpr_free(completion); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 213 | static void producer_thread(void *arg) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 214 | test_thread_options *opt = arg; |
| 215 | int i; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 216 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 217 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 218 | gpr_log(GPR_INFO, "producer %d started", opt->id); |
| 219 | gpr_event_set(&opt->on_started, (void *)(gpr_intptr)1); |
| 220 | GPR_ASSERT(gpr_event_wait(opt->phase1, ten_seconds_time())); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 221 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 222 | gpr_log(GPR_INFO, "producer %d phase 1", opt->id); |
| 223 | for (i = 0; i < TEST_THREAD_EVENTS; i++) { |
| 224 | grpc_cq_begin_op(opt->cc); |
| 225 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 226 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 227 | gpr_log(GPR_INFO, "producer %d phase 1 done", opt->id); |
| 228 | gpr_event_set(&opt->on_phase1_done, (void *)(gpr_intptr)1); |
| 229 | GPR_ASSERT(gpr_event_wait(opt->phase2, ten_seconds_time())); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 230 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 231 | gpr_log(GPR_INFO, "producer %d phase 2", opt->id); |
| 232 | for (i = 0; i < TEST_THREAD_EVENTS; i++) { |
Craig Tiller | b8b1a46 | 2015-09-22 12:50:03 -0700 | [diff] [blame] | 233 | grpc_cq_end_op(&exec_ctx, opt->cc, (void *)(gpr_intptr)1, 1, |
| 234 | free_completion, NULL, |
| 235 | gpr_malloc(sizeof(grpc_cq_completion))); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 236 | opt->events_triggered++; |
| 237 | grpc_exec_ctx_finish(&exec_ctx); |
| 238 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 239 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 240 | gpr_log(GPR_INFO, "producer %d phase 2 done", opt->id); |
| 241 | gpr_event_set(&opt->on_finished, (void *)(gpr_intptr)1); |
Craig Tiller | b8b1a46 | 2015-09-22 12:50:03 -0700 | [diff] [blame] | 242 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 245 | static void consumer_thread(void *arg) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 246 | test_thread_options *opt = arg; |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 247 | grpc_event ev; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 248 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 249 | gpr_log(GPR_INFO, "consumer %d started", opt->id); |
| 250 | gpr_event_set(&opt->on_started, (void *)(gpr_intptr)1); |
| 251 | GPR_ASSERT(gpr_event_wait(opt->phase1, ten_seconds_time())); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 252 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 253 | gpr_log(GPR_INFO, "consumer %d phase 1", opt->id); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 254 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 255 | gpr_log(GPR_INFO, "consumer %d phase 1 done", opt->id); |
| 256 | gpr_event_set(&opt->on_phase1_done, (void *)(gpr_intptr)1); |
| 257 | GPR_ASSERT(gpr_event_wait(opt->phase2, ten_seconds_time())); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 258 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 259 | gpr_log(GPR_INFO, "consumer %d phase 2", opt->id); |
| 260 | for (;;) { |
| 261 | ev = grpc_completion_queue_next(opt->cc, ten_seconds_time(), NULL); |
| 262 | switch (ev.type) { |
| 263 | case GRPC_OP_COMPLETE: |
| 264 | GPR_ASSERT(ev.success); |
| 265 | opt->events_triggered++; |
| 266 | break; |
| 267 | case GRPC_QUEUE_SHUTDOWN: |
| 268 | gpr_log(GPR_INFO, "consumer %d phase 2 done", opt->id); |
| 269 | gpr_event_set(&opt->on_finished, (void *)(gpr_intptr)1); |
| 270 | return; |
| 271 | case GRPC_QUEUE_TIMEOUT: |
| 272 | gpr_log(GPR_ERROR, "Invalid timeout received"); |
| 273 | abort(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 274 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 275 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 278 | static void test_threading(size_t producers, size_t consumers) { |
| 279 | test_thread_options *options = |
| 280 | gpr_malloc((producers + consumers) * sizeof(test_thread_options)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 281 | gpr_event phase1 = GPR_EVENT_INIT; |
| 282 | gpr_event phase2 = GPR_EVENT_INIT; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 283 | grpc_completion_queue *cc = grpc_completion_queue_create(NULL); |
Craig Tiller | 32ca48c | 2015-09-10 11:47:15 -0700 | [diff] [blame] | 284 | size_t i; |
| 285 | size_t total_consumed = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 286 | static int optid = 101; |
| 287 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 288 | gpr_log(GPR_INFO, "%s: %d producers, %d consumers", "test_threading", |
| 289 | producers, consumers); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 290 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 291 | /* start all threads: they will wait for phase1 */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 292 | for (i = 0; i < producers + consumers; i++) { |
| 293 | gpr_thd_id id; |
| 294 | gpr_event_init(&options[i].on_started); |
| 295 | gpr_event_init(&options[i].on_phase1_done); |
| 296 | gpr_event_init(&options[i].on_finished); |
| 297 | options[i].phase1 = &phase1; |
| 298 | options[i].phase2 = &phase2; |
| 299 | options[i].events_triggered = 0; |
| 300 | options[i].cc = cc; |
| 301 | options[i].id = optid++; |
| 302 | GPR_ASSERT(gpr_thd_new(&id, |
| 303 | i < producers ? producer_thread : consumer_thread, |
| 304 | options + i, NULL)); |
| 305 | gpr_event_wait(&options[i].on_started, ten_seconds_time()); |
| 306 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 307 | |
| 308 | /* start phase1: producers will pre-declare all operations they will |
| 309 | complete */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 310 | gpr_log(GPR_INFO, "start phase 1"); |
| 311 | gpr_event_set(&phase1, (void *)(gpr_intptr)1); |
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, "wait phase 1"); |
| 314 | for (i = 0; i < producers + consumers; i++) { |
| 315 | GPR_ASSERT(gpr_event_wait(&options[i].on_phase1_done, ten_seconds_time())); |
| 316 | } |
| 317 | gpr_log(GPR_INFO, "done phase 1"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 318 | |
| 319 | /* start phase2: operations will complete, and consumers will consume them */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 320 | gpr_log(GPR_INFO, "start phase 2"); |
| 321 | gpr_event_set(&phase2, (void *)(gpr_intptr)1); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 322 | |
| 323 | /* in parallel, we shutdown the completion channel - all events should still |
| 324 | be consumed */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 325 | grpc_completion_queue_shutdown(cc); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 326 | |
| 327 | /* join all threads */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 328 | gpr_log(GPR_INFO, "wait phase 2"); |
| 329 | for (i = 0; i < producers + consumers; i++) { |
| 330 | GPR_ASSERT(gpr_event_wait(&options[i].on_finished, ten_seconds_time())); |
| 331 | } |
| 332 | gpr_log(GPR_INFO, "done phase 2"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 333 | |
| 334 | /* destroy the completion channel */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 335 | grpc_completion_queue_destroy(cc); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 336 | |
| 337 | /* verify that everything was produced and consumed */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 338 | for (i = 0; i < producers + consumers; i++) { |
| 339 | if (i < producers) { |
| 340 | GPR_ASSERT(options[i].events_triggered == TEST_THREAD_EVENTS); |
| 341 | } else { |
| 342 | total_consumed += options[i].events_triggered; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 343 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 344 | } |
| 345 | GPR_ASSERT(total_consumed == producers * TEST_THREAD_EVENTS); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 346 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 347 | gpr_free(options); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 350 | int main(int argc, char **argv) { |
| 351 | grpc_test_init(argc, argv); |
| 352 | grpc_init(); |
| 353 | test_no_op(); |
| 354 | test_wait_empty(); |
| 355 | test_shutdown_then_next_polling(); |
| 356 | test_shutdown_then_next_with_timeout(); |
| 357 | test_cq_end_op(); |
| 358 | test_pluck(); |
yang-g | 1ed62e4 | 2015-12-08 08:00:24 -0800 | [diff] [blame] | 359 | test_pluck_after_shutdown(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 360 | test_threading(1, 1); |
| 361 | test_threading(1, 10); |
| 362 | test_threading(10, 1); |
| 363 | test_threading(10, 10); |
| 364 | grpc_shutdown(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 365 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 366 | } |