Files
Renderive/render_3D/tests/Render_Domain_Tests.cpp
T
2026-08-15 23:24:23 +08:00

76 lines
2.8 KiB
C++

#include "render_3D/detail/Render_Domain.h"
#include <gtest/gtest.h>
#include <atomic>
#include <chrono>
#include <future>
#include <memory>
#include <stdexcept>
#include <utility>
namespace renderive::render_3d::detail {
namespace {
static_assert(noexcept(std::declval<Render_Domain&>().post(std::declval<Render_Domain::Prepared_Task>())));
TEST(RenderDomain, SharesDomainPerGpuIndex) {
auto first = Render_Domain::acquire(0x7ffffffcU);
auto second = Render_Domain::acquire(0x7ffffffcU);
auto other = Render_Domain::acquire(0x7ffffffbU);
EXPECT_EQ(first, second);
EXPECT_NE(first, other);
}
TEST(RenderDomain, PreparedTaskRunsAfterNoThrowHandoff) {
auto domain = Render_Domain::acquire(0x7ffffffaU);
std::atomic<bool> executed{};
auto task = domain->prepare([&] {
executed.store(true, std::memory_order_release);
});
domain->post(std::move(task));
domain->invoke([] {});
EXPECT_TRUE(executed.load(std::memory_order_acquire));
}
TEST(RenderDomain, AbandonedPreparedTasksReleaseReservedCapacity) {
auto domain = Render_Domain::acquire(0x7ffffff9U);
for (std::size_t index = 0; index < 128; ++index) {
auto task = domain->prepare([] {});
}
domain->invoke([] {});
}
TEST(RenderDomain, NestedInvokeExecutesInlineOnTheAffinityThread) {
auto domain = Render_Domain::acquire(0x7ffffff8U);
const int value = domain->invoke([domain] {
return domain->invoke([] { return 42; });
});
EXPECT_EQ(value, 42);
}
TEST(RenderDomain, ReportsBoundedAdmissionStatistics) {
auto domain = Render_Domain::acquire(0x7ffffff7U);
const auto statistics = domain->statistics();
EXPECT_EQ(statistics.capacity, 64U);
EXPECT_TRUE(statistics.admitted <= statistics.capacity);
EXPECT_TRUE(statistics.queued <= statistics.capacity);
}
TEST(RenderDomain, ContainsUnhandledFireAndForgetExceptionsAndKeepsRunning) {
auto domain = Render_Domain::acquire(0x7ffffff6U);
const auto before = domain->statistics().unhandled_exception_count;
domain->post([] { throw std::runtime_error("unexpected render-domain failure"); });
domain->invoke([] {});
const auto after = domain->statistics().unhandled_exception_count;
EXPECT_EQ(after, before + 1U);
EXPECT_EQ(domain->invoke([] { return 17; }), 17);
}
TEST(RenderDomain, FinalOwnerMayBeReleasedOnAffinityThread) {
auto domain = Render_Domain::acquire(0x7ffffff5U);
std::weak_ptr<Render_Domain> weak = domain;
std::promise<void> released;
auto finished = released.get_future();
domain->post([owned = domain, &released]() mutable {
owned.reset();
released.set_value();
});
domain.reset();
EXPECT_EQ(finished.wait_for(std::chrono::seconds(1)),
std::future_status::ready);
EXPECT_TRUE(weak.expired());
}
}
}