Craig Tiller | 9fb89ea | 2016-03-11 20:12:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | a45782f | 2016-03-11 20:15:51 -0800 | [diff] [blame] | 3 | * Copyright 2016, Google Inc. |
Craig Tiller | 9fb89ea | 2016-03-11 20:12:45 -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 | |
Craig Tiller | 57e2743 | 2016-03-11 16:53:58 -0800 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | |
| 36 | #include <grpc/grpc.h> |
| 37 | #include <grpc/support/alloc.h> |
Craig Tiller | 0ce7bec | 2016-03-11 17:02:07 -0800 | [diff] [blame] | 38 | #include <grpc/support/log.h> |
Craig Tiller | 57e2743 | 2016-03-11 16:53:58 -0800 | [diff] [blame] | 39 | #include <grpc/support/thd.h> |
Craig Tiller | 0ce7bec | 2016-03-11 17:02:07 -0800 | [diff] [blame] | 40 | #include "test/core/util/test_config.h" |
Craig Tiller | 57e2743 | 2016-03-11 16:53:58 -0800 | [diff] [blame] | 41 | |
| 42 | #define NUM_THREADS 100 |
Masood Malekghassemi | 6643ce7 | 2016-03-14 14:21:08 -0700 | [diff] [blame] | 43 | #define NUM_OUTER_LOOPS 10 |
| 44 | #define NUM_INNER_LOOPS 10 |
| 45 | #define DELAY_MILLIS 10 |
| 46 | #define POLL_MILLIS 3000 |
Craig Tiller | 57e2743 | 2016-03-11 16:53:58 -0800 | [diff] [blame] | 47 | |
Masood Malekghassemi | 6643ce7 | 2016-03-14 14:21:08 -0700 | [diff] [blame] | 48 | void create_loop_destroy(void* unused) { |
| 49 | for (int i = 0; i < NUM_OUTER_LOOPS; ++i) { |
Craig Tiller | 57e2743 | 2016-03-11 16:53:58 -0800 | [diff] [blame] | 50 | grpc_completion_queue* cq = grpc_completion_queue_create(NULL); |
| 51 | grpc_channel* chan = grpc_insecure_channel_create("localhost", NULL, NULL); |
| 52 | |
Masood Malekghassemi | 6643ce7 | 2016-03-14 14:21:08 -0700 | [diff] [blame] | 53 | for (int j = 0; j < NUM_INNER_LOOPS; ++j) { |
| 54 | gpr_timespec later_time = GRPC_TIMEOUT_MILLIS_TO_DEADLINE(DELAY_MILLIS); |
Craig Tiller | 57e2743 | 2016-03-11 16:53:58 -0800 | [diff] [blame] | 55 | 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 Malekghassemi | 6643ce7 | 2016-03-14 14:21:08 -0700 | [diff] [blame] | 58 | 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 Tiller | 57e2743 | 2016-03-11 16:53:58 -0800 | [diff] [blame] | 61 | } |
Masood Malekghassemi | 6643ce7 | 2016-03-14 14:21:08 -0700 | [diff] [blame] | 62 | grpc_channel_destroy(chan); |
| 63 | grpc_completion_queue_destroy(cq); |
Craig Tiller | 57e2743 | 2016-03-11 16:53:58 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Craig Tiller | 0ce7bec | 2016-03-11 17:02:07 -0800 | [diff] [blame] | 67 | int main(int argc, char** argv) { |
| 68 | grpc_test_init(argc, argv); |
Craig Tiller | 57e2743 | 2016-03-11 16:53:58 -0800 | [diff] [blame] | 69 | grpc_init(); |
| 70 | gpr_thd_id threads[NUM_THREADS]; |
Masood Malekghassemi | 6643ce7 | 2016-03-14 14:21:08 -0700 | [diff] [blame] | 71 | for (size_t i = 0; i < NUM_THREADS; ++i) { |
Craig Tiller | 57e2743 | 2016-03-11 16:53:58 -0800 | [diff] [blame] | 72 | gpr_thd_options options = gpr_thd_options_default(); |
| 73 | gpr_thd_options_set_joinable(&options); |
Masood Malekghassemi | 6643ce7 | 2016-03-14 14:21:08 -0700 | [diff] [blame] | 74 | gpr_thd_new(&threads[i], create_loop_destroy, NULL, &options); |
Craig Tiller | 57e2743 | 2016-03-11 16:53:58 -0800 | [diff] [blame] | 75 | } |
Masood Malekghassemi | 6643ce7 | 2016-03-14 14:21:08 -0700 | [diff] [blame] | 76 | for (size_t i = 0; i < NUM_THREADS; ++i) { |
Craig Tiller | 57e2743 | 2016-03-11 16:53:58 -0800 | [diff] [blame] | 77 | gpr_thd_join(threads[i]); |
| 78 | } |
| 79 | grpc_shutdown(); |
| 80 | return 0; |
| 81 | } |