Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2016, Google Inc. |
| 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 | #include "src/core/lib/iomgr/port.h" |
| 34 | |
Sree Kuchibhotla | 113267b | 2017-01-25 14:37:19 -0800 | [diff] [blame] | 35 | /* This test only relevant on linux systems where epoll is available */ |
| 36 | #ifdef GRPC_LINUX_EPOLL |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 37 | |
| 38 | #include <errno.h> |
| 39 | #include <string.h> |
| 40 | #include <unistd.h> |
| 41 | |
| 42 | #include <grpc/support/alloc.h> |
| 43 | #include <grpc/support/log.h> |
Sree Kuchibhotla | 9930e4b | 2017-01-25 14:48:20 -0800 | [diff] [blame^] | 44 | #include <grpc/support/useful.h> |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 45 | |
Sree Kuchibhotla | 113267b | 2017-01-25 14:37:19 -0800 | [diff] [blame] | 46 | #include "src/core/lib/iomgr/ev_posix.h" |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 47 | #include "src/core/lib/iomgr/iomgr.h" |
| 48 | #include "test/core/util/test_config.h" |
| 49 | |
| 50 | /******************************************************************************* |
| 51 | * test_pollset_set |
| 52 | */ |
| 53 | |
| 54 | typedef struct test_pollset_set { grpc_pollset_set *pss; } test_pollset_set; |
| 55 | |
Sree Kuchibhotla | 9930e4b | 2017-01-25 14:48:20 -0800 | [diff] [blame^] | 56 | void init_test_pollset_sets(test_pollset_set *pollset_sets, const int num_pss) { |
Sree Kuchibhotla | 113267b | 2017-01-25 14:37:19 -0800 | [diff] [blame] | 57 | for (int i = 0; i < num_pss; i++) { |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 58 | pollset_sets[i].pss = grpc_pollset_set_create(); |
| 59 | } |
| 60 | } |
| 61 | |
Sree Kuchibhotla | 9930e4b | 2017-01-25 14:48:20 -0800 | [diff] [blame^] | 62 | void cleanup_test_pollset_sets(test_pollset_set *pollset_sets, |
| 63 | const int num_pss) { |
Sree Kuchibhotla | 113267b | 2017-01-25 14:37:19 -0800 | [diff] [blame] | 64 | for (int i = 0; i < num_pss; i++) { |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 65 | grpc_pollset_set_destroy(pollset_sets[i].pss); |
| 66 | pollset_sets[i].pss = NULL; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /******************************************************************************* |
| 71 | * test_pollset |
| 72 | */ |
| 73 | |
| 74 | typedef struct test_pollset { |
| 75 | grpc_pollset *ps; |
| 76 | gpr_mu *mu; |
| 77 | } test_pollset; |
| 78 | |
Sree Kuchibhotla | 9930e4b | 2017-01-25 14:48:20 -0800 | [diff] [blame^] | 79 | static void init_test_pollsets(test_pollset *pollsets, const int num_pollsets) { |
Sree Kuchibhotla | 113267b | 2017-01-25 14:37:19 -0800 | [diff] [blame] | 80 | for (int i = 0; i < num_pollsets; i++) { |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 81 | pollsets[i].ps = gpr_malloc(grpc_pollset_size()); |
| 82 | grpc_pollset_init(pollsets[i].ps, &pollsets[i].mu); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static void destroy_pollset(grpc_exec_ctx *exec_ctx, void *p, |
| 87 | grpc_error *error) { |
| 88 | grpc_pollset_destroy(p); |
| 89 | } |
| 90 | |
| 91 | static void cleanup_test_pollsets(grpc_exec_ctx *exec_ctx, |
Sree Kuchibhotla | 9930e4b | 2017-01-25 14:48:20 -0800 | [diff] [blame^] | 92 | test_pollset *pollsets, |
| 93 | const int num_pollsets) { |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 94 | grpc_closure destroyed; |
Sree Kuchibhotla | 113267b | 2017-01-25 14:37:19 -0800 | [diff] [blame] | 95 | for (int i = 0; i < num_pollsets; i++) { |
Sree Kuchibhotla | 645e30a | 2017-01-20 10:59:15 -0800 | [diff] [blame] | 96 | grpc_closure_init(&destroyed, destroy_pollset, pollsets[i].ps, |
| 97 | grpc_schedule_on_exec_ctx); |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 98 | grpc_pollset_shutdown(exec_ctx, pollsets[i].ps, &destroyed); |
| 99 | |
| 100 | grpc_exec_ctx_flush(exec_ctx); |
| 101 | gpr_free(pollsets[i].ps); |
| 102 | pollsets[i].ps = NULL; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /******************************************************************************* |
| 107 | * test_fd |
| 108 | */ |
| 109 | |
| 110 | typedef struct test_fd { |
| 111 | grpc_fd *fd; |
| 112 | grpc_wakeup_fd wakeup_fd; |
| 113 | |
| 114 | bool is_on_readable_called; /* Is on_readable closure is called ? */ |
| 115 | grpc_closure on_readable; /* Closure to call when this fd is readable */ |
| 116 | } test_fd; |
| 117 | |
| 118 | void on_readable(grpc_exec_ctx *exec_ctx, void *tfd, grpc_error *error) { |
| 119 | ((test_fd *)tfd)->is_on_readable_called = true; |
| 120 | } |
| 121 | |
| 122 | static void reset_test_fd(grpc_exec_ctx *exec_ctx, test_fd *tfd) { |
| 123 | tfd->is_on_readable_called = false; |
| 124 | |
Sree Kuchibhotla | 645e30a | 2017-01-20 10:59:15 -0800 | [diff] [blame] | 125 | grpc_closure_init(&tfd->on_readable, on_readable, tfd, |
| 126 | grpc_schedule_on_exec_ctx); |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 127 | grpc_fd_notify_on_read(exec_ctx, tfd->fd, &tfd->on_readable); |
| 128 | } |
| 129 | |
Sree Kuchibhotla | 9930e4b | 2017-01-25 14:48:20 -0800 | [diff] [blame^] | 130 | static void init_test_fds(grpc_exec_ctx *exec_ctx, test_fd *tfds, |
| 131 | const int num_fds) { |
Sree Kuchibhotla | 113267b | 2017-01-25 14:37:19 -0800 | [diff] [blame] | 132 | for (int i = 0; i < num_fds; i++) { |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 133 | GPR_ASSERT(GRPC_ERROR_NONE == grpc_wakeup_fd_init(&tfds[i].wakeup_fd)); |
| 134 | tfds[i].fd = grpc_fd_create(GRPC_WAKEUP_FD_GET_READ_FD(&tfds[i].wakeup_fd), |
| 135 | "test_fd"); |
| 136 | reset_test_fd(exec_ctx, &tfds[i]); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static void cleanup_test_fds(grpc_exec_ctx *exec_ctx, test_fd *tfds, |
Sree Kuchibhotla | 9930e4b | 2017-01-25 14:48:20 -0800 | [diff] [blame^] | 141 | const int num_fds) { |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 142 | int release_fd; |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 143 | |
Sree Kuchibhotla | 113267b | 2017-01-25 14:37:19 -0800 | [diff] [blame] | 144 | for (int i = 0; i < num_fds; i++) { |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 145 | grpc_fd_shutdown(exec_ctx, tfds[i].fd); |
| 146 | grpc_exec_ctx_flush(exec_ctx); |
| 147 | |
| 148 | /* grpc_fd_orphan frees the memory allocated for grpc_fd. Normally it also |
| 149 | * calls close() on the underlying fd. In our case, we are using |
| 150 | * grpc_wakeup_fd and we would like to destroy it ourselves (by calling |
| 151 | * grpc_wakeup_fd_destroy). To prevent grpc_fd from calling close() on the |
| 152 | * underlying fd, call it with a non-NULL 'release_fd' parameter */ |
| 153 | grpc_fd_orphan(exec_ctx, tfds[i].fd, NULL, &release_fd, "test_fd_cleanup"); |
| 154 | grpc_exec_ctx_flush(exec_ctx); |
| 155 | |
| 156 | grpc_wakeup_fd_destroy(&tfds[i].wakeup_fd); |
| 157 | } |
| 158 | } |
| 159 | |
Sree Kuchibhotla | 9930e4b | 2017-01-25 14:48:20 -0800 | [diff] [blame^] | 160 | static void make_test_fds_readable(test_fd *tfds, const int num_fds) { |
Sree Kuchibhotla | 113267b | 2017-01-25 14:37:19 -0800 | [diff] [blame] | 161 | for (int i = 0; i < num_fds; i++) { |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 162 | GPR_ASSERT(GRPC_ERROR_NONE == grpc_wakeup_fd_wakeup(&tfds[i].wakeup_fd)); |
| 163 | } |
| 164 | } |
| 165 | |
Sree Kuchibhotla | 113267b | 2017-01-25 14:37:19 -0800 | [diff] [blame] | 166 | static void verify_readable_and_reset(grpc_exec_ctx *exec_ctx, test_fd *tfds, |
Sree Kuchibhotla | 9930e4b | 2017-01-25 14:48:20 -0800 | [diff] [blame^] | 167 | const int num_fds) { |
Sree Kuchibhotla | 113267b | 2017-01-25 14:37:19 -0800 | [diff] [blame] | 168 | for (int i = 0; i < num_fds; i++) { |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 169 | /* Verify that the on_readable callback was called */ |
| 170 | GPR_ASSERT(tfds[i].is_on_readable_called); |
| 171 | |
| 172 | /* Reset the tfd[i] structure */ |
| 173 | GPR_ASSERT(GRPC_ERROR_NONE == |
| 174 | grpc_wakeup_fd_consume_wakeup(&tfds[i].wakeup_fd)); |
| 175 | reset_test_fd(exec_ctx, &tfds[i]); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /******************************************************************************* |
| 180 | * Main tests |
| 181 | */ |
| 182 | |
| 183 | /* Test some typical scenarios in pollset_set */ |
| 184 | static void pollset_set_test_basic() { |
| 185 | /* We construct the following structure for this test: |
| 186 | * |
| 187 | * +---> FD0 (Added before PSS1, PS1 and PS2 are added to PSS0) |
| 188 | * | |
| 189 | * +---> FD5 (Added after PSS1, PS1 and PS2 are added to PSS0) |
| 190 | * | |
| 191 | * | |
| 192 | * | +---> FD1 (Added before PSS1 is added to PSS0) |
| 193 | * | | |
| 194 | * | +---> FD6 (Added after PSS1 is added to PSS0) |
| 195 | * | | |
| 196 | * +---> PSS1--+ +--> FD2 (Added before PS0 is added to PSS1) |
| 197 | * | | | |
| 198 | * | +---> PS0---+ |
| 199 | * | | |
| 200 | * PSS0---+ +--> FD7 (Added after PS0 is added to PSS1) |
| 201 | * | |
| 202 | * | |
| 203 | * | +---> FD3 (Added before PS1 is added to PSS0) |
| 204 | * | | |
| 205 | * +---> PS1---+ |
| 206 | * | | |
| 207 | * | +---> FD8 (Added after PS1 added to PSS0) |
| 208 | * | |
| 209 | * | |
| 210 | * | +---> FD4 (Added before PS2 is added to PSS0) |
| 211 | * | | |
| 212 | * +---> PS2---+ |
| 213 | * | |
| 214 | * +---> FD9 (Added after PS2 is added to PSS0) |
| 215 | */ |
| 216 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 217 | grpc_pollset_worker *worker; |
| 218 | gpr_timespec deadline; |
| 219 | |
| 220 | test_fd tfds[10]; |
| 221 | test_pollset pollsets[3]; |
| 222 | test_pollset_set pollset_sets[2]; |
Sree Kuchibhotla | 9930e4b | 2017-01-25 14:48:20 -0800 | [diff] [blame^] | 223 | const int num_fds = GPR_ARRAY_SIZE(tfds); |
| 224 | const int num_ps = GPR_ARRAY_SIZE(pollsets); |
| 225 | const int num_pss = GPR_ARRAY_SIZE(pollset_sets); |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 226 | |
| 227 | init_test_fds(&exec_ctx, tfds, num_fds); |
| 228 | init_test_pollsets(pollsets, num_ps); |
| 229 | init_test_pollset_sets(pollset_sets, num_pss); |
| 230 | |
| 231 | /* Construct the pollset_set/pollset/fd tree (see diagram above) */ |
| 232 | |
| 233 | grpc_pollset_set_add_fd(&exec_ctx, pollset_sets[0].pss, tfds[0].fd); |
| 234 | grpc_pollset_set_add_fd(&exec_ctx, pollset_sets[1].pss, tfds[1].fd); |
| 235 | |
| 236 | grpc_pollset_add_fd(&exec_ctx, pollsets[0].ps, tfds[2].fd); |
| 237 | grpc_pollset_add_fd(&exec_ctx, pollsets[1].ps, tfds[3].fd); |
| 238 | grpc_pollset_add_fd(&exec_ctx, pollsets[2].ps, tfds[4].fd); |
| 239 | |
| 240 | grpc_pollset_set_add_pollset_set(&exec_ctx, pollset_sets[0].pss, |
| 241 | pollset_sets[1].pss); |
| 242 | |
| 243 | grpc_pollset_set_add_pollset(&exec_ctx, pollset_sets[1].pss, pollsets[0].ps); |
| 244 | grpc_pollset_set_add_pollset(&exec_ctx, pollset_sets[0].pss, pollsets[1].ps); |
| 245 | grpc_pollset_set_add_pollset(&exec_ctx, pollset_sets[0].pss, pollsets[2].ps); |
| 246 | |
| 247 | grpc_pollset_set_add_fd(&exec_ctx, pollset_sets[0].pss, tfds[5].fd); |
| 248 | grpc_pollset_set_add_fd(&exec_ctx, pollset_sets[1].pss, tfds[6].fd); |
| 249 | |
| 250 | grpc_pollset_add_fd(&exec_ctx, pollsets[0].ps, tfds[7].fd); |
| 251 | grpc_pollset_add_fd(&exec_ctx, pollsets[1].ps, tfds[8].fd); |
| 252 | grpc_pollset_add_fd(&exec_ctx, pollsets[2].ps, tfds[9].fd); |
| 253 | |
| 254 | grpc_exec_ctx_flush(&exec_ctx); |
| 255 | |
| 256 | /* Test that if any FD in the above structure is readable, it is observable by |
| 257 | * doing grpc_pollset_work on any pollset |
| 258 | * |
| 259 | * For every pollset, do the following: |
| 260 | * - (Ensure that all FDs are in reset state) |
| 261 | * - Make all FDs readable |
| 262 | * - Call grpc_pollset_work() on the pollset |
| 263 | * - Flush the exec_ctx |
| 264 | * - Verify that on_readable call back was called for all FDs (and |
| 265 | * reset the FDs) |
| 266 | * */ |
Sree Kuchibhotla | 113267b | 2017-01-25 14:37:19 -0800 | [diff] [blame] | 267 | for (int i = 0; i < num_ps; i++) { |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 268 | make_test_fds_readable(tfds, num_fds); |
| 269 | |
| 270 | gpr_mu_lock(pollsets[i].mu); |
| 271 | deadline = GRPC_TIMEOUT_MILLIS_TO_DEADLINE(2); |
| 272 | GPR_ASSERT(GRPC_ERROR_NONE == |
| 273 | grpc_pollset_work(&exec_ctx, pollsets[i].ps, &worker, |
| 274 | gpr_now(GPR_CLOCK_MONOTONIC), deadline)); |
| 275 | gpr_mu_unlock(pollsets[i].mu); |
| 276 | |
| 277 | grpc_exec_ctx_flush(&exec_ctx); |
| 278 | |
| 279 | verify_readable_and_reset(&exec_ctx, tfds, num_fds); |
| 280 | grpc_exec_ctx_flush(&exec_ctx); |
| 281 | } |
| 282 | |
| 283 | /* Test tear down */ |
| 284 | grpc_pollset_set_del_fd(&exec_ctx, pollset_sets[0].pss, tfds[0].fd); |
| 285 | grpc_pollset_set_del_fd(&exec_ctx, pollset_sets[0].pss, tfds[5].fd); |
| 286 | grpc_pollset_set_del_fd(&exec_ctx, pollset_sets[1].pss, tfds[1].fd); |
| 287 | grpc_pollset_set_del_fd(&exec_ctx, pollset_sets[1].pss, tfds[6].fd); |
| 288 | grpc_exec_ctx_flush(&exec_ctx); |
| 289 | |
| 290 | grpc_pollset_set_del_pollset(&exec_ctx, pollset_sets[1].pss, pollsets[0].ps); |
| 291 | grpc_pollset_set_del_pollset(&exec_ctx, pollset_sets[0].pss, pollsets[1].ps); |
| 292 | grpc_pollset_set_del_pollset(&exec_ctx, pollset_sets[0].pss, pollsets[2].ps); |
| 293 | |
| 294 | grpc_pollset_set_del_pollset_set(&exec_ctx, pollset_sets[0].pss, |
| 295 | pollset_sets[1].pss); |
| 296 | grpc_exec_ctx_flush(&exec_ctx); |
| 297 | |
| 298 | cleanup_test_fds(&exec_ctx, tfds, num_fds); |
| 299 | cleanup_test_pollsets(&exec_ctx, pollsets, num_ps); |
| 300 | cleanup_test_pollset_sets(pollset_sets, num_pss); |
| 301 | grpc_exec_ctx_finish(&exec_ctx); |
| 302 | } |
| 303 | |
| 304 | /* Same FD added multiple times to the pollset_set tree */ |
| 305 | void pollset_set_test_dup_fds() { |
| 306 | /* We construct the following structure for this test: |
| 307 | * |
| 308 | * +---> FD0 |
| 309 | * | |
| 310 | * | |
| 311 | * PSS0---+ |
| 312 | * | +---> FD0 (also under PSS0) |
| 313 | * | | |
| 314 | * +---> PSS1--+ +--> FD1 (also under PSS1) |
| 315 | * | | |
| 316 | * +---> PS ---+ |
| 317 | * | | |
| 318 | * | +--> FD2 |
| 319 | * +---> FD1 |
| 320 | */ |
| 321 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 322 | grpc_pollset_worker *worker; |
| 323 | gpr_timespec deadline; |
| 324 | |
| 325 | test_fd tfds[3]; |
| 326 | test_pollset pollset; |
| 327 | test_pollset_set pollset_sets[2]; |
Sree Kuchibhotla | 9930e4b | 2017-01-25 14:48:20 -0800 | [diff] [blame^] | 328 | const int num_fds = GPR_ARRAY_SIZE(tfds); |
| 329 | const int num_ps = 1; |
| 330 | const int num_pss = GPR_ARRAY_SIZE(pollset_sets); |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 331 | |
| 332 | init_test_fds(&exec_ctx, tfds, num_fds); |
| 333 | init_test_pollsets(&pollset, num_ps); |
| 334 | init_test_pollset_sets(pollset_sets, num_pss); |
| 335 | |
| 336 | /* Construct the structure */ |
| 337 | grpc_pollset_set_add_fd(&exec_ctx, pollset_sets[0].pss, tfds[0].fd); |
| 338 | grpc_pollset_set_add_fd(&exec_ctx, pollset_sets[1].pss, tfds[0].fd); |
| 339 | grpc_pollset_set_add_fd(&exec_ctx, pollset_sets[1].pss, tfds[1].fd); |
| 340 | |
| 341 | grpc_pollset_add_fd(&exec_ctx, pollset.ps, tfds[1].fd); |
| 342 | grpc_pollset_add_fd(&exec_ctx, pollset.ps, tfds[2].fd); |
| 343 | |
| 344 | grpc_pollset_set_add_pollset(&exec_ctx, pollset_sets[1].pss, pollset.ps); |
| 345 | grpc_pollset_set_add_pollset_set(&exec_ctx, pollset_sets[0].pss, |
| 346 | pollset_sets[1].pss); |
| 347 | |
| 348 | /* Test. Make all FDs readable and make sure that can be observed by doing a |
| 349 | * grpc_pollset_work on the pollset 'PS' */ |
| 350 | make_test_fds_readable(tfds, num_fds); |
| 351 | |
| 352 | gpr_mu_lock(pollset.mu); |
| 353 | deadline = GRPC_TIMEOUT_MILLIS_TO_DEADLINE(2); |
| 354 | GPR_ASSERT(GRPC_ERROR_NONE == |
| 355 | grpc_pollset_work(&exec_ctx, pollset.ps, &worker, |
| 356 | gpr_now(GPR_CLOCK_MONOTONIC), deadline)); |
| 357 | gpr_mu_unlock(pollset.mu); |
| 358 | grpc_exec_ctx_flush(&exec_ctx); |
| 359 | |
| 360 | verify_readable_and_reset(&exec_ctx, tfds, num_fds); |
| 361 | grpc_exec_ctx_flush(&exec_ctx); |
| 362 | |
| 363 | /* Tear down */ |
| 364 | grpc_pollset_set_del_fd(&exec_ctx, pollset_sets[0].pss, tfds[0].fd); |
| 365 | grpc_pollset_set_del_fd(&exec_ctx, pollset_sets[1].pss, tfds[0].fd); |
| 366 | grpc_pollset_set_del_fd(&exec_ctx, pollset_sets[1].pss, tfds[1].fd); |
| 367 | |
| 368 | grpc_pollset_set_del_pollset(&exec_ctx, pollset_sets[1].pss, pollset.ps); |
| 369 | grpc_pollset_set_del_pollset_set(&exec_ctx, pollset_sets[0].pss, |
| 370 | pollset_sets[1].pss); |
| 371 | grpc_exec_ctx_flush(&exec_ctx); |
| 372 | |
| 373 | cleanup_test_fds(&exec_ctx, tfds, num_fds); |
| 374 | cleanup_test_pollsets(&exec_ctx, &pollset, num_ps); |
| 375 | cleanup_test_pollset_sets(pollset_sets, num_pss); |
| 376 | grpc_exec_ctx_finish(&exec_ctx); |
| 377 | } |
| 378 | |
| 379 | /* Pollset_set with an empty pollset */ |
| 380 | void pollset_set_test_empty_pollset() { |
| 381 | /* We construct the following structure for this test: |
| 382 | * |
| 383 | * +---> PS0 (EMPTY) |
| 384 | * | |
| 385 | * +---> FD0 |
| 386 | * | |
| 387 | * PSS0---+ |
| 388 | * | +---> FD1 |
| 389 | * | | |
| 390 | * +---> PS1--+ |
| 391 | * | |
| 392 | * +---> FD2 |
| 393 | */ |
| 394 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 395 | grpc_pollset_worker *worker; |
| 396 | gpr_timespec deadline; |
| 397 | |
| 398 | test_fd tfds[3]; |
| 399 | test_pollset pollsets[2]; |
| 400 | test_pollset_set pollset_set; |
Sree Kuchibhotla | 9930e4b | 2017-01-25 14:48:20 -0800 | [diff] [blame^] | 401 | const int num_fds = GPR_ARRAY_SIZE(tfds); |
| 402 | const int num_ps = GPR_ARRAY_SIZE(pollsets); |
| 403 | const int num_pss = 1; |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 404 | |
| 405 | init_test_fds(&exec_ctx, tfds, num_fds); |
| 406 | init_test_pollsets(pollsets, num_ps); |
| 407 | init_test_pollset_sets(&pollset_set, num_pss); |
| 408 | |
| 409 | /* Construct the structure */ |
| 410 | grpc_pollset_set_add_fd(&exec_ctx, pollset_set.pss, tfds[0].fd); |
| 411 | grpc_pollset_add_fd(&exec_ctx, pollsets[1].ps, tfds[1].fd); |
| 412 | grpc_pollset_add_fd(&exec_ctx, pollsets[1].ps, tfds[2].fd); |
| 413 | |
| 414 | grpc_pollset_set_add_pollset(&exec_ctx, pollset_set.pss, pollsets[0].ps); |
| 415 | grpc_pollset_set_add_pollset(&exec_ctx, pollset_set.pss, pollsets[1].ps); |
| 416 | |
| 417 | /* Test. Make all FDs readable and make sure that can be observed by doing |
| 418 | * grpc_pollset_work on the empty pollset 'PS0' */ |
| 419 | make_test_fds_readable(tfds, num_fds); |
| 420 | |
| 421 | gpr_mu_lock(pollsets[0].mu); |
| 422 | deadline = GRPC_TIMEOUT_MILLIS_TO_DEADLINE(2); |
| 423 | GPR_ASSERT(GRPC_ERROR_NONE == |
| 424 | grpc_pollset_work(&exec_ctx, pollsets[0].ps, &worker, |
| 425 | gpr_now(GPR_CLOCK_MONOTONIC), deadline)); |
| 426 | gpr_mu_unlock(pollsets[0].mu); |
| 427 | grpc_exec_ctx_flush(&exec_ctx); |
| 428 | |
| 429 | verify_readable_and_reset(&exec_ctx, tfds, num_fds); |
| 430 | grpc_exec_ctx_flush(&exec_ctx); |
| 431 | |
| 432 | /* Tear down */ |
| 433 | grpc_pollset_set_del_fd(&exec_ctx, pollset_set.pss, tfds[0].fd); |
| 434 | grpc_pollset_set_del_pollset(&exec_ctx, pollset_set.pss, pollsets[0].ps); |
| 435 | grpc_pollset_set_del_pollset(&exec_ctx, pollset_set.pss, pollsets[1].ps); |
| 436 | grpc_exec_ctx_flush(&exec_ctx); |
| 437 | |
| 438 | cleanup_test_fds(&exec_ctx, tfds, num_fds); |
| 439 | cleanup_test_pollsets(&exec_ctx, pollsets, num_ps); |
| 440 | cleanup_test_pollset_sets(&pollset_set, num_pss); |
| 441 | grpc_exec_ctx_finish(&exec_ctx); |
| 442 | } |
| 443 | |
| 444 | int main(int argc, char **argv) { |
Sree Kuchibhotla | 113267b | 2017-01-25 14:37:19 -0800 | [diff] [blame] | 445 | const char *poll_strategy = grpc_get_poll_strategy_name(); |
Sree Kuchibhotla | 645e30a | 2017-01-20 10:59:15 -0800 | [diff] [blame] | 446 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 447 | grpc_test_init(argc, argv); |
| 448 | grpc_iomgr_init(); |
| 449 | |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 450 | if (poll_strategy != NULL && strcmp(poll_strategy, "epoll") == 0) { |
| 451 | pollset_set_test_basic(); |
| 452 | pollset_set_test_dup_fds(); |
| 453 | pollset_set_test_empty_pollset(); |
| 454 | } else { |
| 455 | gpr_log(GPR_INFO, |
| 456 | "Skipping the test. The test is only relevant for 'epoll' " |
| 457 | "strategy. and the current strategy is: '%s'", |
| 458 | poll_strategy); |
| 459 | } |
| 460 | |
Sree Kuchibhotla | 645e30a | 2017-01-20 10:59:15 -0800 | [diff] [blame] | 461 | grpc_iomgr_shutdown(&exec_ctx); |
| 462 | grpc_exec_ctx_finish(&exec_ctx); |
Sree Kuchibhotla | ef8ef3b | 2016-12-09 23:03:33 -0800 | [diff] [blame] | 463 | return 0; |
| 464 | } |
| 465 | #else /* defined(GRPC_LINUX_EPOLL) */ |
| 466 | int main(int argc, char **argv) { return 0; } |
| 467 | #endif /* !defined(GRPC_LINUX_EPOLL) */ |