#!/usr/bin/env python3 """Compile and execute durable point queue and DB settlement date tests.""" import os from pathlib import Path import re import subprocess import tempfile import unittest ROOT = Path(__file__).resolve().parents[2] class ItemShopDurableTest(unittest.TestCase): def test_db_settlement_date_unification(self): source = (ROOT / 'src/server/db/src/Ava2ItemShop.cpp').read_text(encoding='latin1') op3_block = source.split('if(r.operation==3)', 1)[1].split('reply(peer,handle,r,out);return;', 1)[0] # Verify CURDATE() is queried exactly once under wallet row lock and stored as rewardDay self.assertIn('auto dateQ=query("SELECT CURDATE()");', op3_block) self.assertIn('const std::string rewardDay = mysql_fetch_row(dateQ->Get()->pSQLResult)[0];', op3_block) # Verify both quota query and reward insertion strictly use rewardDay, not CURDATE() self.assertIn("reward_day='\"+rewardDay+\"'", op3_block) self.assertIn(",'\"+rewardDay+\"')\"", op3_block) # Ensure no un-unified CURDATE() remains in operation 3 self.assertNotIn("reward_day=CURDATE()", op3_block) self.assertNotIn(",CURDATE())", op3_block) def test_game_durable_log_and_recovery(self): harness = r''' #include #include static bool failWrite=false,failSync=false; static ssize_t TestWrite(int fd,const void* data,size_t n){if(failWrite){errno=ENOSPC;return -1;}return ::write(fd,data,n);} static int TestSync(int fd){if(failSync){errno=EIO;return -1;}return ::fsync(fd);} #define write TestWrite #define fsync TestSync #include "src/server/common/point_journal.h" #undef write #undef fsync #include #include #include int main(int argc,char** argv){ std::string path=argv[1];Ava2ShopRequest request={};request.operation=3;request.account=1;request.player=2;request.product=101;request.count=1;request.expectedVnum=10;strcpy(request.token,"0123456789abcdef0123456789abcdef"); { PointJournal journal;std::map pending;assert(journal.Open(path,pending));assert(pending.empty()); auto invalid=request;invalid.count=0;assert(!journal.Intent(invalid) && journal.Ready()); invalid=request;memset(invalid.token,'z',32);assert(!journal.Intent(invalid) && journal.Ready()); assert(!journal.Done(request.token,0,1,-1) && journal.Ready()); assert(!journal.Done(request.token,255,0,100) && journal.Ready()); assert(!journal.Done(request.token,2,1,100) && journal.Ready()); assert(journal.Intent(request)); PointJournal competitor;assert(!competitor.Open(path,pending)); failWrite=true;assert(!journal.Done(request.token,0,1,10));assert(!journal.Ready());failWrite=false; assert(!journal.Intent(request)); // I/O failure latches until repaired/reopened. } { PointJournal journal;std::map pending;assert(journal.Open(path,pending));assert(pending.size()==1);assert(pending.begin()->second.count==1); assert(journal.Done(request.token,0,1,10)); } { PointJournal journal;std::map pending;assert(journal.Open(path,pending));assert(pending.empty()); failSync=true;assert(!journal.Intent(request));failSync=false;assert(!journal.Ready()); } { PointJournal journal;std::map pending;assert(journal.Open(path,pending));assert(pending.size()==1); // same durable token, never reroll. } {std::ofstream out(path,std::ios::app);out<<"INTENT v2 broken tail";} { PointJournal journal;std::map pending;assert(!journal.Open(path,pending));assert(pending.size()==1);assert(!journal.Intent(request)); } // A process death after fsync but before sending the request must be recoverable. const std::string crash=path+".crash";pid_t child=fork();assert(child>=0); if(child==0){PointJournal j;std::map p;assert(j.Open(crash,p));assert(j.Intent(request));_exit(0);} int status=0;waitpid(child,&status,0);assert(WIFEXITED(status)&&WEXITSTATUS(status)==0); {PointJournal j;std::map p;assert(j.Open(crash,p));assert(p.size()==1);assert(j.Done(request.token,0,1,10));} // Checksums reject a syntactically valid but changed amount; original bytes remain on disk. {std::fstream out(crash,std::ios::in|std::ios::out);out.seekp(10);out.put('f');} {PointJournal j;std::map p;assert(!j.Open(crash,p));} puts("PASS actual point journal: replay, exclusive recovery, ENOSPC/EIO, corrupt tail/checksum, abrupt exit"); } ''' with tempfile.TemporaryDirectory() as tmp: src=Path(tmp)/'journal.cpp';exe=Path(tmp)/'journal';src.write_text(harness) subprocess.run(['c++','-std=c++17','-I'+str(ROOT),str(src),'-o',str(exe)],check=True) subprocess.run([str(exe),str(Path(tmp)/'pending.log')],check=True) if __name__=='__main__':unittest.main()