[fixit] Add source location to CqVerifier output (#30702)

* [fixit] Add source location to CqVerifier output

Supersedes #30701

* Automated change: Fix sanity tests

Co-authored-by: drfloob <drfloob@users.noreply.github.com>
pull/30411/head
AJ Heller 3 years ago committed by GitHub
parent a9d4bc4cf9
commit 4c2aa29b13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 26
      test/core/end2end/cq_verifier.cc
  2. 11
      test/core/end2end/cq_verifier.h

@ -187,26 +187,28 @@ std::string CqVerifier::ToString() const {
return absl::StrJoin(expectations, "\n"); return absl::StrJoin(expectations, "\n");
} }
void CqVerifier::FailNoEventReceived() const { void CqVerifier::FailNoEventReceived(const SourceLocation& location) const {
gpr_log(GPR_ERROR, "no event received, but expected:%s", ToString().c_str()); gpr_log(GPR_ERROR, "[%s:%d] no event received, but expected:%s",
location.file(), location.line(), ToString().c_str());
abort(); abort();
} }
void CqVerifier::FailUnexpectedEvent(grpc_event* ev) const { void CqVerifier::FailUnexpectedEvent(grpc_event* ev,
gpr_log(GPR_ERROR, "cq returned unexpected event: %s", const SourceLocation& location) const {
grpc_event_string(ev).c_str()); gpr_log(GPR_ERROR, "[%s:%d] cq returned unexpected event: %s",
location.file(), location.line(), grpc_event_string(ev).c_str());
gpr_log(GPR_ERROR, "expected tags:\n%s", ToString().c_str()); gpr_log(GPR_ERROR, "expected tags:\n%s", ToString().c_str());
abort(); abort();
} }
void CqVerifier::Verify(Duration timeout) { void CqVerifier::Verify(Duration timeout, SourceLocation location) {
const gpr_timespec deadline = const gpr_timespec deadline =
grpc_timeout_milliseconds_to_deadline(timeout.millis()); grpc_timeout_milliseconds_to_deadline(timeout.millis());
while (!expectations_.empty()) { while (!expectations_.empty()) {
grpc_event ev = grpc_completion_queue_next(cq_, deadline, nullptr); grpc_event ev = grpc_completion_queue_next(cq_, deadline, nullptr);
if (ev.type == GRPC_QUEUE_TIMEOUT) break; if (ev.type == GRPC_QUEUE_TIMEOUT) break;
if (ev.type != GRPC_OP_COMPLETE) { if (ev.type != GRPC_OP_COMPLETE) {
FailUnexpectedEvent(&ev); FailUnexpectedEvent(&ev, location);
} }
bool found = false; bool found = false;
for (auto it = expectations_.begin(); it != expectations_.end(); ++it) { for (auto it = expectations_.begin(); it != expectations_.end(); ++it) {
@ -222,13 +224,13 @@ void CqVerifier::Verify(Duration timeout) {
return true; return true;
}); });
if (!expected) { if (!expected) {
FailUnexpectedEvent(&ev); FailUnexpectedEvent(&ev, location);
} }
expectations_.erase(it); expectations_.erase(it);
found = true; found = true;
break; break;
} }
if (!found) FailUnexpectedEvent(&ev); if (!found) FailUnexpectedEvent(&ev, location);
if (AllMaybes()) break; if (AllMaybes()) break;
} }
expectations_.erase( expectations_.erase(
@ -237,7 +239,7 @@ void CqVerifier::Verify(Duration timeout) {
return absl::holds_alternative<Maybe>(e.result); return absl::holds_alternative<Maybe>(e.result);
}), }),
expectations_.end()); expectations_.end());
if (!expectations_.empty()) FailNoEventReceived(); if (!expectations_.empty()) FailNoEventReceived(location);
} }
bool CqVerifier::AllMaybes() const { bool CqVerifier::AllMaybes() const {
@ -247,13 +249,13 @@ bool CqVerifier::AllMaybes() const {
return true; return true;
} }
void CqVerifier::VerifyEmpty(Duration timeout) { void CqVerifier::VerifyEmpty(Duration timeout, SourceLocation location) {
const gpr_timespec deadline = const gpr_timespec deadline =
gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), timeout.as_timespec()); gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), timeout.as_timespec());
GPR_ASSERT(expectations_.empty()); GPR_ASSERT(expectations_.empty());
grpc_event ev = grpc_completion_queue_next(cq_, deadline, nullptr); grpc_event ev = grpc_completion_queue_next(cq_, deadline, nullptr);
if (ev.type != GRPC_QUEUE_TIMEOUT) { if (ev.type != GRPC_QUEUE_TIMEOUT) {
FailUnexpectedEvent(&ev); FailUnexpectedEvent(&ev, location);
} }
} }

@ -57,10 +57,12 @@ class CqVerifier {
// Ensure all expected events (and only those events) are present on the // Ensure all expected events (and only those events) are present on the
// bound completion queue within \a timeout. // bound completion queue within \a timeout.
void Verify(Duration timeout = Duration::Seconds(10)); void Verify(Duration timeout = Duration::Seconds(10),
SourceLocation location = SourceLocation());
// Ensure that the completion queue is empty, waiting up to \a timeout. // Ensure that the completion queue is empty, waiting up to \a timeout.
void VerifyEmpty(Duration timeout = Duration::Seconds(1)); void VerifyEmpty(Duration timeout = Duration::Seconds(1),
SourceLocation location = SourceLocation());
// Match an expectation about a status. // Match an expectation about a status.
// location must be DEBUG_LOCATION. // location must be DEBUG_LOCATION.
@ -80,8 +82,9 @@ class CqVerifier {
std::string ToString() const; std::string ToString() const;
}; };
void FailNoEventReceived() const; void FailNoEventReceived(const SourceLocation& location) const;
void FailUnexpectedEvent(grpc_event* ev) const; void FailUnexpectedEvent(grpc_event* ev,
const SourceLocation& location) const;
bool AllMaybes() const; bool AllMaybes() const;
grpc_completion_queue* const cq_; grpc_completion_queue* const cq_;

Loading…
Cancel
Save