pw_rpc: new FakeChannelOutput testing APIs
- Add an individual count for each packet type.
- Avoid crashing when the channel output is done.
- Add methods to inject an error status useful when testing RPC error
handling.
No-Docs-Update-Reason: documented in API comments.
Change-Id: I9df303cef3fe6c069fff7350c0dc0e909a326404
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/61906
Pigweed-Auto-Submit: Carlos Chinchilla <cachinchilla@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Reviewed-by: Wyatt Hepler <hepler@google.com>
diff --git a/pw_rpc/fake_channel_output.cc b/pw_rpc/fake_channel_output.cc
index 0fc56dc..7c0a9e5 100644
--- a/pw_rpc/fake_channel_output.cc
+++ b/pw_rpc/fake_channel_output.cc
@@ -22,9 +22,11 @@
void FakeChannelOutput::clear() {
ClearResponses();
- total_responses_ = 0;
+ total_response_packets_ = 0;
+ total_stream_packets_ = 0;
last_status_ = Status::Unknown();
- done_ = false;
+ send_status_ = OkStatus();
+ return_after_packet_count_ = -1;
}
Status FakeChannelOutput::SendAndReleaseBuffer(
@@ -36,7 +38,15 @@
return OkStatus();
}
- PW_CHECK(!done_);
+ if (return_after_packet_count_ == 0) {
+ return send_status_;
+ }
+ if (return_after_packet_count_ > 0 &&
+ return_after_packet_count_ == static_cast<int>(total_responses())) {
+ // Disable behavior.
+ return_after_packet_count_ = -1;
+ return send_status_;
+ }
Result<Packet> result = Packet::FromBuffer(buffer);
PW_CHECK_OK(result.status());
@@ -49,12 +59,13 @@
if (!HasServerStream(method_type_)) {
ProcessResponse(result.value().payload());
}
- done_ = true;
+ ++total_response_packets_;
break;
case PacketType::SERVER_ERROR:
PW_CRASH("Server error: %s", result.value().status().str());
case PacketType::SERVER_STREAM:
ProcessResponse(result.value().payload());
+ ++total_stream_packets_;
break;
default:
PW_CRASH("Unhandled PacketType %d",