3 // Copyright 2015 gRPC authors.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "test/core/end2end/cq_verifier.h"
30 #include "absl/strings/str_format.h"
31 #include "absl/strings/str_join.h"
33 #include <grpc/byte_buffer.h>
34 #include <grpc/byte_buffer_reader.h>
35 #include <grpc/support/alloc.h>
36 #include <grpc/support/log.h>
37 #include <grpc/support/string_util.h>
38 #include <grpc/support/time.h>
39 #include "src/core/lib/compression/compression_internal.h"
40 #include "src/core/lib/compression/message_compress.h"
41 #include "src/core/lib/gpr/string.h"
42 #include "src/core/lib/surface/event_string.h"
44 #define ROOT_EXPECTATION 1000
46 // a set of metadata we expect to find on an event
47 typedef struct metadata {
54 // details what we expect to find on a single event
56 Expectation(const char* f, int l, grpc_completion_type t, void* tag_arg,
57 bool check_success_arg, int success_arg, bool* seen_arg)
62 check_success(check_success_arg),
67 grpc_completion_type type;
74 // the verifier itself
76 // bound completion queue
77 grpc_completion_queue* cq;
79 std::list<Expectation> expectations;
80 // maybe expectation list
81 std::list<Expectation> maybe_expectations;
84 // TODO(yashykt): Convert this to constructor/destructor pair
85 cq_verifier* cq_verifier_create(grpc_completion_queue* cq) {
86 cq_verifier* v = new cq_verifier;
91 void cq_verifier_destroy(cq_verifier* v) {
96 static int has_metadata(const grpc_metadata* md, size_t count, const char* key,
99 for (i = 0; i < count; i++) {
100 if (0 == grpc_slice_str_cmp(md[i].key, key) &&
101 0 == grpc_slice_str_cmp(md[i].value, value)) {
108 int contains_metadata(grpc_metadata_array* array, const char* key,
110 return has_metadata(array->metadata, array->count, key, value);
113 static int has_metadata_slices(const grpc_metadata* md, size_t count,
114 grpc_slice key, grpc_slice value) {
116 for (i = 0; i < count; i++) {
117 if (grpc_slice_eq(md[i].key, key) && grpc_slice_eq(md[i].value, value)) {
124 int contains_metadata_slices(grpc_metadata_array* array, grpc_slice key,
126 return has_metadata_slices(array->metadata, array->count, key, value);
129 static grpc_slice merge_slices(grpc_slice* slices, size_t nslices) {
135 for (i = 0; i < nslices; i++) {
136 len += GRPC_SLICE_LENGTH(slices[i]);
139 out = grpc_slice_malloc(len);
140 cursor = GRPC_SLICE_START_PTR(out);
142 for (i = 0; i < nslices; i++) {
143 memcpy(cursor, GRPC_SLICE_START_PTR(slices[i]),
144 GRPC_SLICE_LENGTH(slices[i]));
145 cursor += GRPC_SLICE_LENGTH(slices[i]);
151 int raw_byte_buffer_eq_slice(grpc_byte_buffer* rbb, grpc_slice b) {
157 a = merge_slices(rbb->data.raw.slice_buffer.slices,
158 rbb->data.raw.slice_buffer.count);
159 ok = GRPC_SLICE_LENGTH(a) == GRPC_SLICE_LENGTH(b) &&
160 0 == memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b),
161 GRPC_SLICE_LENGTH(a));
167 int byte_buffer_eq_slice(grpc_byte_buffer* bb, grpc_slice b) {
168 if (bb->data.raw.compression > GRPC_COMPRESS_NONE) {
169 grpc_slice_buffer decompressed_buffer;
170 grpc_slice_buffer_init(&decompressed_buffer);
171 GPR_ASSERT(grpc_msg_decompress(
172 grpc_compression_algorithm_to_message_compression_algorithm(
173 bb->data.raw.compression),
174 &bb->data.raw.slice_buffer, &decompressed_buffer));
175 grpc_byte_buffer* rbb = grpc_raw_byte_buffer_create(
176 decompressed_buffer.slices, decompressed_buffer.count);
177 int ret_val = raw_byte_buffer_eq_slice(rbb, b);
178 grpc_byte_buffer_destroy(rbb);
179 grpc_slice_buffer_destroy(&decompressed_buffer);
182 return raw_byte_buffer_eq_slice(bb, b);
185 int byte_buffer_eq_string(grpc_byte_buffer* bb, const char* str) {
186 return byte_buffer_eq_slice(bb, grpc_slice_from_copied_string(str));
189 static bool is_probably_integer(void* p) {
190 return reinterpret_cast<uintptr_t>(p) < 1000000;
195 std::string ExpectationString(const Expectation& e) {
197 if (is_probably_integer(e.tag)) {
198 out = absl::StrFormat("tag(%" PRIdPTR ") ",
199 reinterpret_cast<intptr_t>(e.tag));
201 out = absl::StrFormat("%p ", e.tag);
204 case GRPC_OP_COMPLETE:
205 absl::StrAppendFormat(&out, "GRPC_OP_COMPLETE success=%d %s:%d",
206 e.success, e.file, e.line);
208 case GRPC_QUEUE_TIMEOUT:
209 case GRPC_QUEUE_SHUTDOWN:
210 gpr_log(GPR_ERROR, "not implemented");
217 std::string ExpectationsString(const cq_verifier& v) {
218 std::vector<std::string> expectations;
219 for (const auto& e : v.expectations) {
220 expectations.push_back(ExpectationString(e));
222 return absl::StrJoin(expectations, "\n");
227 static void fail_no_event_received(cq_verifier* v) {
228 gpr_log(GPR_ERROR, "no event received, but expected:%s",
229 ExpectationsString(*v).c_str());
233 static void verify_matches(const Expectation& e, const grpc_event& ev) {
234 GPR_ASSERT(e.type == ev.type);
236 case GRPC_OP_COMPLETE:
237 if (e.check_success && e.success != ev.success) {
238 gpr_log(GPR_ERROR, "actual success does not match expected: %s",
239 ExpectationString(e).c_str());
243 case GRPC_QUEUE_SHUTDOWN:
244 gpr_log(GPR_ERROR, "premature queue shutdown");
247 case GRPC_QUEUE_TIMEOUT:
248 gpr_log(GPR_ERROR, "not implemented");
254 // Try to find the event in the expectations list
255 bool FindExpectations(std::list<Expectation>* expectations,
256 const grpc_event& ev) {
257 for (auto e = expectations->begin(); e != expectations->end(); ++e) {
258 if (e->tag == ev.tag) {
259 verify_matches(*e, ev);
260 if (e->seen != nullptr) {
263 expectations->erase(e);
270 void cq_verify(cq_verifier* v, int timeout_sec) {
271 const gpr_timespec deadline = grpc_timeout_seconds_to_deadline(timeout_sec);
272 while (!v->expectations.empty()) {
273 grpc_event ev = grpc_completion_queue_next(v->cq, deadline, nullptr);
274 if (ev.type == GRPC_QUEUE_TIMEOUT) {
275 fail_no_event_received(v);
278 if (FindExpectations(&v->expectations, ev)) continue;
279 if (FindExpectations(&v->maybe_expectations, ev)) continue;
280 gpr_log(GPR_ERROR, "cq returned unexpected event: %s",
281 grpc_event_string(&ev).c_str());
282 gpr_log(GPR_ERROR, "expected tags:\n%s", ExpectationsString(*v).c_str());
285 v->maybe_expectations.clear();
288 void cq_verify_empty_timeout(cq_verifier* v, int timeout_sec) {
289 gpr_timespec deadline =
290 gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
291 gpr_time_from_seconds(timeout_sec, GPR_TIMESPAN));
294 GPR_ASSERT(v->expectations.empty() && "expectation queue must be empty");
296 ev = grpc_completion_queue_next(v->cq, deadline, nullptr);
297 if (ev.type != GRPC_QUEUE_TIMEOUT) {
298 gpr_log(GPR_ERROR, "unexpected event (expected nothing): %s",
299 grpc_event_string(&ev).c_str());
304 void cq_verify_empty(cq_verifier* v) { cq_verify_empty_timeout(v, 1); }
306 void cq_maybe_expect_completion(cq_verifier* v, const char* file, int line,
307 void* tag, bool success, bool* seen) {
308 v->maybe_expectations.emplace_back(file, line, GRPC_OP_COMPLETE, tag,
309 true /* check_success */, success, seen);
312 static void add(cq_verifier* v, const char* file, int line,
313 grpc_completion_type type, void* tag, bool check_success,
315 v->expectations.emplace_back(file, line, type, tag, check_success, success,
319 void cq_expect_completion(cq_verifier* v, const char* file, int line, void* tag,
321 add(v, file, line, GRPC_OP_COMPLETE, tag, true, success);
324 void cq_expect_completion_any_status(cq_verifier* v, const char* file, int line,
326 add(v, file, line, GRPC_OP_COMPLETE, tag, false, false);