Files
WoW-Hybrid-gateway/tests/diagnostics_journal_boundary_test.cpp

50 lines
6.2 KiB
C++

#define GW_DIAGNOSTICS_TEST_HOOKS
#include "../native/diagnostics_journal.hpp"
#include <cassert>
#include <iostream>
#include <memory>
#ifndef _WIN32
#include <sys/stat.h>
#endif
using namespace gw_diagnostics;
namespace fs=std::filesystem;
template<class F>void wait(F predicate){for(unsigned n=0;n<400;++n){if(predicate())return;std::this_thread::sleep_for(std::chrono::milliseconds(5));}assert(false&&"worker boundary deadline");}
void write(const fs::path&p,const std::string&s){std::ofstream f(p,std::ios::binary);f<<s;assert(f);}
void owned(const fs::path&p){fs::create_directories(p);write(p/"manifest.json","{\"kind\":\"wowhybrid-diagnostics-session-v1\"}\n");write(p/"known_spells.jsonl",std::string(1024,'x'));write(p/"complete","complete\n");}
struct Barrier {std::atomic<bool>entered{false},release{false};void block(){entered=true;while(!release.load())std::this_thread::yield();}};
struct BlockingRange {
Barrier&gate;
struct Iterator {Barrier*gate;unsigned offset;uint32_t operator*()const{gate->block();return 51602;}Iterator&operator++(){++offset;return *this;}bool operator!=(const Iterator&other)const{return offset!=other.offset;}};
Iterator begin()const{return{&gate,0};}Iterator end()const{return{&gate,1};}
};
int main(int argc,char**argv){assert(argc==2);fs::path root=argv[1];fs::create_directories(root);
// Public hook evaluation itself cannot leak defaults/allocation exceptions.
{bool factory_ran=false;JournalHandle disabled(process_instance([&]()->Journal*{factory_ran=true;throw std::bad_alloc();}));assert(factory_ran);static_assert(noexcept(journal()));disabled.begin(70);disabled.enabled(true);disabled.known(4514,std::vector<uint32_t>{51602});assert(!disabled.definition(0x58,1,true,{}));assert(!disabled.status().enabled&&disabled.status().error==6);}
// Real producer pauses after capturing its epoch, then off clears and on
// advances it. Resuming that old producer must not enqueue into the new epoch.
{Options o;o.directory=root/"epoch";Journal j(o);j.begin(70);Barrier gate;std::thread producer([&]{j.known(4514,BlockingRange{gate});});wait([&]{return gate.entered.load();});j.enabled(false);j.enabled(true);gate.release=true;producer.join();assert(j.status().dropped>=1);wait([&]{return j.status().bytes[4]>0;});std::this_thread::sleep_for(std::chrono::milliseconds(1100));assert(j.status().events[0]==0);}
// Off wins before the worker is scheduled: it must neither create session
// headers nor prune an older completed directory. On resumes that same worker.
{Options o;o.directory=root/"startup";o.retained_bytes=100;auto old=o.directory/"20000101T000000Z-1";owned(old);Barrier gate;o.before_worker_start=[&]{gate.block();};Journal j(o);j.begin(70);wait([&]{return gate.entered.load();});j.enabled(false);gate.release=true;std::this_thread::sleep_for(std::chrono::milliseconds(100));assert(fs::exists(old/"known_spells.jsonl"));assert(std::distance(fs::directory_iterator(o.directory),fs::directory_iterator{})==1);assert(j.status().bytes[0]==0);j.enabled(true);wait([&]{return j.status().bytes[4]>0;});assert(!fs::exists(old));}
// An unknown file prevents deletion but cannot hide our known JSONL bytes
// from the retained-byte budget. Fail the journal, preserve every old file.
{Options o;o.directory=root/"accounting";o.retained_bytes=100;auto old=o.directory/"20000101T000000Z-1";owned(old);write(old/"precious.bin","keep");Journal j(o);j.begin(70);wait([&]{return j.status().error!=0;});assert(!j.status().enabled&&fs::file_size(old/"known_spells.jsonl")==1024&&fs::file_size(old/"precious.bin")==4);}
// Safe completed sessions are still pruned oldest-first when an owned
// undeletable directory consumes a retained-session slot.
{Options o;o.directory=root/"count-retention";o.retained_bytes=2000;o.retained_sessions=2;auto first=o.directory/"20000101T000000Z-1",second=o.directory/"20000101T000001Z-1",foreign=o.directory/"foreign";owned(first);owned(second);fs::create_directories(foreign);write(foreign/"manifest.json","{\"kind\":\"wowhybrid-diagnostics-session-v1\"}\n");write(foreign/"precious.bin","keep");Journal j(o);j.begin(70);wait([&]{return j.status().bytes[4]>0;});assert(!fs::exists(first)&&!fs::exists(second)&&fs::file_size(foreign/"precious.bin")==4);}
// Real filesystem mutation between scan/delete: fresh unknown content is
// preserved and cleanup stops before any known stream/manifest is deleted.
{Options o;o.directory=root/"changed";o.retained_bytes=100;auto old=o.directory/"20000101T000000Z-1";owned(old);o.before_retention_delete=[&]{write(old/"precious.bin","late");};Journal j(o);j.begin(70);wait([&]{return j.status().error!=0;});assert(fs::exists(old/"manifest.json")&&fs::file_size(old/"known_spells.jsonl")==1024&&fs::file_size(old/"precious.bin")==4);}
#ifndef _WIN32
// Ownership probing rejects a FIFO/linked manifest BEFORE opening it.
{Options o;o.directory=root/"manifest-types";fs::create_directories(o.directory/"fifo");assert(mkfifo((o.directory/"fifo/manifest.json").c_str(),0600)==0);fs::create_directories(o.directory/"linked");auto target=root/"outside-marker";write(target,"{\"kind\":\"wowhybrid-diagnostics-session-v1\"}\n");const auto modified=fs::last_write_time(target);fs::create_symlink(target,o.directory/"linked/manifest.json");Journal j(o);j.begin(70);wait([&]{return j.status().bytes[4]>0;});assert(!j.status().error&&fs::last_write_time(target)==modified&&fs::is_fifo(fs::symlink_status(o.directory/"fifo/manifest.json")));}
#endif
// Wine: every process is PID 32, so an unfinished old session is never provably finished. One untouched for
// 12+ hours is reclaimed; a fresh unfinished one (a concurrent instance) is kept.
{Options o;o.directory=root/"stale";o.retained_sessions=2;auto oldp=o.directory/"20000101T000000Z-32",fresh=o.directory/"20000101T000001Z-32";
for(auto d:{oldp,fresh}){owned(d);fs::remove(d/"complete");}
const auto past=fs::file_time_type::clock::now()-std::chrono::hours(13);for(auto&f:fs::directory_iterator(oldp))fs::last_write_time(f.path(),past);
Journal j(o);j.begin(70);wait([&]{return j.status().error!=0||j.status().bytes[4]>0;});assert(!fs::exists(oldp)&&fs::exists(fresh/"manifest.json"));}
std::cout<<"W36 actual epoch/off-startup/factoryfailure/retention-accounting-and-mutation/manifest-type boundaries PASS\n";
}