feat(Containers): fix pool lease lifetimes/budgets and non-spinning MPSC ring (M-CONTAINERS-02, M-CONTAINERS-03)
This commit is contained in:
1 parent
883bbdcf38
commit
9ac76bbcf9
7 files changed
+1252
-285
No files matched your search
@@ -1,8 +1,11 @@
|
||||
#include <Void/Containers/BufferPool.hpp>
|
||||
#include <Void/Memory/BudgetResource.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -82,5 +85,92 @@ namespace
|
||||
pool.shrink();
|
||||
Void::Testing::expect_eq(pool.stats().pooled, std::size_t{0});
|
||||
}
|
||||
|
||||
Test leases_survive_pool_move_and_destruction()
|
||||
{
|
||||
Void::Memory::BudgetResource budget{65536};
|
||||
BufferPool<std::uint32_t>::Buffer retained;
|
||||
{
|
||||
BufferPool<std::uint32_t> p1{&budget};
|
||||
auto b = p1.try_acquire(64);
|
||||
Void::Testing::require(b.has_value());
|
||||
(*b)[0] = 0xCAFEBABEu;
|
||||
auto p2 = std::move(p1);
|
||||
retained = std::move(*b);
|
||||
Void::Testing::expect_eq(p2.stats().allocations,
|
||||
std::uint64_t{1});
|
||||
}
|
||||
// Pool destroyed; retained buffer still intact and frees cleanly on drop.
|
||||
Void::Testing::expect_eq(retained[0], 0xCAFEBABEu);
|
||||
Void::Testing::expect(budget.stats().committed_live > 0);
|
||||
retained = BufferPool<std::uint32_t>::Buffer{};
|
||||
Void::Testing::expect_eq(budget.stats().committed_live,
|
||||
std::size_t{0});
|
||||
}
|
||||
|
||||
Test enforces_request_idle_total_limits_and_overflow()
|
||||
{
|
||||
using Void::BufferPool::BufferPoolError;
|
||||
|
||||
BufferPool<std::uint64_t> small_pool{
|
||||
BufferPool<std::uint64_t>::Limits{
|
||||
.max_request_bytes = 256,
|
||||
.max_idle_bytes = 256,
|
||||
.max_total_bytes = 512}};
|
||||
|
||||
const auto ovf = small_pool.try_acquire(
|
||||
std::numeric_limits<std::size_t>::max());
|
||||
Void::Testing::expect(
|
||||
!ovf.has_value() &&
|
||||
ovf.error() == BufferPoolError::size_overflow);
|
||||
|
||||
const auto too_big = small_pool.try_acquire(64); // 512B > 256B
|
||||
Void::Testing::expect(
|
||||
!too_big.has_value() &&
|
||||
too_big.error() == BufferPoolError::request_too_large);
|
||||
|
||||
auto a = small_pool.try_acquire(32); // 256B
|
||||
auto b = small_pool.try_acquire(32); // 256B (total 512B)
|
||||
auto c = small_pool.try_acquire(32); // exceeds 512B
|
||||
Void::Testing::require(a.has_value() && b.has_value());
|
||||
Void::Testing::expect(
|
||||
!c.has_value() &&
|
||||
c.error() == BufferPoolError::budget_exceeded);
|
||||
|
||||
a = {}; // fits in max_idle_bytes=256B
|
||||
b = {}; // exceeds max_idle_bytes=256B -> dropped_on_return
|
||||
Void::Testing::expect_eq(small_pool.stats().pooled,
|
||||
std::size_t{1});
|
||||
Void::Testing::expect_eq(small_pool.stats().idle_bytes,
|
||||
std::size_t{256});
|
||||
Void::Testing::expect_eq(small_pool.stats().dropped_on_return,
|
||||
std::uint64_t{1});
|
||||
}
|
||||
|
||||
Test budget_resource_zero_allocs_on_reuse_and_handles_oom()
|
||||
{
|
||||
using Void::BufferPool::BufferPoolError;
|
||||
|
||||
Void::Memory::BudgetResource budget{1024};
|
||||
BufferPool<std::uint8_t> pool{&budget};
|
||||
{
|
||||
auto b = pool.try_acquire(256);
|
||||
Void::Testing::require(b.has_value());
|
||||
}
|
||||
const auto allocs = budget.stats().allocations;
|
||||
Void::Testing::expect_eq(allocs, std::uint64_t{1});
|
||||
|
||||
{
|
||||
auto b2 = pool.try_acquire(200); // same class 256
|
||||
Void::Testing::require(b2.has_value());
|
||||
Void::Testing::expect_eq(budget.stats().allocations, allocs);
|
||||
Void::Testing::expect_eq(pool.stats().reuses, std::uint64_t{1});
|
||||
}
|
||||
|
||||
const auto fail_alloc = pool.try_acquire(2048);
|
||||
Void::Testing::expect(
|
||||
!fail_alloc.has_value() &&
|
||||
fail_alloc.error() == BufferPoolError::allocation_failed);
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
Reference in new issue
Block a user