blob: 7a5cf30506f71dd0f336ead1274ec4894f08e228 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
ctiller18b49ab2014-12-09 14:39:16 -080036#include "src/core/iomgr/iomgr.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037#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 Nobleb7ebd3b2014-11-26 16:33:03 -080042#include "test/core/util/test_config.h"
43
Craig Tiller35696192015-05-24 15:00:37 -070044#define LOG_TEST(x) gpr_log(GPR_INFO, "%s", x)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080045
Craig Tillera82950e2015-09-22 12:33:20 -070046static void *create_test_tag(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047 static gpr_intptr i = 0;
Craig Tillera82950e2015-09-22 12:33:20 -070048 return (void *)(++i);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049}
50
51/* helper for tests to shutdown correctly and tersely */
Craig Tillera82950e2015-09-22 12:33:20 -070052static void shutdown_and_destroy(grpc_completion_queue *cc) {
Craig Tiller64be9f72015-05-04 14:53:51 -070053 grpc_event ev;
Craig Tillera82950e2015-09-22 12:33:20 -070054 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 Nobleb7ebd3b2014-11-26 16:33:03 -080058}
59
60/* ensure we can create and destroy a completion channel */
Craig Tillera82950e2015-09-22 12:33:20 -070061static void test_no_op(void) {
62 LOG_TEST("test_no_op");
63 shutdown_and_destroy(grpc_completion_queue_create(NULL));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064}
65
Craig Tillera82950e2015-09-22 12:33:20 -070066static void test_wait_empty(void) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067 grpc_completion_queue *cc;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +020068 grpc_event event;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069
Craig Tillera82950e2015-09-22 12:33:20 -070070 LOG_TEST("test_wait_empty");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080071
Craig Tillera82950e2015-09-22 12:33:20 -070072 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 Nobleb7ebd3b2014-11-26 16:33:03 -080076}
77
Craig Tillera82950e2015-09-22 12:33:20 -070078static void do_nothing_end_completion(grpc_exec_ctx *exec_ctx, void *arg,
79 grpc_cq_completion *c) {}
Craig Tiller97fc6a32015-07-08 15:31:35 -070080
Craig Tillera82950e2015-09-22 12:33:20 -070081static void test_cq_end_op(void) {
Craig Tiller64be9f72015-05-04 14:53:51 -070082 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083 grpc_completion_queue *cc;
Craig Tiller97fc6a32015-07-08 15:31:35 -070084 grpc_cq_completion completion;
Craig Tillerf5768a62015-09-22 10:54:34 -070085 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -070086 void *tag = create_test_tag();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080087
Craig Tillera82950e2015-09-22 12:33:20 -070088 LOG_TEST("test_cq_end_op");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080089
Craig Tillera82950e2015-09-22 12:33:20 -070090 cc = grpc_completion_queue_create(NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080091
Craig Tillera82950e2015-09-22 12:33:20 -070092 grpc_cq_begin_op(cc);
93 grpc_cq_end_op(&exec_ctx, cc, tag, 1, do_nothing_end_completion, NULL,
94 &completion);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080095
Craig Tillera82950e2015-09-22 12:33:20 -070096 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800100
Craig Tillera82950e2015-09-22 12:33:20 -0700101 shutdown_and_destroy(cc);
Craig Tillerb8b1a462015-09-22 12:50:03 -0700102 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800103}
104
Craig Tillera82950e2015-09-22 12:33:20 -0700105static void test_shutdown_then_next_polling(void) {
Craig Tiller59657db2015-06-01 20:01:15 -0700106 grpc_completion_queue *cc;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200107 grpc_event event;
Craig Tillera82950e2015-09-22 12:33:20 -0700108 LOG_TEST("test_shutdown_then_next_polling");
Craig Tiller59657db2015-06-01 20:01:15 -0700109
Craig Tillera82950e2015-09-22 12:33:20 -0700110 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 Tiller59657db2015-06-01 20:01:15 -0700116}
117
Craig Tillera82950e2015-09-22 12:33:20 -0700118static void test_shutdown_then_next_with_timeout(void) {
Craig Tillerc0c48442015-06-01 20:04:36 -0700119 grpc_completion_queue *cc;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +0200120 grpc_event event;
Craig Tillera82950e2015-09-22 12:33:20 -0700121 LOG_TEST("test_shutdown_then_next_with_timeout");
Craig Tillerc0c48442015-06-01 20:04:36 -0700122
Craig Tillera82950e2015-09-22 12:33:20 -0700123 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 Tillerc0c48442015-06-01 20:04:36 -0700129}
130
Craig Tillera82950e2015-09-22 12:33:20 -0700131static void test_pluck(void) {
Craig Tiller64be9f72015-05-04 14:53:51 -0700132 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800133 grpc_completion_queue *cc;
134 void *tags[128];
Craig Tillera82950e2015-09-22 12:33:20 -0700135 grpc_cq_completion completions[GPR_ARRAY_SIZE(tags)];
Craig Tillerf5768a62015-09-22 10:54:34 -0700136 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100137 unsigned i, j;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800138
Craig Tillera82950e2015-09-22 12:33:20 -0700139 LOG_TEST("test_pluck");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800140
Craig Tillera82950e2015-09-22 12:33:20 -0700141 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800145 }
Craig Tillera82950e2015-09-22 12:33:20 -0700146 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800147
Craig Tillera82950e2015-09-22 12:33:20 -0700148 cc = grpc_completion_queue_create(NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800149
Craig Tillera82950e2015-09-22 12:33:20 -0700150 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800155
Craig Tillera82950e2015-09-22 12:33:20 -0700156 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800161
Craig Tillera82950e2015-09-22 12:33:20 -0700162 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800167
Craig Tillera82950e2015-09-22 12:33:20 -0700168 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800173
Craig Tillera82950e2015-09-22 12:33:20 -0700174 shutdown_and_destroy(cc);
Craig Tillerb8b1a462015-09-22 12:50:03 -0700175 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800176}
177
yang-g1ed62e42015-12-08 08:00:24 -0800178static 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800191#define TEST_THREAD_EVENTS 10000
192
Craig Tillera82950e2015-09-22 12:33:20 -0700193typedef struct test_thread_options {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800194 gpr_event on_started;
195 gpr_event *phase1;
196 gpr_event on_phase1_done;
197 gpr_event *phase2;
198 gpr_event on_finished;
Craig Tiller32ca48c2015-09-10 11:47:15 -0700199 size_t events_triggered;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800200 int id;
201 grpc_completion_queue *cc;
202} test_thread_options;
203
Craig Tillera82950e2015-09-22 12:33:20 -0700204gpr_timespec ten_seconds_time(void) {
205 return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206}
207
Craig Tillera82950e2015-09-22 12:33:20 -0700208static void free_completion(grpc_exec_ctx *exec_ctx, void *arg,
209 grpc_cq_completion *completion) {
210 gpr_free(completion);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800211}
212
Craig Tillera82950e2015-09-22 12:33:20 -0700213static void producer_thread(void *arg) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800214 test_thread_options *opt = arg;
215 int i;
Craig Tillerf5768a62015-09-22 10:54:34 -0700216 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800217
Craig Tillera82950e2015-09-22 12:33:20 -0700218 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800221
Craig Tillera82950e2015-09-22 12:33:20 -0700222 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800226
Craig Tillera82950e2015-09-22 12:33:20 -0700227 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800230
Craig Tillera82950e2015-09-22 12:33:20 -0700231 gpr_log(GPR_INFO, "producer %d phase 2", opt->id);
232 for (i = 0; i < TEST_THREAD_EVENTS; i++) {
Craig Tillerb8b1a462015-09-22 12:50:03 -0700233 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 Tillera82950e2015-09-22 12:33:20 -0700236 opt->events_triggered++;
237 grpc_exec_ctx_finish(&exec_ctx);
238 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800239
Craig Tillera82950e2015-09-22 12:33:20 -0700240 gpr_log(GPR_INFO, "producer %d phase 2 done", opt->id);
241 gpr_event_set(&opt->on_finished, (void *)(gpr_intptr)1);
Craig Tillerb8b1a462015-09-22 12:50:03 -0700242 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800243}
244
Craig Tillera82950e2015-09-22 12:33:20 -0700245static void consumer_thread(void *arg) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800246 test_thread_options *opt = arg;
Craig Tiller64be9f72015-05-04 14:53:51 -0700247 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800248
Craig Tillera82950e2015-09-22 12:33:20 -0700249 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800252
Craig Tillera82950e2015-09-22 12:33:20 -0700253 gpr_log(GPR_INFO, "consumer %d phase 1", opt->id);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800254
Craig Tillera82950e2015-09-22 12:33:20 -0700255 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800258
Craig Tillera82950e2015-09-22 12:33:20 -0700259 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800274 }
Craig Tillera82950e2015-09-22 12:33:20 -0700275 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800276}
277
Craig Tillera82950e2015-09-22 12:33:20 -0700278static void test_threading(size_t producers, size_t consumers) {
279 test_thread_options *options =
280 gpr_malloc((producers + consumers) * sizeof(test_thread_options));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800281 gpr_event phase1 = GPR_EVENT_INIT;
282 gpr_event phase2 = GPR_EVENT_INIT;
Craig Tillera82950e2015-09-22 12:33:20 -0700283 grpc_completion_queue *cc = grpc_completion_queue_create(NULL);
Craig Tiller32ca48c2015-09-10 11:47:15 -0700284 size_t i;
285 size_t total_consumed = 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800286 static int optid = 101;
287
Craig Tillera82950e2015-09-22 12:33:20 -0700288 gpr_log(GPR_INFO, "%s: %d producers, %d consumers", "test_threading",
289 producers, consumers);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800290
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800291 /* start all threads: they will wait for phase1 */
Craig Tillera82950e2015-09-22 12:33:20 -0700292 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800307
308 /* start phase1: producers will pre-declare all operations they will
309 complete */
Craig Tillera82950e2015-09-22 12:33:20 -0700310 gpr_log(GPR_INFO, "start phase 1");
311 gpr_event_set(&phase1, (void *)(gpr_intptr)1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800312
Craig Tillera82950e2015-09-22 12:33:20 -0700313 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800318
319 /* start phase2: operations will complete, and consumers will consume them */
Craig Tillera82950e2015-09-22 12:33:20 -0700320 gpr_log(GPR_INFO, "start phase 2");
321 gpr_event_set(&phase2, (void *)(gpr_intptr)1);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800322
323 /* in parallel, we shutdown the completion channel - all events should still
324 be consumed */
Craig Tillera82950e2015-09-22 12:33:20 -0700325 grpc_completion_queue_shutdown(cc);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800326
327 /* join all threads */
Craig Tillera82950e2015-09-22 12:33:20 -0700328 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800333
334 /* destroy the completion channel */
Craig Tillera82950e2015-09-22 12:33:20 -0700335 grpc_completion_queue_destroy(cc);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800336
337 /* verify that everything was produced and consumed */
Craig Tillera82950e2015-09-22 12:33:20 -0700338 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800343 }
Craig Tillera82950e2015-09-22 12:33:20 -0700344 }
345 GPR_ASSERT(total_consumed == producers * TEST_THREAD_EVENTS);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800346
Craig Tillera82950e2015-09-22 12:33:20 -0700347 gpr_free(options);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800348}
349
Craig Tillera82950e2015-09-22 12:33:20 -0700350int 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-g1ed62e42015-12-08 08:00:24 -0800359 test_pluck_after_shutdown();
Craig Tillera82950e2015-09-22 12:33:20 -0700360 test_threading(1, 1);
361 test_threading(1, 10);
362 test_threading(10, 1);
363 test_threading(10, 10);
364 grpc_shutdown();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800365 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800366}