Files
Library/tests/standalone/Plugin/HotReloadUnderLoad.cpp
T
faumaray b3c959b920 Windows: full module parity and runtime-qualified test suite
- Net/IO: Winsock sockets (UdpSocket, TlsClientStream, WebSocketClient),
  HttpServer port with a loopback wake socket replacing eventfd, and the
  select-based IO backend built on Windows (ws2_32).
- Net: implement StaticFileResolver::open_safe and OpenedStaticFile on
  Windows (reparse-point rejection plus canonical containment check on the
  opened handle) and widen native handles to intptr_t.
- Plugin: hot reload works on Windows by loading a fresh copy while the old
  DLL stays mapped; enable the reload, discovery and FSM service reload
  tests and retire the plugin file by rename instead of replacement.
- Update: activation pointer as a directory junction on Windows.
- Stacktrace: portable path comparison for wide native strings.
- Docs/targets: every module allowlisted on Windows; record the Wine run
  (2000 tests, 1977 passed, 23 skipped, 0 failed).
- CMake: add -Wa,-mbig-obj for GNU MinGW targets (COFF section limit).
- Async: fix the FuturePool stopped-after-close test to wait for the
  executor to release its admission slot instead of racing it.

Windows and Linux suites both pass: Windows 2000/1977/23 skips/0 failed,
Linux 2027/2023/4 skips/0 failed.
2026-12-06 14:56:26 +03:00

130 lines
4.0 KiB
C++

#include <Void/Plugin/Loader.hpp>
#include <Void/Plugin/Manager.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include "Stateful.hpp"
#include <atomic>
#include <chrono>
#include <filesystem>
#include <thread>
#include <vector>
using namespace Void::Plugin;
#if defined(VOID_STATEFUL_V1_PATH) && defined(VOID_STATEFUL_V2_PATH)
namespace
{
// Atomic-rename install (fresh inode) — overwriting a mapped .so in
// place corrupts the running mapping (SIGBUS).
void install(const std::filesystem::path& src,
const std::filesystem::path& dst)
{
auto staging = dst;
staging += ".incoming";
std::filesystem::copy_file(
src, staging, std::filesystem::copy_options::overwrite_existing);
#ifdef _WIN32
// A loaded image can be renamed away but not replaced in place.
std::error_code error;
auto retired = dst;
retired += ".old";
std::filesystem::remove(retired, error);
error.clear();
std::filesystem::rename(dst, retired, error);
#endif
std::filesystem::rename(staging, dst);
}
} // namespace
// @tag stress, concurrency
Void::Testing::Test hot_reload_under_concurrent_load()
{
Void::FileSystem::TempDir tmp{"void_hotload"};
const auto so = tmp.path() / "plugin.so";
std::filesystem::copy_file(VOID_STATEFUL_V1_PATH, so);
PluginManager mgr;
Void::Testing::expect(mgr.load(so).has_value());
std::atomic<bool> stop{false};
std::atomic<long long> ops{0};
std::atomic<bool> sane{true};
std::atomic<int> crashes{0};
// Reader threads continuously acquire a pinned handle and use the
// plugin through it. acquire() returns a handle that keeps whichever
// instance was current alive for the whole call — so even as the main
// thread swaps instances underneath, no reader ever touches a freed
// object (ASan would catch it otherwise).
std::vector<std::thread> readers;
readers.reserve(6);
for (int r = 0; r < 6; ++r) {
readers.emplace_back([&] {
while (!stop.load(std::memory_order_relaxed)) {
auto h = mgr.acquire("StatefulPlugin");
if (!h) {
crashes.fetch_add(1);
continue;
}
auto* s = VoidTest::as_stateful(h.get());
const int before = s->counter();
s->bump();
const int after = s->counter();
// The pinned instance is consistent within the call.
if (after < before + 1) {
sane.store(false);
}
const int v = s->schema_version();
if (v != 1 && v != 2) {
sane.store(false);
}
ops.fetch_add(1, std::memory_order_relaxed);
}
});
}
// Main thread flips the file v1<->v2 and reloads repeatedly. The first
// v2 reload migrates v1 state forward; after that, installing v1 is a
// DOWNGRADE — v1 cannot deserialize v2 state, so the transactional
// reload must fail with StateRejected and keep the v2 instance. (An
// earlier version of this test expected every reload to succeed; that
// only passed because a fixed hotswap temp name made dlopen return the
// already-mapped image, so no flip after the first ever took effect.)
for (int i = 0; i < 24; ++i) {
const bool to_v2 = (i % 2 == 0);
install(to_v2 ? VOID_STATEFUL_V2_PATH : VOID_STATEFUL_V1_PATH, so);
const auto r = mgr.reload("StatefulPlugin");
if (to_v2) {
Void::Testing::expect(r.has_value());
} else {
Void::Testing::expect(!r.has_value());
Void::Testing::expect(r.error() == LoadError::StateRejected);
}
std::this_thread::sleep_for(std::chrono::milliseconds{2});
}
stop.store(true);
for (auto& t : readers) {
t.join();
}
Void::Testing::expect(sane.load());
Void::Testing::expect_eq(crashes.load(), 0);
Void::Testing::expect(ops.load() > 0);
// State survived every migration: the counter is monotonic, never
// reset by a swap, and the surviving instance is the v2 build (every
// attempted downgrade was rejected transactionally).
auto final = mgr.acquire("StatefulPlugin");
Void::Testing::expect(static_cast<bool>(final));
Void::Testing::expect(VoidTest::as_stateful(final.get())->counter() > 0);
Void::Testing::expect_eq(
VoidTest::as_stateful(final.get())->schema_version(), 2);
}
#endif // stateful plugins available