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