| 1 |
// |
| 2 |
// |
| 3 |
// Copyright 2015 gRPC authors. |
| 4 |
// |
| 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 |
| 8 |
// |
| 9 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
// |
| 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. |
| 16 |
// |
| 17 |
// |
| 18 |
|
| 19 |
#include "test/core/end2end/cq_verifier.h" |
| 20 |
|
| 21 |
#include <inttypes.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
#include <string> |
| 28 |
#include <utility> |
| 29 |
#include <vector> |
| 30 |
|
| 31 |
#include "absl/strings/str_cat.h" |
| 32 |
#include "absl/strings/str_format.h" |
| 33 |
#include "absl/strings/str_join.h" |
| 34 |
|
| 35 |
#include <grpc/byte_buffer.h> |
| 36 |
#include <grpc/compression.h> |
| 37 |
#include <grpc/grpc.h> |
| 38 |
#include <grpc/slice_buffer.h> |
| 39 |
#include <grpc/support/log.h> |
| 40 |
#include <grpc/support/time.h> |
| 41 |
|
| 42 |
#include "src/core/lib/compression/message_compress.h" |
| 43 |
#include "src/core/lib/gprpp/debug_location.h" |
| 44 |
#include "src/core/lib/gprpp/match.h" |
| 45 |
#include "src/core/lib/surface/event_string.h" |
| 46 |
#include "test/core/util/test_config.h" |
| 47 |
|
| 48 |
// a set of metadata we expect to find on an event |
| 49 |
typedef struct metadata { |
| 50 |
size_t count; |
| 51 |
size_t cap; |
| 52 |
char** keys; |
| 53 |
char** values; |
| 54 |
} metadata; |
| 55 |
|
| 56 |
static int has_metadata(const grpc_metadata* md, size_t count, const char* key, |
| 57 |
const char* value) { |
| 58 |
size_t i; |
| 59 |
for (i = 0; i < count; i++) { |
| 60 |
if (0 == grpc_slice_str_cmp(md[i].key, key) && |
| 61 |
0 == grpc_slice_str_cmp(md[i].value, value)) { |
| 62 |
return 1; |
| 63 |
} |
| 64 |
} |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
int contains_metadata(grpc_metadata_array* array, const char* key, |
| 69 |
const char* value) { |
| 70 |
return has_metadata(array->metadata, array->count, key, value); |
| 71 |
} |
| 72 |
|
| 73 |
static int has_metadata_slices(const grpc_metadata* md, size_t count, |
| 74 |
grpc_slice key, grpc_slice value) { |
| 75 |
size_t i; |
| 76 |
for (i = 0; i < count; i++) { |
| 77 |
if (grpc_slice_eq(md[i].key, key) && grpc_slice_eq(md[i].value, value)) { |
| 78 |
return 1; |
| 79 |
} |
| 80 |
} |
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
int contains_metadata_slices(grpc_metadata_array* array, grpc_slice key, |
| 85 |
grpc_slice value) { |
| 86 |
return has_metadata_slices(array->metadata, array->count, key, value); |
| 87 |
} |
| 88 |
|
| 89 |
static grpc_slice merge_slices(grpc_slice* slices, size_t nslices) { |
| 90 |
size_t i; |
| 91 |
size_t len = 0; |
| 92 |
uint8_t* cursor; |
| 93 |
grpc_slice out; |
| 94 |
|
| 95 |
for (i = 0; i < nslices; i++) { |
| 96 |
len += GRPC_SLICE_LENGTH(slices[i]); |
| 97 |
} |
| 98 |
|
| 99 |
out = grpc_slice_malloc(len); |
| 100 |
cursor = GRPC_SLICE_START_PTR(out); |
| 101 |
|
| 102 |
for (i = 0; i < nslices; i++) { |
| 103 |
memcpy(cursor, GRPC_SLICE_START_PTR(slices[i]), |
| 104 |
GRPC_SLICE_LENGTH(slices[i])); |
| 105 |
cursor += GRPC_SLICE_LENGTH(slices[i]); |
| 106 |
} |
| 107 |
|
| 108 |
return out; |
| 109 |
} |
| 110 |
|
| 111 |
int raw_byte_buffer_eq_slice(grpc_byte_buffer* rbb, grpc_slice b) { |
| 112 |
grpc_slice a; |
| 113 |
int ok; |
| 114 |
|
| 115 |
if (!rbb) return 0; |
| 116 |
|
| 117 |
a = merge_slices(rbb->data.raw.slice_buffer.slices, |
| 118 |
rbb->data.raw.slice_buffer.count); |
| 119 |
ok = GRPC_SLICE_LENGTH(a) == GRPC_SLICE_LENGTH(b) && |
| 120 |
0 == memcmp(GRPC_SLICE_START_PTR(a), GRPC_SLICE_START_PTR(b), |
| 121 |
GRPC_SLICE_LENGTH(a)); |
| 122 |
grpc_slice_unref(a); |
| 123 |
grpc_slice_unref(b); |
| 124 |
return ok; |
| 125 |
} |
| 126 |
|
| 127 |
int byte_buffer_eq_slice(grpc_byte_buffer* bb, grpc_slice b) { |
| 128 |
if (bb == nullptr) return 0; |
| 129 |
if (bb->data.raw.compression > GRPC_COMPRESS_NONE) { |
| 130 |
grpc_slice_buffer decompressed_buffer; |
| 131 |
grpc_slice_buffer_init(&decompressed_buffer); |
| 132 |
GPR_ASSERT(grpc_msg_decompress(bb->data.raw.compression, |
| 133 |
&bb->data.raw.slice_buffer, |
| 134 |
&decompressed_buffer)); |
| 135 |
grpc_byte_buffer* rbb = grpc_raw_byte_buffer_create( |
| 136 |
decompressed_buffer.slices, decompressed_buffer.count); |
| 137 |
int ret_val = raw_byte_buffer_eq_slice(rbb, b); |
| 138 |
grpc_byte_buffer_destroy(rbb); |
| 139 |
grpc_slice_buffer_destroy(&decompressed_buffer); |
| 140 |
return ret_val; |
| 141 |
} |
| 142 |
return raw_byte_buffer_eq_slice(bb, b); |
| 143 |
} |
| 144 |
|
| 145 |
int byte_buffer_eq_string(grpc_byte_buffer* bb, const char* str) { |
| 146 |
return byte_buffer_eq_slice(bb, grpc_slice_from_copied_string(str)); |
| 147 |
} |
| 148 |
|
| 149 |
namespace { |
| 150 |
bool IsProbablyInteger(void* p) { |
| 151 |
return reinterpret_cast<uintptr_t>(p) < 1000000; |
| 152 |
} |
| 153 |
|
| 154 |
std::string TagStr(void* tag) { |
| 155 |
if (IsProbablyInteger(tag)) { |
| 156 |
return absl::StrFormat("tag(%" PRIdPTR ")", |
| 157 |
reinterpret_cast<intptr_t>(tag)); |
| 158 |
} else { |
| 159 |
return absl::StrFormat("%p", tag); |
| 160 |
} |
| 161 |
} |
| 162 |
} // namespace |
| 163 |
|
| 164 |
namespace grpc_core { |
| 165 |
|
| 166 |
CqVerifier::CqVerifier(grpc_completion_queue* cq) : cq_(cq) {} |
| 167 |
|
| 168 |
CqVerifier::~CqVerifier() { Verify(); } |
| 169 |
|
| 170 |
std::string CqVerifier::Expectation::ToString() const { |
| 171 |
return absl::StrCat( |
| 172 |
location.file(), ":", location.line(), ": ", TagStr(tag), " ", |
| 173 |
Match( |
| 174 |
result, |
| 175 |
[](bool success) { |
| 176 |
return absl::StrCat("success=", success ? "true" : "false"); |
| 177 |
}, |
| 178 |
[](Maybe) { return std::string("maybe"); }, |
| 179 |
[](AnyStatus) { return std::string("any success value"); })); |
| 180 |
} |
| 181 |
|
| 182 |
std::string CqVerifier::ToString() const { |
| 183 |
std::vector<std::string> expectations; |
| 184 |
expectations.reserve(expectations_.size()); |
| 185 |
for (const auto& e : expectations_) { |
| 186 |
expectations.push_back(e.ToString()); |
| 187 |
} |
| 188 |
return absl::StrJoin(expectations, "\n"); |
| 189 |
} |
| 190 |
|
| 191 |
void CqVerifier::FailNoEventReceived(const SourceLocation& location) const { |
| 192 |
gpr_log(GPR_ERROR, "[%s:%d] no event received, but expected:%s", |
| 193 |
location.file(), location.line(), ToString().c_str()); |
| 194 |
abort(); |
| 195 |
} |
| 196 |
|
| 197 |
void CqVerifier::FailUnexpectedEvent(grpc_event* ev, |
| 198 |
const SourceLocation& location) const { |
| 199 |
gpr_log(GPR_ERROR, "[%s:%d] cq returned unexpected event: %s", |
| 200 |
location.file(), location.line(), grpc_event_string(ev).c_str()); |
| 201 |
gpr_log(GPR_ERROR, "expected tags:\n%s", ToString().c_str()); |
| 202 |
abort(); |
| 203 |
} |
| 204 |
|
| 205 |
void CqVerifier::Verify(Duration timeout, SourceLocation location) { |
| 206 |
const gpr_timespec deadline = |
| 207 |
grpc_timeout_milliseconds_to_deadline(timeout.millis()); |
| 208 |
while (!expectations_.empty()) { |
| 209 |
grpc_event ev = grpc_completion_queue_next(cq_, deadline, nullptr); |
| 210 |
if (ev.type == GRPC_QUEUE_TIMEOUT) break; |
| 211 |
if (ev.type != GRPC_OP_COMPLETE) { |
| 212 |
FailUnexpectedEvent(&ev, location); |
| 213 |
} |
| 214 |
bool found = false; |
| 215 |
for (auto it = expectations_.begin(); it != expectations_.end(); ++it) { |
| 216 |
if (it->tag != ev.tag) continue; |
| 217 |
const bool expected = Match( |
| 218 |
it->result, [ev](bool success) { return ev.success == success; }, |
| 219 |
[ev](Maybe m) { |
| 220 |
if (m.seen != nullptr) *m.seen = true; |
| 221 |
return ev.success != 0; |
| 222 |
}, |
| 223 |
[ev](AnyStatus a) { |
| 224 |
if (a.result != nullptr) *a.result = ev.success; |
| 225 |
return true; |
| 226 |
}); |
| 227 |
if (!expected) { |
| 228 |
FailUnexpectedEvent(&ev, location); |
| 229 |
} |
| 230 |
expectations_.erase(it); |
| 231 |
found = true; |
| 232 |
break; |
| 233 |
} |
| 234 |
if (!found) FailUnexpectedEvent(&ev, location); |
| 235 |
if (AllMaybes()) break; |
| 236 |
} |
| 237 |
expectations_.erase( |
| 238 |
std::remove_if(expectations_.begin(), expectations_.end(), |
| 239 |
[](const Expectation& e) { |
| 240 |
return absl::holds_alternative<Maybe>(e.result); |
| 241 |
}), |
| 242 |
expectations_.end()); |
| 243 |
if (!expectations_.empty()) FailNoEventReceived(location); |
| 244 |
} |
| 245 |
|
| 246 |
bool CqVerifier::AllMaybes() const { |
| 247 |
for (const auto& e : expectations_) { |
| 248 |
if (!absl::holds_alternative<Maybe>(e.result)) return false; |
| 249 |
} |
| 250 |
return true; |
| 251 |
} |
| 252 |
|
| 253 |
void CqVerifier::VerifyEmpty(Duration timeout, SourceLocation location) { |
| 254 |
const gpr_timespec deadline = |
| 255 |
gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), timeout.as_timespec()); |
| 256 |
GPR_ASSERT(expectations_.empty()); |
| 257 |
grpc_event ev = grpc_completion_queue_next(cq_, deadline, nullptr); |
| 258 |
if (ev.type != GRPC_QUEUE_TIMEOUT) { |
| 259 |
FailUnexpectedEvent(&ev, location); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
void CqVerifier::Expect(void* tag, ExpectedResult result, |
| 264 |
SourceLocation location) { |
| 265 |
expectations_.push_back(Expectation{location, tag, result}); |
| 266 |
} |
| 267 |
|
| 268 |
} // namespace grpc_core |