82 lines
5.4 KiB
Python
82 lines
5.4 KiB
Python
"""Execute catalogue I/O, corruption, writer exclusion and crash boundaries."""
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
ROOT=Path(__file__).resolve().parents[2]
|
|
|
|
class GayaCatalogue(unittest.TestCase):
|
|
def test_atomic_snapshots_and_failures(self):
|
|
code=r'''
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <sys/stat.h>
|
|
#include <cerrno>
|
|
#include <cstdio>
|
|
static bool failWrite=false,failRename=false;static int syncs=0,failSyncAt=0,crashAt=0;
|
|
static ssize_t Write(int fd,const void* p,size_t n){if(failWrite){errno=ENOSPC;return -1;}return ::write(fd,p,n);}
|
|
static int Sync(int fd){++syncs;if(syncs==failSyncAt){errno=EIO;return -1;}int r=::fsync(fd);if(crashAt==1 && syncs==1)_exit(0);return r;}
|
|
static int Rename(int a,const char* b,int c,const char* d){if(failRename){errno=EIO;return -1;}int r=::renameat(a,b,c,d);if(crashAt==2 && r==0)_exit(0);return r;}
|
|
#define write Write
|
|
#define fsync Sync
|
|
#define renameat Rename
|
|
#include "src/server/common/gaya_catalog.h"
|
|
#undef write
|
|
#undef fsync
|
|
#undef renameat
|
|
#include <cassert>
|
|
#include <fstream>
|
|
#include <sys/wait.h>
|
|
struct Item{uint32_t itemVnum,itemCount;int itemPrice;bool itemBuyed;};
|
|
using Items=std::map<uint8_t,Item>;
|
|
std::string readFile(const std::string& path){std::ifstream f(path);return std::string((std::istreambuf_iterator<char>(f)),{});}
|
|
void writeFile(const std::string& path,const std::string& bytes){std::ofstream out(path);out<<bytes;}
|
|
int main(int argc,char** argv){
|
|
const std::string dir=argv[1],path=dir+"/1";mkdir(dir.c_str(),0700);
|
|
Items original={{0,{19,2000,50,false}},{26,{20,1,100,true}}},loaded;int refresh=99;
|
|
{PortGayaCatalogFile file;assert(file.Open(dir,1));assert(file.Load(loaded,refresh)==PortGayaCatalogFile::Missing && loaded.empty());assert(file.Save(original,200000));}
|
|
const std::string saved=readFile(path);assert(saved.find("gaya-v2 200000 ")==0);
|
|
{PortGayaCatalogFile file;assert(file.Open(dir,1));assert(file.Load(loaded,refresh)==PortGayaCatalogFile::Loaded);assert(loaded.size()==2 && loaded[0].itemCount==2000 && loaded[26].itemBuyed && refresh==200000);}
|
|
for(int fault=0;fault<4;++fault){
|
|
writeFile(path,saved);PortGayaCatalogFile file;assert(file.Open(dir,1));syncs=0;failWrite=fault==0;failSyncAt=fault==1?1:fault==3?2:0;failRename=fault==2;
|
|
auto next=original;next[0].itemBuyed=true;assert(!file.Save(next,300000));assert(!file.Ready());
|
|
failWrite=false;failSyncAt=0;failRename=false;
|
|
if(fault<3)assert(readFile(path)==saved); // before rename: last durable snapshot unchanged.
|
|
}
|
|
// A directory-fsync failure leaves a whole new file and pauses this writer.
|
|
{PortGayaCatalogFile file;assert(file.Open(dir,1));assert(file.Load(loaded,refresh)==PortGayaCatalogFile::Loaded && loaded[0].itemBuyed && refresh==300000);}
|
|
const std::string legacy="index-vnum-count-price-buyed\n";
|
|
const std::string good="0 19 2000 50 0\n26 20 1 100 1\n";
|
|
writeFile(path,legacy+good);
|
|
{PortGayaCatalogFile file;assert(file.Open(dir,1));assert(file.Load(loaded,refresh)==PortGayaCatalogFile::Loaded && refresh==0);assert(file.Save(loaded,200000));}
|
|
const std::string broken[]={"",legacy,legacy+good+"0 21 1 2 0\n",legacy+"256 19 1 1 0\n",legacy+"0 19 1 -1 0\n",legacy+"0 19 2001 1 0\n",legacy+"0 19 1 1 2\n",legacy+"0 19 1 1 0 junk\n",legacy+"0 19 1 1 0",legacy+"0 4294967296 1 1 0\n",legacy+"0 19 1 1 0\n\n",saved+"0 21 1 2 0\n"};
|
|
for(const auto& bytes:broken){
|
|
writeFile(path,bytes);PortGayaCatalogFile file;assert(file.Open(dir,1));Items before={{9,{99,1,99,false}}};refresh=77;
|
|
assert(file.Load(before,refresh)==PortGayaCatalogFile::Error && before.size()==1 && before[9].itemVnum==99 && refresh==77);
|
|
assert(readFile(path)==bytes && !file.Save(original,1));
|
|
}
|
|
writeFile(path,saved);
|
|
{PortGayaCatalogFile file;assert(file.Open(dir,1));assert(file.Save(original,200000));PortGayaCatalogFile competitor;assert(!competitor.Open(dir,1));assert(file.Save(original,200001));PortGayaCatalogFile afterRename;assert(!afterRename.Open(dir,1));}
|
|
for(int boundary=1;boundary<=2;++boundary){
|
|
writeFile(path,saved);pid_t child=fork();assert(child>=0);
|
|
if(child==0){PortGayaCatalogFile file;assert(file.Open(dir,1));syncs=0;crashAt=boundary;auto next=original;next[0].itemBuyed=true;file.Save(next,300000);_exit(9);}
|
|
int status=0;waitpid(child,&status,0);assert(WIFEXITED(status) && WEXITSTATUS(status)==0);
|
|
PortGayaCatalogFile file;assert(file.Open(dir,1));assert(file.Load(loaded,refresh)==PortGayaCatalogFile::Loaded);assert(loaded[0].itemBuyed==(boundary==2));
|
|
}
|
|
unlink(path.c_str());symlink("/does/not/exist",path.c_str());
|
|
{PortGayaCatalogFile file;assert(file.Open(dir,1));assert(file.Load(loaded,refresh)==PortGayaCatalogFile::Error);}
|
|
int next=123;
|
|
assert(PortGayaNextRefresh(100000,0,true,next) && next==118000);
|
|
assert(PortGayaNextRefresh(100000,90000,true,next) && next==108000);
|
|
assert(PortGayaNextRefresh(200000,90000,true,next) && next==216000); // many elapsed windows, still future.
|
|
assert(PortGayaNextRefresh(100000,110000,true,next) && next==110000);
|
|
assert(!PortGayaNextRefresh(INT32_MAX,1,true,next));assert(!PortGayaNextRefresh(1,-1,true,next));
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src=Path(tmp)/'probe.cpp';exe=Path(tmp)/'probe';src.write_text(code)
|
|
subprocess.run(['c++','-std=c++17','-fsanitize=undefined,address','-fno-sanitize-recover=all','-I'+str(ROOT),str(src),'-o',str(exe)],check=True)
|
|
subprocess.run([str(exe),str(Path(tmp)/'gem')],check=True)
|
|
|
|
if __name__=='__main__':unittest.main()
|