blob: 96761b05023ed5b7fdb14f1e474eca738a58f4b4 [file] [log] [blame]
Craig Tiller9fb89ea2016-03-11 20:12:45 -08001/*
2*
Craig Tillera45782f2016-03-11 20:15:51 -08003* Copyright 2016, Google Inc.
Craig Tiller9fb89ea2016-03-11 20:12:45 -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
Craig Tiller57e27432016-03-11 16:53:58 -080034#include <stdio.h>
35
36#include <grpc/grpc.h>
37#include <grpc/support/alloc.h>
Craig Tiller0ce7bec2016-03-11 17:02:07 -080038#include <grpc/support/log.h>
Craig Tiller57e27432016-03-11 16:53:58 -080039#include <grpc/support/thd.h>
Craig Tiller0ce7bec2016-03-11 17:02:07 -080040#include "test/core/util/test_config.h"
Craig Tiller57e27432016-03-11 16:53:58 -080041
42#define NUM_THREADS 100
Masood Malekghassemi6643ce72016-03-14 14:21:08 -070043#define NUM_OUTER_LOOPS 10
44#define NUM_INNER_LOOPS 10
45#define DELAY_MILLIS 10
46#define POLL_MILLIS 3000
Craig Tiller57e27432016-03-11 16:53:58 -080047
Masood Malekghassemi6643ce72016-03-14 14:21:08 -070048void create_loop_destroy(void* unused) {
49 for (int i = 0; i < NUM_OUTER_LOOPS; ++i) {
Craig Tiller57e27432016-03-11 16:53:58 -080050 grpc_completion_queue* cq = grpc_completion_queue_create(NULL);
51 grpc_channel* chan = grpc_insecure_channel_create("localhost", NULL, NULL);
52
Masood Malekghassemi6643ce72016-03-14 14:21:08 -070053 for (int j = 0; j < NUM_INNER_LOOPS; ++j) {
54 gpr_timespec later_time = GRPC_TIMEOUT_MILLIS_TO_DEADLINE(DELAY_MILLIS);
Craig Tiller57e27432016-03-11 16:53:58 -080055 grpc_connectivity_state state =
56 grpc_channel_check_connectivity_state(chan, 1);
57 grpc_channel_watch_connectivity_state(chan, state, later_time, cq, NULL);
Masood Malekghassemi6643ce72016-03-14 14:21:08 -070058 gpr_timespec poll_time = GRPC_TIMEOUT_MILLIS_TO_DEADLINE(POLL_MILLIS);
59 GPR_ASSERT(grpc_completion_queue_next(cq, poll_time, NULL).type ==
60 GRPC_OP_COMPLETE);
Craig Tiller57e27432016-03-11 16:53:58 -080061 }
Masood Malekghassemi6643ce72016-03-14 14:21:08 -070062 grpc_channel_destroy(chan);
63 grpc_completion_queue_destroy(cq);
Craig Tiller57e27432016-03-11 16:53:58 -080064 }
65}
66
Craig Tiller0ce7bec2016-03-11 17:02:07 -080067int main(int argc, char** argv) {
68 grpc_test_init(argc, argv);
Craig Tiller57e27432016-03-11 16:53:58 -080069 grpc_init();
70 gpr_thd_id threads[NUM_THREADS];
Masood Malekghassemi6643ce72016-03-14 14:21:08 -070071 for (size_t i = 0; i < NUM_THREADS; ++i) {
Craig Tiller57e27432016-03-11 16:53:58 -080072 gpr_thd_options options = gpr_thd_options_default();
73 gpr_thd_options_set_joinable(&options);
Masood Malekghassemi6643ce72016-03-14 14:21:08 -070074 gpr_thd_new(&threads[i], create_loop_destroy, NULL, &options);
Craig Tiller57e27432016-03-11 16:53:58 -080075 }
Masood Malekghassemi6643ce72016-03-14 14:21:08 -070076 for (size_t i = 0; i < NUM_THREADS; ++i) {
Craig Tiller57e27432016-03-11 16:53:58 -080077 gpr_thd_join(threads[i]);
78 }
79 grpc_shutdown();
80 return 0;
81}