Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [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 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 34 | #include "test/core/end2end/cq_verifier.h" |
| 35 | |
| 36 | #include <stdarg.h> |
| 37 | #include <stdio.h> |
| 38 | #include <string.h> |
| 39 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include <grpc/byte_buffer.h> |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 41 | #include <grpc/byte_buffer_reader.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | #include <grpc/support/alloc.h> |
| 43 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 44 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | #include <grpc/support/time.h> |
| 46 | #include <grpc/support/useful.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 47 | #include "src/core/lib/support/string.h" |
| 48 | #include "src/core/lib/surface/event_string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 50 | #define ROOT_EXPECTATION 1000 |
| 51 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 52 | /* a set of metadata we expect to find on an event */ |
| 53 | typedef struct metadata { |
| 54 | size_t count; |
| 55 | size_t cap; |
Craig Tiller | e005d22 | 2015-01-21 15:02:20 -0800 | [diff] [blame] | 56 | char **keys; |
| 57 | char **values; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | } metadata; |
| 59 | |
| 60 | /* details what we expect to find on a single event - and forms a linked |
| 61 | list to detail other expectations */ |
| 62 | typedef struct expectation { |
| 63 | struct expectation *next; |
Mark D. Roth | 7187ab9 | 2016-08-24 13:49:22 -0700 | [diff] [blame] | 64 | const char *file; |
| 65 | int line; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | grpc_completion_type type; |
| 67 | void *tag; |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 68 | int success; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 69 | } expectation; |
| 70 | |
| 71 | /* the verifier itself */ |
| 72 | struct cq_verifier { |
| 73 | /* bound completion queue */ |
| 74 | grpc_completion_queue *cq; |
Mark D. Roth | 37c1c8f | 2016-08-18 07:05:11 -0700 | [diff] [blame] | 75 | /* start of expectation list */ |
Mark D. Roth | 5f3cb4d | 2016-08-15 14:50:11 -0700 | [diff] [blame] | 76 | expectation *first_expectation; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | cq_verifier *cq_verifier_create(grpc_completion_queue *cq) { |
Craig Tiller | 4d16f4d | 2017-05-10 08:21:08 -0700 | [diff] [blame^] | 80 | cq_verifier *v = (cq_verifier *)gpr_malloc(sizeof(cq_verifier)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | v->cq = cq; |
Mark D. Roth | cf501a7 | 2016-08-02 14:04:52 -0700 | [diff] [blame] | 82 | v->first_expectation = NULL; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | return v; |
| 84 | } |
| 85 | |
| 86 | void cq_verifier_destroy(cq_verifier *v) { |
| 87 | cq_verify(v); |
| 88 | gpr_free(v); |
| 89 | } |
| 90 | |
| 91 | static int has_metadata(const grpc_metadata *md, size_t count, const char *key, |
| 92 | const char *value) { |
| 93 | size_t i; |
| 94 | for (i = 0; i < count; i++) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 95 | if (0 == grpc_slice_str_cmp(md[i].key, key) && |
| 96 | 0 == grpc_slice_str_cmp(md[i].value, value)) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 97 | return 1; |
| 98 | } |
| 99 | } |
| 100 | return 0; |
| 101 | } |
| 102 | |
Craig Tiller | 8ad8a41 | 2015-02-25 08:36:40 -0800 | [diff] [blame] | 103 | int contains_metadata(grpc_metadata_array *array, const char *key, |
| 104 | const char *value) { |
Craig Tiller | 16c3967 | 2015-02-05 12:57:08 -0800 | [diff] [blame] | 105 | return has_metadata(array->metadata, array->count, key, value); |
| 106 | } |
| 107 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 108 | static int has_metadata_slices(const grpc_metadata *md, size_t count, |
| 109 | grpc_slice key, grpc_slice value) { |
| 110 | size_t i; |
| 111 | for (i = 0; i < count; i++) { |
| 112 | if (grpc_slice_eq(md[i].key, key) && grpc_slice_eq(md[i].value, value)) { |
| 113 | return 1; |
| 114 | } |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int contains_metadata_slices(grpc_metadata_array *array, grpc_slice key, |
| 120 | grpc_slice value) { |
| 121 | return has_metadata_slices(array->metadata, array->count, key, value); |
| 122 | } |
| 123 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 124 | static grpc_slice merge_slices(grpc_slice *slices, size_t nslices) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 125 | size_t i; |
| 126 | size_t len = 0; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 127 | uint8_t *cursor; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 128 | grpc_slice out; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 129 | |
| 130 | for (i = 0; i < nslices; i++) { |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 131 | len += GRPC_SLICE_LENGTH(slices[i]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 134 | out = grpc_slice_malloc(len); |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 135 | cursor = GRPC_SLICE_START_PTR(out); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 136 | |
| 137 | for (i = 0; i < nslices; i++) { |
Craig Tiller | 28b7242 | 2016-10-26 21:15:29 -0700 | [diff] [blame] | 138 | memcpy(cursor, GRPC_SLICE_START_PTR(slices[i]), |
| 139 | GRPC_SLICE_LENGTH(slices[i])); |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 140 | cursor += GRPC_SLICE_LENGTH(slices[i]); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | return out; |
| 144 | } |
| 145 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 146 | int raw_byte_buffer_eq_slice(grpc_byte_buffer *rbb, grpc_slice b) { |
| 147 | grpc_slice a; |
Craig Tiller | c259988 | 2015-05-19 15:47:19 -0700 | [diff] [blame] | 148 | int ok; |
| 149 | |
Robbie Shade | d5d851b | 2016-07-15 17:16:00 -0400 | [diff] [blame] | 150 | if (!rbb) return 0; |
Craig Tiller | c259988 | 2015-05-19 15:47:19 -0700 | [diff] [blame] | 151 | |
Robbie Shade | d5d851b | 2016-07-15 17:16:00 -0400 | [diff] [blame] | 152 | a = merge_slices(rbb->data.raw.slice_buffer.slices, |
| 153 | rbb->data.raw.slice_buffer.count); |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 154 | ok = GRPC_SLICE_LENGTH(a) == GRPC_SLICE_LENGTH(b) && |
| 155 | 0 == memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b), |
| 156 | GRPC_SLICE_LENGTH(a)); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 157 | grpc_slice_unref(a); |
| 158 | grpc_slice_unref(b); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 159 | return ok; |
| 160 | } |
| 161 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 162 | int byte_buffer_eq_slice(grpc_byte_buffer *bb, grpc_slice b) { |
Robbie Shade | d5d851b | 2016-07-15 17:16:00 -0400 | [diff] [blame] | 163 | grpc_byte_buffer_reader reader; |
| 164 | grpc_byte_buffer *rbb; |
| 165 | int res; |
| 166 | |
| 167 | GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, bb) && |
| 168 | "Couldn't init byte buffer reader"); |
| 169 | rbb = grpc_raw_byte_buffer_from_reader(&reader); |
| 170 | res = raw_byte_buffer_eq_slice(rbb, b); |
| 171 | grpc_byte_buffer_reader_destroy(&reader); |
| 172 | grpc_byte_buffer_destroy(rbb); |
| 173 | |
| 174 | return res; |
| 175 | } |
| 176 | |
Craig Tiller | 6a60cba | 2015-02-04 13:00:17 -0800 | [diff] [blame] | 177 | int byte_buffer_eq_string(grpc_byte_buffer *bb, const char *str) { |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 178 | grpc_byte_buffer_reader reader; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 179 | grpc_byte_buffer *rbb; |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 180 | int res; |
| 181 | |
David Garcia Quintas | 6721d4f | 2016-06-30 17:17:23 -0700 | [diff] [blame] | 182 | GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, bb) && |
| 183 | "Couldn't init byte buffer reader"); |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 184 | rbb = grpc_raw_byte_buffer_from_reader(&reader); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 185 | res = raw_byte_buffer_eq_slice(rbb, grpc_slice_from_copied_string(str)); |
David Garcia Quintas | f74a49e | 2015-06-18 17:22:45 -0700 | [diff] [blame] | 186 | grpc_byte_buffer_reader_destroy(&reader); |
| 187 | grpc_byte_buffer_destroy(rbb); |
| 188 | |
| 189 | return res; |
Craig Tiller | 6a60cba | 2015-02-04 13:00:17 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Craig Tiller | 3385782 | 2015-01-23 15:39:40 -0800 | [diff] [blame] | 192 | static void expectation_to_strvec(gpr_strvec *buf, expectation *e) { |
Craig Tiller | 3385782 | 2015-01-23 15:39:40 -0800 | [diff] [blame] | 193 | char *tmp; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 194 | |
Craig Tiller | 9dcb58c | 2015-05-29 22:06:35 -0700 | [diff] [blame] | 195 | gpr_asprintf(&tmp, "%p ", e->tag); |
| 196 | gpr_strvec_add(buf, tmp); |
| 197 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 198 | switch (e->type) { |
Craig Tiller | fb189f8 | 2015-02-03 12:07:07 -0800 | [diff] [blame] | 199 | case GRPC_OP_COMPLETE: |
Mark D. Roth | 13ded3f | 2017-05-02 10:43:48 -0700 | [diff] [blame] | 200 | gpr_asprintf(&tmp, "GRPC_OP_COMPLETE success=%d %s:%d", e->success, |
Mark D. Roth | 661408c | 2016-08-26 13:45:01 -0700 | [diff] [blame] | 201 | e->file, e->line); |
Craig Tiller | cce17ac | 2015-01-20 09:29:28 -0800 | [diff] [blame] | 202 | gpr_strvec_add(buf, tmp); |
Craig Tiller | 3385782 | 2015-01-23 15:39:40 -0800 | [diff] [blame] | 203 | break; |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 204 | case GRPC_QUEUE_TIMEOUT: |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 205 | case GRPC_QUEUE_SHUTDOWN: |
| 206 | gpr_log(GPR_ERROR, "not implemented"); |
| 207 | abort(); |
| 208 | break; |
| 209 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Craig Tiller | 3385782 | 2015-01-23 15:39:40 -0800 | [diff] [blame] | 212 | static void expectations_to_strvec(gpr_strvec *buf, cq_verifier *v) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 213 | expectation *e; |
| 214 | |
Mark D. Roth | cf501a7 | 2016-08-02 14:04:52 -0700 | [diff] [blame] | 215 | for (e = v->first_expectation; e != NULL; e = e->next) { |
Craig Tiller | 3385782 | 2015-01-23 15:39:40 -0800 | [diff] [blame] | 216 | expectation_to_strvec(buf, e); |
| 217 | gpr_strvec_add(buf, gpr_strdup("\n")); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 218 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static void fail_no_event_received(cq_verifier *v) { |
Craig Tiller | 3385782 | 2015-01-23 15:39:40 -0800 | [diff] [blame] | 222 | gpr_strvec buf; |
| 223 | char *msg; |
| 224 | gpr_strvec_init(&buf); |
| 225 | gpr_strvec_add(&buf, gpr_strdup("no event received, but expected:\n")); |
| 226 | expectations_to_strvec(&buf, v); |
| 227 | msg = gpr_strvec_flatten(&buf, NULL); |
| 228 | gpr_log(GPR_ERROR, "%s", msg); |
| 229 | gpr_strvec_destroy(&buf); |
| 230 | gpr_free(msg); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 231 | abort(); |
| 232 | } |
| 233 | |
Mark D. Roth | 13ded3f | 2017-05-02 10:43:48 -0700 | [diff] [blame] | 234 | static void verify_matches(expectation *e, grpc_event *ev) { |
| 235 | GPR_ASSERT(e->type == ev->type); |
| 236 | switch (e->type) { |
| 237 | case GRPC_OP_COMPLETE: |
| 238 | if (e->success != ev->success) { |
| 239 | gpr_strvec expected; |
| 240 | gpr_strvec_init(&expected); |
| 241 | expectation_to_strvec(&expected, e); |
| 242 | char *s = gpr_strvec_flatten(&expected, NULL); |
| 243 | gpr_strvec_destroy(&expected); |
| 244 | gpr_log(GPR_ERROR, "actual success does not match expected: %s", s); |
| 245 | gpr_free(s); |
| 246 | abort(); |
| 247 | } |
| 248 | break; |
| 249 | case GRPC_QUEUE_SHUTDOWN: |
| 250 | gpr_log(GPR_ERROR, "premature queue shutdown"); |
| 251 | abort(); |
| 252 | break; |
| 253 | case GRPC_QUEUE_TIMEOUT: |
| 254 | gpr_log(GPR_ERROR, "not implemented"); |
| 255 | abort(); |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 260 | void cq_verify(cq_verifier *v) { |
Robbie Shade | ca7effc | 2017-01-17 09:14:29 -0500 | [diff] [blame] | 261 | const gpr_timespec deadline = grpc_timeout_seconds_to_deadline(10); |
Mark D. Roth | cf501a7 | 2016-08-02 14:04:52 -0700 | [diff] [blame] | 262 | while (v->first_expectation != NULL) { |
Mark D. Roth | 7187ab9 | 2016-08-24 13:49:22 -0700 | [diff] [blame] | 263 | grpc_event ev = grpc_completion_queue_next(v->cq, deadline, NULL); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 264 | if (ev.type == GRPC_QUEUE_TIMEOUT) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 265 | fail_no_event_received(v); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 266 | break; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 267 | } |
Mark D. Roth | 7187ab9 | 2016-08-24 13:49:22 -0700 | [diff] [blame] | 268 | expectation *e; |
Mark D. Roth | 5f3cb4d | 2016-08-15 14:50:11 -0700 | [diff] [blame] | 269 | expectation *prev = NULL; |
Mark D. Roth | cf501a7 | 2016-08-02 14:04:52 -0700 | [diff] [blame] | 270 | for (e = v->first_expectation; e != NULL; e = e->next) { |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 271 | if (e->tag == ev.tag) { |
| 272 | verify_matches(e, &ev); |
Mark D. Roth | 5f3cb4d | 2016-08-15 14:50:11 -0700 | [diff] [blame] | 273 | if (e == v->first_expectation) v->first_expectation = e->next; |
| 274 | if (prev != NULL) prev->next = e->next; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 275 | gpr_free(e); |
| 276 | break; |
| 277 | } |
Mark D. Roth | cf501a7 | 2016-08-02 14:04:52 -0700 | [diff] [blame] | 278 | prev = e; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 279 | } |
Mark D. Roth | cf501a7 | 2016-08-02 14:04:52 -0700 | [diff] [blame] | 280 | if (e == NULL) { |
Mark D. Roth | 7187ab9 | 2016-08-24 13:49:22 -0700 | [diff] [blame] | 281 | char *s = grpc_event_string(&ev); |
Mark D. Roth | cf501a7 | 2016-08-02 14:04:52 -0700 | [diff] [blame] | 282 | gpr_log(GPR_ERROR, "cq returned unexpected event: %s", s); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 283 | gpr_free(s); |
Mark D. Roth | 7187ab9 | 2016-08-24 13:49:22 -0700 | [diff] [blame] | 284 | gpr_strvec expectations; |
| 285 | gpr_strvec_init(&expectations); |
| 286 | expectations_to_strvec(&expectations, v); |
| 287 | s = gpr_strvec_flatten(&expectations, NULL); |
| 288 | gpr_strvec_destroy(&expectations); |
| 289 | gpr_log(GPR_ERROR, "expected tags:\n%s", s); |
Craig Tiller | 715342e | 2015-01-23 15:42:33 -0800 | [diff] [blame] | 290 | gpr_free(s); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 291 | abort(); |
| 292 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 296 | void cq_verify_empty_timeout(cq_verifier *v, int timeout_sec) { |
| 297 | gpr_timespec deadline = |
| 298 | gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 299 | gpr_time_from_seconds(timeout_sec, GPR_TIMESPAN)); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 300 | grpc_event ev; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 301 | |
Mark D. Roth | cf501a7 | 2016-08-02 14:04:52 -0700 | [diff] [blame] | 302 | GPR_ASSERT(v->first_expectation == NULL && "expectation queue must be empty"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 303 | |
Nicolas "Pixel" Noble | 9a123df | 2015-07-29 23:45:08 +0200 | [diff] [blame] | 304 | ev = grpc_completion_queue_next(v->cq, deadline, NULL); |
Craig Tiller | 64be9f7 | 2015-05-04 14:53:51 -0700 | [diff] [blame] | 305 | if (ev.type != GRPC_QUEUE_TIMEOUT) { |
| 306 | char *s = grpc_event_string(&ev); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 307 | gpr_log(GPR_ERROR, "unexpected event (expected nothing): %s", s); |
| 308 | gpr_free(s); |
| 309 | abort(); |
| 310 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 311 | } |
| 312 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 313 | void cq_verify_empty(cq_verifier *v) { cq_verify_empty_timeout(v, 1); } |
| 314 | |
Mark D. Roth | 7187ab9 | 2016-08-24 13:49:22 -0700 | [diff] [blame] | 315 | static void add(cq_verifier *v, const char *file, int line, |
| 316 | grpc_completion_type type, void *tag, bool success) { |
Craig Tiller | 4d16f4d | 2017-05-10 08:21:08 -0700 | [diff] [blame^] | 317 | expectation *e = (expectation *)gpr_malloc(sizeof(expectation)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 318 | e->type = type; |
Mark D. Roth | 7187ab9 | 2016-08-24 13:49:22 -0700 | [diff] [blame] | 319 | e->file = file; |
| 320 | e->line = line; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 321 | e->tag = tag; |
Mark D. Roth | cf501a7 | 2016-08-02 14:04:52 -0700 | [diff] [blame] | 322 | e->success = success; |
Mark D. Roth | 37c1c8f | 2016-08-18 07:05:11 -0700 | [diff] [blame] | 323 | e->next = v->first_expectation; |
| 324 | v->first_expectation = e; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Mark D. Roth | 661408c | 2016-08-26 13:45:01 -0700 | [diff] [blame] | 327 | void cq_expect_completion(cq_verifier *v, const char *file, int line, void *tag, |
| 328 | bool success) { |
Mark D. Roth | 7187ab9 | 2016-08-24 13:49:22 -0700 | [diff] [blame] | 329 | add(v, file, line, GRPC_OP_COMPLETE, tag, success); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 330 | } |