RPC: bounded streaming with credit, half-close and cancellation (S-RPC-04)
This commit is contained in:
1 parent
103523f04a
commit
310fea8d7b
5 files changed
+721
-1
No files matched your search
@@ -0,0 +1,195 @@
|
||||
#include <Void/RPC/Stream.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace Void::RPC;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
OwnedBytes bytes_of(std::string_view text)
|
||||
{
|
||||
OwnedBytes out;
|
||||
out.reserve(text.size());
|
||||
for (const char ch : text) {
|
||||
out.push_back(static_cast<std::byte>(ch));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string text_of(std::span<const std::byte> bytes)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(bytes.size());
|
||||
for (const std::byte b : bytes) {
|
||||
out.push_back(static_cast<char>(std::to_integer<unsigned int>(b)));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string read_all(StreamEndpoint& endpoint, std::size_t expected)
|
||||
{
|
||||
std::string out;
|
||||
while (out.size() < expected) {
|
||||
auto chunk = endpoint.read();
|
||||
Void::Testing::require(chunk.has_value());
|
||||
if (chunk->empty()) {
|
||||
break;
|
||||
}
|
||||
out += text_of(*chunk);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test rpc_stream_round_trip_and_credit_bounds()
|
||||
{
|
||||
// Credit must cover the whole message for a single synchronous write;
|
||||
// the frame cap still splits reads into small chunks.
|
||||
auto pair = make_stream_pair(
|
||||
StreamLimits{.initial_credit_bytes = 16, .max_data_frame_bytes = 4});
|
||||
|
||||
const auto message = bytes_of("hello stream");
|
||||
Void::Testing::require(pair.client.write(message).has_value());
|
||||
Void::Testing::expect_eq(read_all(pair.server, message.size()),
|
||||
std::string{"hello stream"});
|
||||
Void::Testing::expect_eq(pair.client.credit_available(), std::size_t{16});
|
||||
Void::Testing::expect(pair.server.peak_buffered_bytes() <= 16);
|
||||
|
||||
// Reverse direction is independent.
|
||||
const auto reply = bytes_of("reply");
|
||||
Void::Testing::require(pair.server.write(reply).has_value());
|
||||
Void::Testing::expect_eq(read_all(pair.client, reply.size()),
|
||||
std::string{"reply"});
|
||||
}
|
||||
|
||||
Void::Testing::Test rpc_stream_half_close_is_directional()
|
||||
{
|
||||
auto pair = make_stream_pair(
|
||||
StreamLimits{.initial_credit_bytes = 16, .max_data_frame_bytes = 16});
|
||||
|
||||
Void::Testing::require(pair.client.write(bytes_of("tail")).has_value());
|
||||
pair.client.half_close();
|
||||
Void::Testing::expect(pair.client.local_half_closed());
|
||||
Void::Testing::expect(pair.client.state() == StreamState::HalfClosedLocal);
|
||||
|
||||
// The peer drains buffered data, then observes EOF.
|
||||
Void::Testing::expect_eq(read_all(pair.server, 4), std::string{"tail"});
|
||||
auto eof = pair.server.read();
|
||||
Void::Testing::require(eof.has_value());
|
||||
Void::Testing::expect(eof->empty());
|
||||
Void::Testing::expect(pair.server.eof());
|
||||
|
||||
// The server may still write in the other direction.
|
||||
Void::Testing::require(pair.server.write(bytes_of("after")).has_value());
|
||||
Void::Testing::expect_eq(read_all(pair.client, 5), std::string{"after"});
|
||||
|
||||
// Writing on a half-closed side is rejected.
|
||||
auto rejected = pair.client.write(bytes_of("x"));
|
||||
Void::Testing::require(!rejected.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
rejected.error().code().value,
|
||||
static_cast<std::int32_t>(RpcStatusCode::FailedPrecondition));
|
||||
}
|
||||
|
||||
Void::Testing::Test rpc_stream_cancel_aborts_both_sides()
|
||||
{
|
||||
auto pair = make_stream_pair();
|
||||
pair.client.cancel();
|
||||
Void::Testing::expect(pair.client.cancelled());
|
||||
Void::Testing::expect(pair.client.state() == StreamState::Cancelled);
|
||||
|
||||
auto read = pair.server.read();
|
||||
Void::Testing::require(!read.has_value());
|
||||
Void::Testing::expect_eq(read.error().code().value,
|
||||
static_cast<std::int32_t>(RpcStatusCode::Cancelled));
|
||||
auto write = pair.server.write(bytes_of("x"));
|
||||
Void::Testing::require(!write.has_value());
|
||||
auto terminal = pair.server.wait_terminal();
|
||||
Void::Testing::require(!terminal.has_value());
|
||||
|
||||
// Idempotent, and a terminal outcome can no longer be committed.
|
||||
pair.server.cancel();
|
||||
Void::Testing::require(!pair.server.finish(RpcStatus{}).has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test rpc_stream_terminal_outcome_is_single()
|
||||
{
|
||||
auto pair = make_stream_pair(
|
||||
StreamLimits{.initial_credit_bytes = 16, .max_data_frame_bytes = 16});
|
||||
Void::Testing::require(pair.server.write(bytes_of("body")).has_value());
|
||||
Void::Testing::require(
|
||||
pair.server
|
||||
.finish(RpcStatus{.code = RpcStatusCode::Ok, .message = "done"},
|
||||
bytes_of("trailer"))
|
||||
.has_value());
|
||||
|
||||
Void::Testing::expect_eq(read_all(pair.client, 4), std::string{"body"});
|
||||
// The terminal payload is delivered by wait_terminal, not the data stream.
|
||||
auto eof = pair.client.read();
|
||||
Void::Testing::require(eof.has_value());
|
||||
Void::Testing::expect(eof->empty());
|
||||
|
||||
auto terminal = pair.client.wait_terminal();
|
||||
Void::Testing::require(terminal.has_value());
|
||||
Void::Testing::expect(terminal->status.code == RpcStatusCode::Ok);
|
||||
Void::Testing::expect_eq(terminal->status.message, std::string{"done"});
|
||||
Void::Testing::expect_eq(text_of(terminal->payload), std::string{"trailer"});
|
||||
|
||||
// A second terminal is rejected.
|
||||
Void::Testing::require(!pair.server.finish(RpcStatus{}).has_value());
|
||||
|
||||
// finish is server-side only.
|
||||
auto client_finish = pair.client.finish(RpcStatus{});
|
||||
Void::Testing::require(!client_finish.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test rpc_stream_stalled_consumer_is_bounded()
|
||||
{
|
||||
constexpr std::size_t kCredit = 16;
|
||||
constexpr std::size_t kTotal = 64;
|
||||
auto pair = make_stream_pair(
|
||||
StreamLimits{.initial_credit_bytes = kCredit, .max_data_frame_bytes = 16});
|
||||
|
||||
const std::vector<std::byte> payload(kTotal, std::byte{0x5A});
|
||||
std::atomic<bool> done{false};
|
||||
std::atomic<bool> write_ok{false};
|
||||
std::thread writer([&] {
|
||||
const auto result = pair.client.write(payload);
|
||||
write_ok.store(result.has_value(), std::memory_order_relaxed);
|
||||
// Signal EOF so the draining reader cannot block after the last
|
||||
// chunk when the writer has already returned.
|
||||
pair.client.half_close();
|
||||
done.store(true, std::memory_order_release);
|
||||
});
|
||||
|
||||
// The consumer stalls: the writer can only buffer one credit window.
|
||||
std::this_thread::sleep_for(50ms);
|
||||
Void::Testing::expect(!done.load(std::memory_order_acquire));
|
||||
Void::Testing::expect(pair.server.buffered_bytes() <= kCredit);
|
||||
Void::Testing::expect(pair.server.peak_buffered_bytes() <= kCredit);
|
||||
Void::Testing::expect_eq(pair.client.credit_available(), std::size_t{0});
|
||||
|
||||
// Draining replenishes credit and lets the writer finish.
|
||||
std::size_t received = 0;
|
||||
while (!done.load(std::memory_order_acquire) || pair.server.buffered_bytes() > 0) {
|
||||
auto chunk = pair.server.read();
|
||||
Void::Testing::require(chunk.has_value());
|
||||
if (chunk->empty()) {
|
||||
break;
|
||||
}
|
||||
received += chunk->size();
|
||||
}
|
||||
writer.join();
|
||||
Void::Testing::expect(write_ok.load());
|
||||
Void::Testing::expect_eq(received, kTotal);
|
||||
Void::Testing::expect(pair.server.peak_buffered_bytes() <= kCredit);
|
||||
}
|
||||
Reference in new issue
Block a user