blob: 8686f4e7b7452c139323068f53a8e9eb5f2c93bc [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080019#include "test/core/end2end/cq_verifier.h"
20
21#include <stdarg.h>
22#include <stdio.h>
23#include <string.h>
24
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080025#include <grpc/byte_buffer.h>
David Garcia Quintasf74a49e2015-06-18 17:22:45 -070026#include <grpc/byte_buffer_reader.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080027#include <grpc/support/alloc.h>
28#include <grpc/support/log.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070029#include <grpc/support/string_util.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080030#include <grpc/support/time.h>
31#include <grpc/support/useful.h>
Craig Tiller9533d042016-03-25 17:11:06 -070032#include "src/core/lib/support/string.h"
33#include "src/core/lib/surface/event_string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080034
Craig Tiller64be9f72015-05-04 14:53:51 -070035#define ROOT_EXPECTATION 1000
36
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037/* a set of metadata we expect to find on an event */
38typedef struct metadata {
39 size_t count;
40 size_t cap;
Craig Tillerbaa14a92017-11-03 09:09:36 -070041 char** keys;
42 char** values;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043} metadata;
44
45/* details what we expect to find on a single event - and forms a linked
46 list to detail other expectations */
47typedef struct expectation {
Craig Tillerbaa14a92017-11-03 09:09:36 -070048 struct expectation* next;
49 const char* file;
Mark D. Roth7187ab92016-08-24 13:49:22 -070050 int line;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051 grpc_completion_type type;
Craig Tillerbaa14a92017-11-03 09:09:36 -070052 void* tag;
Craig Tiller64be9f72015-05-04 14:53:51 -070053 int success;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054} expectation;
55
56/* the verifier itself */
57struct cq_verifier {
58 /* bound completion queue */
Craig Tillerbaa14a92017-11-03 09:09:36 -070059 grpc_completion_queue* cq;
Mark D. Roth37c1c8f2016-08-18 07:05:11 -070060 /* start of expectation list */
Craig Tillerbaa14a92017-11-03 09:09:36 -070061 expectation* first_expectation;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062};
63
Craig Tillerbaa14a92017-11-03 09:09:36 -070064cq_verifier* cq_verifier_create(grpc_completion_queue* cq) {
65 cq_verifier* v = (cq_verifier*)gpr_malloc(sizeof(cq_verifier));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080066 v->cq = cq;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -080067 v->first_expectation = nullptr;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068 return v;
69}
70
Craig Tillerbaa14a92017-11-03 09:09:36 -070071void cq_verifier_destroy(cq_verifier* v) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072 cq_verify(v);
73 gpr_free(v);
74}
75
Craig Tillerbaa14a92017-11-03 09:09:36 -070076static int has_metadata(const grpc_metadata* md, size_t count, const char* key,
77 const char* value) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078 size_t i;
79 for (i = 0; i < count; i++) {
Craig Tiller7c70b6c2017-01-23 07:48:42 -080080 if (0 == grpc_slice_str_cmp(md[i].key, key) &&
81 0 == grpc_slice_str_cmp(md[i].value, value)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080082 return 1;
83 }
84 }
85 return 0;
86}
87
Craig Tillerbaa14a92017-11-03 09:09:36 -070088int contains_metadata(grpc_metadata_array* array, const char* key,
89 const char* value) {
Craig Tiller16c39672015-02-05 12:57:08 -080090 return has_metadata(array->metadata, array->count, key, value);
91}
92
Craig Tillerbaa14a92017-11-03 09:09:36 -070093static int has_metadata_slices(const grpc_metadata* md, size_t count,
Craig Tiller7c70b6c2017-01-23 07:48:42 -080094 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 Tillerbaa14a92017-11-03 09:09:36 -0700104int contains_metadata_slices(grpc_metadata_array* array, grpc_slice key,
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800105 grpc_slice value) {
106 return has_metadata_slices(array->metadata, array->count, key, value);
107}
108
Craig Tillerbaa14a92017-11-03 09:09:36 -0700109static grpc_slice merge_slices(grpc_slice* slices, size_t nslices) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800110 size_t i;
111 size_t len = 0;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700112 uint8_t* cursor;
Craig Tillerd41a4a72016-10-26 16:16:06 -0700113 grpc_slice out;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800114
115 for (i = 0; i < nslices; i++) {
Craig Tiller618e67d2016-10-26 21:08:10 -0700116 len += GRPC_SLICE_LENGTH(slices[i]);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800117 }
118
Craig Tillerd41a4a72016-10-26 16:16:06 -0700119 out = grpc_slice_malloc(len);
Craig Tiller618e67d2016-10-26 21:08:10 -0700120 cursor = GRPC_SLICE_START_PTR(out);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800121
122 for (i = 0; i < nslices; i++) {
Craig Tiller28b72422016-10-26 21:15:29 -0700123 memcpy(cursor, GRPC_SLICE_START_PTR(slices[i]),
124 GRPC_SLICE_LENGTH(slices[i]));
Craig Tiller618e67d2016-10-26 21:08:10 -0700125 cursor += GRPC_SLICE_LENGTH(slices[i]);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126 }
127
128 return out;
129}
130
Craig Tillerbaa14a92017-11-03 09:09:36 -0700131int raw_byte_buffer_eq_slice(grpc_byte_buffer* rbb, grpc_slice b) {
Craig Tillerd41a4a72016-10-26 16:16:06 -0700132 grpc_slice a;
Craig Tillerc2599882015-05-19 15:47:19 -0700133 int ok;
134
Robbie Shaded5d851b2016-07-15 17:16:00 -0400135 if (!rbb) return 0;
Craig Tillerc2599882015-05-19 15:47:19 -0700136
Robbie Shaded5d851b2016-07-15 17:16:00 -0400137 a = merge_slices(rbb->data.raw.slice_buffer.slices,
138 rbb->data.raw.slice_buffer.count);
Craig Tiller618e67d2016-10-26 21:08:10 -0700139 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 Tillerd41a4a72016-10-26 16:16:06 -0700142 grpc_slice_unref(a);
143 grpc_slice_unref(b);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800144 return ok;
145}
146
Craig Tillerbaa14a92017-11-03 09:09:36 -0700147int byte_buffer_eq_slice(grpc_byte_buffer* bb, grpc_slice b) {
Robbie Shaded5d851b2016-07-15 17:16:00 -0400148 grpc_byte_buffer_reader reader;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700149 grpc_byte_buffer* rbb;
Robbie Shaded5d851b2016-07-15 17:16:00 -0400150 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 Tillerbaa14a92017-11-03 09:09:36 -0700162int byte_buffer_eq_string(grpc_byte_buffer* bb, const char* str) {
David Garcia Quintasf74a49e2015-06-18 17:22:45 -0700163 grpc_byte_buffer_reader reader;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700164 grpc_byte_buffer* rbb;
David Garcia Quintasf74a49e2015-06-18 17:22:45 -0700165 int res;
166
David Garcia Quintas6721d4f2016-06-30 17:17:23 -0700167 GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, bb) &&
168 "Couldn't init byte buffer reader");
David Garcia Quintasf74a49e2015-06-18 17:22:45 -0700169 rbb = grpc_raw_byte_buffer_from_reader(&reader);
Craig Tillerd41a4a72016-10-26 16:16:06 -0700170 res = raw_byte_buffer_eq_slice(rbb, grpc_slice_from_copied_string(str));
David Garcia Quintasf74a49e2015-06-18 17:22:45 -0700171 grpc_byte_buffer_reader_destroy(&reader);
172 grpc_byte_buffer_destroy(rbb);
173
174 return res;
Craig Tiller6a60cba2015-02-04 13:00:17 -0800175}
176
Craig Tillerbaa14a92017-11-03 09:09:36 -0700177static bool is_probably_integer(void* p) { return ((uintptr_t)p) < 1000000; }
Craig Tillercc44a382017-05-17 12:40:37 -0700178
Craig Tillerbaa14a92017-11-03 09:09:36 -0700179static void expectation_to_strvec(gpr_strvec* buf, expectation* e) {
180 char* tmp;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800181
Craig Tillercc44a382017-05-17 12:40:37 -0700182 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 Tiller9dcb58c2015-05-29 22:06:35 -0700187 gpr_strvec_add(buf, tmp);
188
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800189 switch (e->type) {
Craig Tillerfb189f82015-02-03 12:07:07 -0800190 case GRPC_OP_COMPLETE:
Mark D. Roth13ded3f2017-05-02 10:43:48 -0700191 gpr_asprintf(&tmp, "GRPC_OP_COMPLETE success=%d %s:%d", e->success,
Mark D. Roth661408c2016-08-26 13:45:01 -0700192 e->file, e->line);
Craig Tillercce17ac2015-01-20 09:29:28 -0800193 gpr_strvec_add(buf, tmp);
Craig Tiller33857822015-01-23 15:39:40 -0800194 break;
Craig Tiller64be9f72015-05-04 14:53:51 -0700195 case GRPC_QUEUE_TIMEOUT:
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800196 case GRPC_QUEUE_SHUTDOWN:
197 gpr_log(GPR_ERROR, "not implemented");
198 abort();
199 break;
200 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800201}
202
Craig Tillerbaa14a92017-11-03 09:09:36 -0700203static void expectations_to_strvec(gpr_strvec* buf, cq_verifier* v) {
204 expectation* e;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800205
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800206 for (e = v->first_expectation; e != nullptr; e = e->next) {
Craig Tiller33857822015-01-23 15:39:40 -0800207 expectation_to_strvec(buf, e);
208 gpr_strvec_add(buf, gpr_strdup("\n"));
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800209 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800210}
211
Craig Tillerbaa14a92017-11-03 09:09:36 -0700212static void fail_no_event_received(cq_verifier* v) {
Craig Tiller33857822015-01-23 15:39:40 -0800213 gpr_strvec buf;
Craig Tillerbaa14a92017-11-03 09:09:36 -0700214 char* msg;
Craig Tiller33857822015-01-23 15:39:40 -0800215 gpr_strvec_init(&buf);
216 gpr_strvec_add(&buf, gpr_strdup("no event received, but expected:\n"));
217 expectations_to_strvec(&buf, v);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800218 msg = gpr_strvec_flatten(&buf, nullptr);
Craig Tiller33857822015-01-23 15:39:40 -0800219 gpr_log(GPR_ERROR, "%s", msg);
220 gpr_strvec_destroy(&buf);
221 gpr_free(msg);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800222 abort();
223}
224
Craig Tillerbaa14a92017-11-03 09:09:36 -0700225static void verify_matches(expectation* e, grpc_event* ev) {
Mark D. Roth13ded3f2017-05-02 10:43:48 -0700226 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 Tiller4ac2b8e2017-11-10 14:14:17 -0800233 char* s = gpr_strvec_flatten(&expected, nullptr);
Mark D. Roth13ded3f2017-05-02 10:43:48 -0700234 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 Tillerbaa14a92017-11-03 09:09:36 -0700251void cq_verify(cq_verifier* v) {
Robbie Shadeca7effc2017-01-17 09:14:29 -0500252 const gpr_timespec deadline = grpc_timeout_seconds_to_deadline(10);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800253 while (v->first_expectation != nullptr) {
254 grpc_event ev = grpc_completion_queue_next(v->cq, deadline, nullptr);
Craig Tiller64be9f72015-05-04 14:53:51 -0700255 if (ev.type == GRPC_QUEUE_TIMEOUT) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800256 fail_no_event_received(v);
Craig Tiller64be9f72015-05-04 14:53:51 -0700257 break;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800258 }
Craig Tillerbaa14a92017-11-03 09:09:36 -0700259 expectation* e;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800260 expectation* prev = nullptr;
261 for (e = v->first_expectation; e != nullptr; e = e->next) {
Craig Tiller64be9f72015-05-04 14:53:51 -0700262 if (e->tag == ev.tag) {
263 verify_matches(e, &ev);
Mark D. Roth5f3cb4d2016-08-15 14:50:11 -0700264 if (e == v->first_expectation) v->first_expectation = e->next;
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800265 if (prev != nullptr) prev->next = e->next;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800266 gpr_free(e);
267 break;
268 }
Mark D. Rothcf501a72016-08-02 14:04:52 -0700269 prev = e;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800270 }
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800271 if (e == nullptr) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700272 char* s = grpc_event_string(&ev);
Mark D. Rothcf501a72016-08-02 14:04:52 -0700273 gpr_log(GPR_ERROR, "cq returned unexpected event: %s", s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800274 gpr_free(s);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700275 gpr_strvec expectations;
276 gpr_strvec_init(&expectations);
277 expectations_to_strvec(&expectations, v);
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800278 s = gpr_strvec_flatten(&expectations, nullptr);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700279 gpr_strvec_destroy(&expectations);
280 gpr_log(GPR_ERROR, "expected tags:\n%s", s);
Craig Tiller715342e2015-01-23 15:42:33 -0800281 gpr_free(s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800282 abort();
283 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800284 }
285}
286
Craig Tillerbaa14a92017-11-03 09:09:36 -0700287void cq_verify_empty_timeout(cq_verifier* v, int timeout_sec) {
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700288 gpr_timespec deadline =
289 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
290 gpr_time_from_seconds(timeout_sec, GPR_TIMESPAN));
Craig Tiller64be9f72015-05-04 14:53:51 -0700291 grpc_event ev;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800292
Craig Tillerbe98d242017-11-10 15:26:57 -0800293 GPR_ASSERT(v->first_expectation == nullptr &&
294 "expectation queue must be empty");
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800295
Craig Tiller4ac2b8e2017-11-10 14:14:17 -0800296 ev = grpc_completion_queue_next(v->cq, deadline, nullptr);
Craig Tiller64be9f72015-05-04 14:53:51 -0700297 if (ev.type != GRPC_QUEUE_TIMEOUT) {
Craig Tillerbaa14a92017-11-03 09:09:36 -0700298 char* s = grpc_event_string(&ev);
ctiller58393c22015-01-07 14:03:30 -0800299 gpr_log(GPR_ERROR, "unexpected event (expected nothing): %s", s);
300 gpr_free(s);
301 abort();
302 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800303}
304
Craig Tillerbaa14a92017-11-03 09:09:36 -0700305void cq_verify_empty(cq_verifier* v) { cq_verify_empty_timeout(v, 1); }
David Garcia Quintas3fb8f732016-06-15 22:53:08 -0700306
Craig Tillerbaa14a92017-11-03 09:09:36 -0700307static 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 Nobleb7ebd3b2014-11-26 16:33:03 -0800310 e->type = type;
Mark D. Roth7187ab92016-08-24 13:49:22 -0700311 e->file = file;
312 e->line = line;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800313 e->tag = tag;
Mark D. Rothcf501a72016-08-02 14:04:52 -0700314 e->success = success;
Mark D. Roth37c1c8f2016-08-18 07:05:11 -0700315 e->next = v->first_expectation;
316 v->first_expectation = e;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800317}
318
Craig Tillerbaa14a92017-11-03 09:09:36 -0700319void cq_expect_completion(cq_verifier* v, const char* file, int line, void* tag,
Mark D. Roth661408c2016-08-26 13:45:01 -0700320 bool success) {
Mark D. Roth7187ab92016-08-24 13:49:22 -0700321 add(v, file, line, GRPC_OP_COMPLETE, tag, success);
Craig Tiller190d3602015-02-18 09:23:38 -0800322}