From b6ccea4ab31c33e73aa8d604ea5c2c5a81b798b1 Mon Sep 17 00:00:00 2001 From: Alexey Ozeritskiy Date: Sun, 15 Nov 2026 20:53:41 +0000 Subject: [PATCH] Add ReadBuffer(): reads into buffers of the poller, on every backend --- coroio/CMakeLists.txt | 1 + coroio/arena.hpp | 95 ++++++++---- coroio/poller.hpp | 14 ++ coroio/read_buffer.cpp | 41 ++++++ coroio/read_buffer.hpp | 207 +++++++++++++++++++++++++++ coroio/socket.hpp | 53 +++++++ tests/tests.cpp | 317 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 702 insertions(+), 26 deletions(-) create mode 100644 coroio/read_buffer.cpp create mode 100644 coroio/read_buffer.hpp diff --git a/coroio/CMakeLists.txt b/coroio/CMakeLists.txt index 19faf7a..9cba216 100644 --- a/coroio/CMakeLists.txt +++ b/coroio/CMakeLists.txt @@ -7,6 +7,7 @@ set(SOURCES address.cpp init.cpp notifier.cpp + read_buffer.cpp socket.cpp sockutils.cpp ssl.cpp diff --git a/coroio/arena.hpp b/coroio/arena.hpp index d9957f8..7bbbf9e 100644 --- a/coroio/arena.hpp +++ b/coroio/arena.hpp @@ -1,46 +1,46 @@ #pragma once +#include +#include +#include #include #include namespace NNet { /** - * @class TArenaAllocator - * @brief Arena allocator for objects of one type that are often created and destroyed. - * - * This class preallocates memory in pools of @c PoolSize objects and reuses the freed slots, - * so that each object does not need its own heap allocation. It hands out raw storage: - * construct the object in it with placement new and destroy the object before giving the - * storage back. + * @class TArena + * @brief Slots of one size, preallocated in pools, with a free list. * - * @tparam T The type of object to allocate. - * @tparam PoolSize The number of objects in each preallocated pool (default is 1024). - * - * @code{.cpp} - * // Example usage: - * TArenaAllocator allocator; - * TItem* item = new (allocator.Allocate()) TItem(); - * // use item... - * item->~TItem(); - * allocator.Deallocate(item); - * @endcode + * The size of a slot and the number of slots of a pool are set at construction. + * An arena of one pool (@p maxPools = 1) is one contiguous region, whose slots are + * also addressed by index; Allocate() returns nullptr when all of them are in use. */ -template -class TArenaAllocator { +class TArena { public: - TArenaAllocator() { + TArena(size_t slotSize, size_t alignment, size_t poolSize, size_t maxPools = std::numeric_limits::max()) + : SlotSize_(slotSize) + , Alignment_(alignment) + , PoolSize_(poolSize) + , MaxPools_(maxPools) + { AllocatePool(); } - ~TArenaAllocator() { + TArena(const TArena&) = delete; + TArena& operator=(const TArena&) = delete; + + ~TArena() { for (auto block : Pools_) { - ::operator delete(block, std::align_val_t(alignof(T))); + ::operator delete(block, std::align_val_t(Alignment_)); } } void* Allocate() { if (FreePages_.empty()) { + if (Pools_.size() == MaxPools_) { + return nullptr; + } AllocatePool(); } ++AllocatedObjects_; @@ -58,18 +58,61 @@ class TArenaAllocator { return AllocatedObjects_; } + /// Slot @p index of the first pool. + char* Slot(size_t index) const { + return Pools_.front() + index * SlotSize_; + } + + /// Index of @p slot, a slot of the first pool. + size_t Index(const void* slot) const { + return static_cast(static_cast(slot) - Pools_.front()) / SlotSize_; + } + private: void AllocatePool() { - char* pool = static_cast(::operator new(PoolSize * sizeof(T), std::align_val_t(alignof(T)))); + char* pool = static_cast(::operator new(PoolSize_ * SlotSize_, std::align_val_t(Alignment_))); Pools_.emplace_back(pool); - for (size_t i = 0; i < PoolSize; i++) { - FreePages_.push(&pool[i * sizeof(T)]); + for (size_t i = 0; i < PoolSize_; i++) { + FreePages_.push(&pool[i * SlotSize_]); } } + size_t SlotSize_; + size_t Alignment_; + size_t PoolSize_; + size_t MaxPools_; std::vector Pools_; std::stack> FreePages_; int AllocatedObjects_ = 0; }; +/** + * @class TArenaAllocator + * @brief Arena allocator for objects of one type that are often created and destroyed. + * + * This class preallocates memory in pools of @c PoolSize objects and reuses the freed slots, + * so that each object does not need its own heap allocation. It hands out raw storage: + * construct the object in it with placement new and destroy the object before giving the + * storage back. + * + * @tparam T The type of object to allocate. + * @tparam PoolSize The number of objects in each preallocated pool (default is 1024). + * + * @code{.cpp} + * // Example usage: + * TArenaAllocator allocator; + * TItem* item = new (allocator.Allocate()) TItem(); + * // use item... + * item->~TItem(); + * allocator.Deallocate(item); + * @endcode + */ +template +class TArenaAllocator: public TArena { +public: + TArenaAllocator() + : TArena(sizeof(T), alignof(T), PoolSize) + { } +}; + } // namespace NNet diff --git a/coroio/poller.hpp b/coroio/poller.hpp index 6b37d6c..a9ffe7e 100644 --- a/coroio/poller.hpp +++ b/coroio/poller.hpp @@ -10,6 +10,7 @@ #include #include "base.hpp" +#include "read_buffer.hpp" #ifdef Yield #undef Yield @@ -358,6 +359,18 @@ class TPollerBase { auto TimersSize() const { return Timers_.size(); } + /** + * @brief Sets the buffers of ReadBuffer() on this poller's handles. + * + * @throws std::logic_error after the first ReadBuffer(), std::invalid_argument for a bad config. + */ + void ConfigureReadBuffers(const TReadBufferConfig& config) { + Buffers_.Configure(config); + } + /// The buffers of ReadBuffer(), used by the handles. + NDetail::TBufferPool& ReadBuffers() { + return Buffers_; + } protected: /** @@ -469,6 +482,7 @@ class TPollerBase { unsigned LastFiredTimer_ = (unsigned)(-1); ///< ID of the last fired timer. std::chrono::milliseconds MaxDuration_ = std::chrono::milliseconds(100); ///< Maximum poll duration. timespec MaxDurationTs_ = GetMaxDuration(MaxDuration_); ///< Max duration represented as timespec. + NDetail::TBufferPool Buffers_; ///< Buffers of ReadBuffer(). private: void QueueRemoval(int fd, int close) { diff --git a/coroio/read_buffer.cpp b/coroio/read_buffer.cpp new file mode 100644 index 0000000..51e3474 --- /dev/null +++ b/coroio/read_buffer.cpp @@ -0,0 +1,41 @@ +#include "read_buffer.hpp" + +#include +#include + +namespace NNet::NDetail { + +void TBufferPool::Configure(const TReadBufferConfig& config) { + if (Arena_) { + throw std::logic_error("read buffers are configured after their first use"); + } + // Buffer ids are 16-bit, and a buffer ring takes a power of two of at most 32768 entries. + if (config.BufferSize == 0 || config.PerSocketLimit == 0 + || config.Count == 0 || config.Count > 32768 || (config.Count & (config.Count - 1))) + { + throw std::invalid_argument("bad read buffer config"); + } + Config_ = config; +} + +void TBufferPool::Allocate() { + if (!Arena_) { + // One pool, so that a buffer has an id. Its pages are touched only when used. + Arena_.emplace(Config_.BufferSize, 64, Config_.Count, 1); + Thread_ = std::this_thread::get_id(); + } +} + +std::span TBufferPool::Acquire(TReadBuffer& lease) { + assert(!Owner_); // the owner's system picks the buffers + Allocate(); + void* buffer = Arena_->Allocate(); + if (!buffer) { + throw std::system_error(std::make_error_code(std::errc::no_buffer_space), "read buffers are all leased"); + } + lease = TReadBuffer{}; + Lend(lease, static_cast(Arena_->Index(buffer))); + return {lease.Ptr_, Config_.BufferSize}; +} + +} // namespace NNet::NDetail diff --git a/coroio/read_buffer.hpp b/coroio/read_buffer.hpp new file mode 100644 index 0000000..6088ea1 --- /dev/null +++ b/coroio/read_buffer.hpp @@ -0,0 +1,207 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "arena.hpp" + +namespace NNet { + +/** + * @brief Buffers of the ReadBuffer() reads of one poller. + * + * Set with TPollerBase::ConfigureReadBuffers() before the first ReadBuffer(). + * The memory, Count * BufferSize bytes, is allocated at the first ReadBuffer(). + */ +struct TReadBufferConfig { + uint32_t BufferSize = 16 * 1024; ///< Bytes of one buffer: a read gets at most this many. + uint32_t Count = 1024; ///< Buffers of the poller, a power of two up to 32768. + uint32_t PerSocketLimit = 8; ///< Leases a poller that reads ahead (TUring) queues on one descriptor. +}; + +namespace NDetail { +class TBufferPool; +} // namespace NDetail + +/** + * @brief Lease of a poller buffer that holds the bytes of one read. + * + * Returned by `co_await handle.ReadBuffer()`. Empty, i.e. Data() is empty, at + * the end of the stream. The destructor gives the buffer back to the poller. + * A lease must be destroyed on the poller's thread before the poller, and it + * holds one of TReadBufferConfig::Count buffers: do not keep many of them. + */ +class TReadBuffer { +public: + TReadBuffer() = default; + TReadBuffer(TReadBuffer&& other) noexcept; + TReadBuffer& operator=(TReadBuffer&& other) noexcept; + TReadBuffer(const TReadBuffer&) = delete; + TReadBuffer& operator=(const TReadBuffer&) = delete; + ~TReadBuffer(); + + /// The bytes not consumed yet. + std::span Data() const { + return {Ptr_ + Pos_, Size_ - Pos_}; + } + + /// Drops the first @p n bytes of Data(). + void Consume(size_t n) { + assert(n <= Size_ - Pos_); + Pos_ += static_cast(n); + } + +private: + friend class NDetail::TBufferPool; + + void Release(); + + NDetail::TBufferPool* Pool_ = nullptr; + char* Ptr_ = nullptr; + uint32_t Size_ = 0; + uint32_t Pos_ = 0; + uint16_t Id_ = 0; +}; + +namespace NDetail { + +/** + * @brief The buffers of TReadBuffer leases of one poller: one TArena of Count slots. + * + * As with the zero-copy readers, a backend Acquire()s a buffer, the system reads + * into it, and Commit() publishes the bytes read as a lease; the lease's destructor + * Release()s the buffer. A poller whose system picks the buffers (TUring's buffer + * ring, addressed by buffer id) sets an owner that takes them back instead of the + * free list. + */ +class TBufferPool { +public: + /// Takes back the buffers of a pool whose system picks them. + struct IOwner { + virtual void Release(uint16_t id) = 0; + + protected: + ~IOwner() = default; + }; + + TBufferPool() = default; + TBufferPool(const TBufferPool&) = delete; + TBufferPool& operator=(const TBufferPool&) = delete; + ~TBufferPool() { + assert(Leased_ == 0); // a lease outlives its poller + } + + /// Throws std::logic_error once the memory is allocated, std::invalid_argument for a bad config. + void Configure(const TReadBufferConfig& config); + + const TReadBufferConfig& Config() const { + return Config_; + } + + /// Allocates the memory, once. + void Allocate(); + + /// Start of buffer @p id. + char* Buffer(uint16_t id) const { + return Arena_->Slot(id); + } + + void SetOwner(IOwner* owner) { + Owner_ = owner; + } + + /** + * @brief Acquires a free buffer for @p lease, which owns it with no bytes yet. + * + * @return The buffer to read into. + * @throws std::system_error with std::errc::no_buffer_space if all buffers are leased. + */ + std::span Acquire(TReadBuffer& lease); + + /// Publishes the first @p size bytes read into the buffer of @p lease. With none, the buffer goes back. + void Commit(TReadBuffer& lease, size_t size) { + lease.Size_ = static_cast(size); + if (size == 0) { + lease.Release(); + } + } + + /// Publishes @p size bytes that the system read into buffer @p id as a lease. + TReadBuffer Commit(uint16_t id, size_t size) { + TReadBuffer lease; + Lend(lease, id); + lease.Size_ = static_cast(size); + return lease; + } + + /// Gives buffer @p id of a lease back: to the owner, or to the free list. + void Release(uint16_t id) { + assert(std::this_thread::get_id() == Thread_); // a lease stays on its poller's thread + assert(Leased_ > 0); + Leased_--; + if (Owner_) { + Owner_->Release(id); + } else { + Arena_->Deallocate(Buffer(id)); + } + } + +private: + void Lend(TReadBuffer& lease, uint16_t id) { + assert(std::this_thread::get_id() == Thread_); + Leased_++; + lease.Pool_ = this; + lease.Ptr_ = Buffer(id); + lease.Size_ = lease.Pos_ = 0; + lease.Id_ = id; + } + + TReadBufferConfig Config_; + std::optional Arena_; + IOwner* Owner_ = nullptr; + size_t Leased_ = 0; + std::thread::id Thread_; +}; + +} // namespace NDetail + +inline TReadBuffer::TReadBuffer(TReadBuffer&& other) noexcept + : Pool_(std::exchange(other.Pool_, nullptr)) + , Ptr_(std::exchange(other.Ptr_, nullptr)) + , Size_(std::exchange(other.Size_, 0)) + , Pos_(std::exchange(other.Pos_, 0)) + , Id_(other.Id_) +{ } + +inline TReadBuffer& TReadBuffer::operator=(TReadBuffer&& other) noexcept { + if (this != &other) { + Release(); + Pool_ = std::exchange(other.Pool_, nullptr); + Ptr_ = std::exchange(other.Ptr_, nullptr); + Size_ = std::exchange(other.Size_, 0); + Pos_ = std::exchange(other.Pos_, 0); + Id_ = other.Id_; + } + return *this; +} + +inline TReadBuffer::~TReadBuffer() { + Release(); +} + +inline void TReadBuffer::Release() { + if (Pool_) { + std::exchange(Pool_, nullptr)->Release(Id_); + Ptr_ = nullptr; + Size_ = Pos_ = 0; + } +} + +} // namespace NNet diff --git a/coroio/socket.hpp b/coroio/socket.hpp index 37d2a1f..ff06156 100644 --- a/coroio/socket.hpp +++ b/coroio/socket.hpp @@ -26,6 +26,34 @@ namespace NNet { template class TSocketBase; +namespace NDetail { + +// makeRead is called before the first suspension, and its awaitable keeps the descriptor, so the handle may move. +template +TFuture ReadIntoBuffer(TBufferPool& pool, TReadBuffer lease, TMakeRead makeRead) { + auto read = makeRead(); + for (;;) { + auto size = co_await read; + if (size >= 0) { + pool.Commit(lease, size); + co_return std::move(lease); + } + // A retry hint, e.g. EAGAIN after a spurious wake-up: wait again. + } +} + +/// ReadBuffer() of a handle that reads into the caller's memory: its ReadSome() into an acquired buffer. +template +TFuture ReadIntoBuffer(THandle& handle, TBufferPool& pool) { + TReadBuffer lease; + std::span buf = pool.Acquire(lease); + return ReadIntoBuffer(pool, std::move(lease), [&handle, buf] { + return handle.ReadSome(buf.data(), buf.size()); + }); +} + +} // namespace NDetail + /** * @class TSocketBase * @brief Internal base class encapsulating a socket (or file) descriptor and its poller. @@ -179,6 +207,21 @@ class TSocketBase: public TSocketBase { }; return TAwaitableRead{Poller_,Fd_,buf,size}; } + /** + * @brief Reads into a buffer of the poller. + * + * Reads as ReadSome() does, into a buffer of TPollerBase::ReadBuffers(), and + * waits again after a spurious wake-up. ReadSome() and ReadBuffer() of one + * descriptor return its bytes in order; use one reader at a time. + * + * @return An awaitable that yields the lease of the bytes read, or an empty lease + * at the end of the stream. + * @throws std::system_error on read errors, and with std::errc::no_buffer_space + * if all buffers of the poller are leased. + */ + TFuture ReadBuffer() { + return NDetail::ReadIntoBuffer(*this, Poller_->ReadBuffers()); + } /** * @brief Asynchronously writes data from the provided buffer to the socket. * @@ -848,6 +891,11 @@ class TPollerDrivenSocket: public TSocket return TAwaitable{Poller_, Fd_, buf, size}; } + /// Reads into a buffer of the poller, see TSocketBase::ReadBuffer(). + TFuture ReadBuffer() { + return NDetail::ReadIntoBuffer(*this, Poller_->ReadBuffers()); + } + /** * @brief Asynchronously writes data to the socket. * @@ -1010,6 +1058,11 @@ class TPollerDrivenFileHandle: public TFileHandle return TAwaitable{Poller_, Fd_, buf, size}; } + /// Reads into a buffer of the poller, see TSocketBase::ReadBuffer(). + TFuture ReadBuffer() { + return NDetail::ReadIntoBuffer(*this, Poller_->ReadBuffers()); + } + /** * @brief Asynchronously writes data from the provided buffer to the file. * diff --git a/tests/tests.cpp b/tests/tests.cpp index e2a28a3..1f50be7 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -1944,6 +1944,313 @@ void test_driven_accept_not_listening(void**) { assert_false(accepted.has_value()); } +namespace { + +#ifdef _WIN32 +constexpr int ConnReset = WSAECONNRESET; +#else +constexpr int ConnReset = ECONNRESET; +#endif + +template +constexpr bool kDrivenSocket = !std::is_same_v; + +template +TFuture ReadLease(TFile& file, std::optional* lease) { + lease->emplace(co_await file.ReadBuffer()); +} + +// Appends one ReadBuffer() to *data. Gives the error code, or -1 at the end of the stream. +template +TFuture AppendLease(TFile& file, std::string* data, int* result) { + try { + TReadBuffer lease = co_await file.ReadBuffer(); + data->append(lease.Data().data(), lease.Data().size()); + *result = lease.Data().empty() ? -1 : 0; + } catch (const std::system_error& ex) { + *result = ex.code().value(); + } +} + +template +std::string ReadToEnd(TLoop& loop, TFile& file) { + std::string data; + int result = 0; + while (result == 0) { + auto read = AppendLease(file, &data, &result); + StepUntilDone(loop, read); + } + assert_int_equal(result, -1); + return data; +} + +template +TFuture ConnectTo(TSocket& socket, const TAddress& address) { + co_await socket.Connect(address); +} + +template +struct TConnected { + using TSocket = typename TPoller::TSocket; + + explicit TConnected(TLoop& loop) + : Address{"127.0.0.1", GetFreePort()} + , Listener(loop.Poller(), Address.Domain()) + , Client(loop.Poller(), Address.Domain()) + { + Listener.Bind(Address); + Listener.Listen(); + std::optional server; + auto accept = AcceptInto(Listener, &server); + auto connect = ConnectTo(Client, Address); + StepUntilDone(loop, connect); + StepUntilDone(loop, accept); + Server = std::move(*server); + } + + TAddress Address; + TSocket Listener; + TSocket Client; + TSocket Server; +}; + +} // namespace + +// The end of the stream is an empty lease, again on every later read. +template +void test_read_buffer_eof(void**) { + TLoop loop; + TConnected c(loop); + assert_int_equal(send(c.Client.Fd(), "hello", 5, 0), 5); + c.Client.Close(); + assert_string_equal(ReadToEnd(loop, c.Server).c_str(), "hello"); + std::string data; + int result = 0; + auto again = AppendLease(c.Server, &data, &result); + StepUntilDone(loop, again); + assert_int_equal(result, -1); +} + +template +void test_read_buffer_error(void**) { + TLoop loop; + TConnected c(loop); + linger reset = {1, 0}; + assert_int_equal(setsockopt(c.Client.Fd(), SOL_SOCKET, SO_LINGER, reinterpret_cast(&reset), sizeof(reset)), 0); + c.Client.Close(); + std::string data; + int result = 0; + auto read = AppendLease(c.Server, &data, &result); + StepUntilDone(loop, read); + assert_int_equal(result, ConnReset); +} + +// Consume() drops bytes from the front, the lease moves, and its destructor gives the buffer back. +template +void test_read_buffer_consume(void**) { + TLoop loop; + loop.Poller().ConfigureReadBuffers({.BufferSize = 64, .Count = 1}); + TConnected c(loop); + assert_int_equal(send(c.Client.Fd(), "hello world", 11, 0), 11); + std::optional lease; + auto read = ReadLease(c.Server, &lease); + StepUntilDone(loop, read); + assert_true(std::string_view(lease->Data().data(), lease->Data().size()) == "hello world"); + lease->Consume(6); + TReadBuffer moved = std::move(*lease); + lease.reset(); + assert_true(std::string_view(moved.Data().data(), moved.Data().size()) == "world"); + moved.Consume(5); + assert_true(moved.Data().empty()); + bool rejected = false; + try { + loop.Poller().ConfigureReadBuffers({}); + } catch (const std::logic_error&) { + rejected = true; + } + assert_true(rejected); + moved = TReadBuffer{}; + // The only buffer is back. + assert_int_equal(send(c.Client.Fd(), "!", 1, 0), 1); + auto next = ReadLease(c.Server, &lease); + StepUntilDone(loop, next); + assert_int_equal(lease->Data().size(), 1); + assert_int_equal(lease->Data()[0], '!'); +} + +// ReadSome() and ReadBuffer() of one socket return its bytes in order, also +// when the poller has read ahead before ReadSome(). +template +void test_read_buffer_mixed_order(void**) { + TLoop loop; + TConnected c(loop); + std::string data; + int result = 0; + assert_int_equal(send(c.Client.Fd(), "01", 2, 0), 2); + while (data.size() < 2) { + auto read = AppendLease(c.Server, &data, &result); + StepUntilDone(loop, read); + assert_int_equal(result, 0); + } + for (const char* chunk : {"23", "45", "67"}) { + assert_int_equal(send(c.Client.Fd(), chunk, 2, 0), 2); + loop.Step(); + } + for (int i = 0; data.size() < 8; i++) { + if (i % 2 == 0) { + char buf[1]; + int got = 0; + auto read = ReadInto(c.Server, buf, sizeof(buf), &got); + StepUntilDone(loop, read); + assert_int_equal(got, 1); + data.push_back(buf[0]); + } else { + auto read = AppendLease(c.Server, &data, &result); + StepUntilDone(loop, read); + assert_int_equal(result, 0); + } + } + assert_string_equal(data.c_str(), "01234567"); +} + +// A reader that holds every buffer gets no more: the readiness pollers throw. +// Bytes sent faster than they are read stay in order. +template +void test_read_buffer_back_pressure(void**) { + TLoop loop; + loop.Poller().ConfigureReadBuffers({.BufferSize = 256, .Count = 2, .PerSocketLimit = 1}); + TConnected c(loop); + std::optional held[2]; + for (auto& lease : held) { + assert_int_equal(send(c.Client.Fd(), "x", 1, 0), 1); + auto read = ReadLease(c.Server, &lease); + StepUntilDone(loop, read); + } + assert_int_equal(send(c.Client.Fd(), "y", 1, 0), 1); + std::string data; + int result = 0; + auto starved = AppendLease(c.Server, &data, &result); + StepUntilDone(loop, starved); + assert_int_equal(result, static_cast(std::errc::no_buffer_space)); + held[0].reset(); + held[1].reset(); + + std::string sent(256 * 1024, 0); + for (size_t i = 0; i < sent.size(); i++) { + sent[i] = static_cast('a' + i % 23); + } + TByteWriter writer(c.Client); + auto write = writer.Write(sent.data(), sent.size()); + std::string got; + while (got.size() < sent.size() + 1) { + auto read = AppendLease(c.Server, &got, &result); + StepUntilDone(loop, read); + assert_int_equal(result, 0); + } + StepUntilDone(loop, write); + assert_true(got == "y" + sent); +} + +// A reader waiting when its socket closes is not resumed on the readiness +// pollers, as with ReadSome(); the driven ones resume it with an error. The +// buffer comes back, and a socket that reuses the number reads its own bytes. +template +void test_read_buffer_close_pending(void**) { + TLoop loop; + loop.Poller().ConfigureReadBuffers({.BufferSize = 64, .Count = 1}); + std::string data; + int result = 0; + { + TConnected c(loop); + std::optional> reader{AppendLease(c.Server, &data, &result)}; + loop.Step(); + c.Server.Close(); + for (int i = 0; i < 3; i++) { + loop.Step(); + } + if constexpr (kDrivenSocket) { + assert_true(reader->done()); + assert_true(result > 0); + } else { + assert_false(reader->done()); + } + reader.reset(); + } + loop.Step(); + TConnected c(loop); + assert_int_equal(send(c.Client.Fd(), "z", 1, 0), 1); + result = 0; + data.clear(); + auto read = AppendLease(c.Server, &data, &result); + StepUntilDone(loop, read); + assert_int_equal(result, 0); + assert_string_equal(data.c_str(), "z"); +} + +// A reader destroyed while waiting gives its buffer back and loses no bytes. +template +void test_read_buffer_destroyed_waiter(void**) { + TLoop loop; + loop.Poller().ConfigureReadBuffers({.BufferSize = 64, .Count = 1}); + TConnected c(loop); + std::string data; + int result = 0; + for (bool submitted : {false, true}) { + std::optional> reader{AppendLease(c.Server, &data, &result)}; + if (submitted) { + loop.Step(); + } + reader.reset(); + assert_int_equal(send(c.Client.Fd(), submitted ? "b" : "a", 1, 0), 1); + for (int i = 0; i < 3; i++) { + loop.Step(); + } + assert_true(data.empty()); + auto read = AppendLease(c.Server, &data, &result); + StepUntilDone(loop, read); + assert_int_equal(result, 0); + assert_string_equal(data.c_str(), submitted ? "b" : "a"); + data.clear(); + } +} + +#ifndef _WIN32 +// Pipes and regular files: the end of the stream and a read error. +template +void test_read_buffer_files(void**) { + using TFileHandle = typename TPoller::TFileHandle; + TLoop loop; + loop.Poller().ConfigureReadBuffers({.BufferSize = 64, .Count = 2}); + int p[2]; + assert_int_equal(pipe(p), 0); + { + TFileHandle in(p[0], loop.Poller()); + TFileHandle out(p[1], loop.Poller()); + assert_int_equal(write(p[1], "pipe", 4), 4); + std::string data; + int result = 0; + auto first = AppendLease(in, &data, &result); + StepUntilDone(loop, first); + assert_string_equal(data.c_str(), "pipe"); + // Not readable: the write end. + auto bad = AppendLease(out, &data, &result); + StepUntilDone(loop, bad); + assert_int_equal(result, EBADF); + out.Close(); + assert_string_equal(ReadToEnd(loop, in).c_str(), ""); + } + char path[] = "/tmp/coroio_read_buffer_XXXXXX"; + int fd = mkstemp(path); + assert_true(fd >= 0); + unlink(path); + std::string sent(1000, 'f'); + assert_int_equal(write(fd, sent.data(), sent.size()), static_cast(sent.size())); + assert_int_equal(lseek(fd, 0, SEEK_SET), 0); + TFileHandle file(fd, loop.Poller()); + assert_true(ReadToEnd(loop, file) == sent); +} +#endif + #ifdef _WIN32 // A failed or aborted read or write gets the error in its packet, not 0 bytes // that look like the end of the stream (#183). @@ -4342,6 +4649,16 @@ int main(int argc, char* argv[]) { ADD_TEST(cmocka_unit_test, test_iocp_file_concurrent); ADD_TEST(cmocka_unit_test, test_iocp_pipe); ADD_TEST(cmocka_unit_test, test_iocp_accepted_socket); +#endif + ADD_TEST(my_unit_poller, test_read_buffer_eof); + ADD_TEST(my_unit_poller, test_read_buffer_error); + ADD_TEST(my_unit_poller, test_read_buffer_consume); + ADD_TEST(my_unit_poller, test_read_buffer_mixed_order); + ADD_TEST(my_unit_poller, test_read_buffer_back_pressure); + ADD_TEST(my_unit_poller, test_read_buffer_close_pending); + ADD_TEST(my_unit_poller, test_read_buffer_destroyed_waiter); +#ifndef _WIN32 + ADD_TEST(my_unit_poller, test_read_buffer_files); #endif ADD_TEST(cmocka_unit_test, test_completions);