| 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 |
#ifndef GRPC_TEST_CORE_END2END_CQ_VERIFIER_H |
| 20 |
#define GRPC_TEST_CORE_END2END_CQ_VERIFIER_H |
| 21 |
|
| 22 |
#include <string> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include "absl/types/variant.h" |
| 26 |
|
| 27 |
#include <grpc/grpc.h> |
| 28 |
#include <grpc/slice.h> |
| 29 |
|
| 30 |
#include "src/core/lib/gprpp/debug_location.h" |
| 31 |
#include "src/core/lib/gprpp/time.h" |
| 32 |
|
| 33 |
namespace grpc_core { |
| 34 |
|
| 35 |
// A CqVerifier can verify that expected events arrive in a timely fashion |
| 36 |
// on a single completion queue |
| 37 |
class CqVerifier { |
| 38 |
public: |
| 39 |
// ExpectedResult - if the tag is received, set *seen to true (if seen is |
| 40 |
// non-null). |
| 41 |
struct Maybe { |
| 42 |
bool* seen = nullptr; |
| 43 |
}; |
| 44 |
// ExpectedResult - expect the tag, but its result may be true or false. |
| 45 |
// Store the result in result (if result is non-null). |
| 46 |
struct AnyStatus { |
| 47 |
bool* result = nullptr; |
| 48 |
}; |
| 49 |
|
| 50 |
using ExpectedResult = absl::variant<bool, Maybe, AnyStatus>; |
| 51 |
|
| 52 |
explicit CqVerifier(grpc_completion_queue* cq); |
| 53 |
~CqVerifier(); |
| 54 |
|
| 55 |
CqVerifier(const CqVerifier&) = delete; |
| 56 |
CqVerifier& operator=(const CqVerifier&) = delete; |
| 57 |
|
| 58 |
// Ensure all expected events (and only those events) are present on the |
| 59 |
// bound completion queue within \a timeout. |
| 60 |
void Verify(Duration timeout = Duration::Seconds(10), |
| 61 |
SourceLocation location = SourceLocation()); |
| 62 |
|
| 63 |
// Ensure that the completion queue is empty, waiting up to \a timeout. |
| 64 |
void VerifyEmpty(Duration timeout = Duration::Seconds(1), |
| 65 |
SourceLocation location = SourceLocation()); |
| 66 |
|
| 67 |
// Match an expectation about a status. |
| 68 |
// location must be DEBUG_LOCATION. |
| 69 |
// result can be any of the types in ExpectedResult - a plain bool means |
| 70 |
// 'expect success to be true/false'. |
| 71 |
void Expect(void* tag, ExpectedResult result, |
| 72 |
SourceLocation location = SourceLocation()); |
| 73 |
|
| 74 |
std::string ToString() const; |
| 75 |
|
| 76 |
private: |
| 77 |
struct Expectation { |
| 78 |
SourceLocation location; |
| 79 |
void* tag; |
| 80 |
ExpectedResult result; |
| 81 |
|
| 82 |
std::string ToString() const; |
| 83 |
}; |
| 84 |
|
| 85 |
void FailNoEventReceived(const SourceLocation& location) const; |
| 86 |
void FailUnexpectedEvent(grpc_event* ev, |
| 87 |
const SourceLocation& location) const; |
| 88 |
bool AllMaybes() const; |
| 89 |
|
| 90 |
grpc_completion_queue* const cq_; |
| 91 |
std::vector<Expectation> expectations_; |
| 92 |
}; |
| 93 |
|
| 94 |
} // namespace grpc_core |
| 95 |
|
| 96 |
int byte_buffer_eq_slice(grpc_byte_buffer* bb, grpc_slice b); |
| 97 |
int byte_buffer_eq_string(grpc_byte_buffer* bb, const char* str); |
| 98 |
int contains_metadata(grpc_metadata_array* array, const char* key, |
| 99 |
const char* value); |
| 100 |
int contains_metadata_slices(grpc_metadata_array* array, grpc_slice key, |
| 101 |
grpc_slice value); |
| 102 |
|
| 103 |
#endif /* GRPC_TEST_CORE_END2END_CQ_VERIFIER_H */ |