#define main cubeBaseMain #include "cube_batch_probe.cpp" #undef main #include #include #include #include static unsigned writes=0,syncs=0,failWrite=0,failSync=0,crashSync=0; static bool shortWrite=false,interruptWrite=false; static ssize_t OutboxWrite(int fd,const void* data,size_t size) { ++writes;if(interruptWrite){interruptWrite=false;errno=EINTR;return -1;} if(writes==failWrite){errno=ENOSPC;return -1;} return ::write(fd,data,shortWrite?std::max(size_t(1),size/2):size); } static int OutboxSync(int fd) { ++syncs;if(syncs==failSync){errno=EIO;return -1;} const int result=::fsync(fd);if(result==0&&syncs==crashSync)_Exit(91);return result; } #define write OutboxWrite #define fsync OutboxSync #include "src/server/common/cube_gold_outbox.h" #undef write #undef fsync #include "src/server/common/cube_gold_store.h" #include "src/server/common/cube_handshake.h" using G=GoldIntentStore; static GoldDelivery delivery(const Batch& b,unsigned seq=1) { GoldDelivery d;auto& i=d.intent;i.account=b.account;i.player=b.player;i.epoch=b.epoch;i.owner=b.owner;i.request=b.token; i.event=std::string(31,'e')+char('0'+seq);i.sequence=seq;i.amount=250;i.source=1; d.session=8;d.cookie=7;d.handle=6;d.ground=123+seq;d.vnum=1;d.map=1;return d; } static void faults(){writes=syncs=failWrite=failSync=crashSync=0;shortWrite=interruptWrite=false;} static void waitWorker(pid_t child,int expected){int status=0;assert(waitpid(child,&status,0)==child&&WIFEXITED(status)&&WEXITSTATUS(status)==expected);} int main(int argc,char** argv) { assert(argc==2||argc==3);address=argv[1];executable=argv[0];auto b=batch();auto d=delivery(b);const auto path=address+".outbox"; if(argc==3) { const std::string mode=argv[2];GoldOutbox out;assert(out.Open(path+"."+mode));faults();crashSync=1; if(mode=="claim-crash")out.Claim(d);else out.Acknowledge(d); assert(false); } { GoldOutbox out;assert(out.Open(path));GoldOutbox second;assert(!second.Open(path)); assert(out.Claim(d)&&out.Claim(d)&&out.Pending().size()==1); auto conflict=d;conflict.intent.amount++;assert(!out.Claim(conflict)); auto gap=delivery(b,3);assert(!out.Claim(gap));auto next=delivery(b,2);assert(out.Claim(next)); auto body=EncodeGoldDelivery(d);assert(body.size()==GoldDeliveryBytes);GoldDelivery decoded; assert(DecodeGoldDelivery(body,decoded));for(size_t n=0;n=0);const char bad='z';assert(pwrite(f,&bad,1,20)==1);close(f);} {GoldOutbox out;assert(!out.Open(path+".corrupt"));struct stat s={};assert(stat((path+".corrupt").c_str(),&s)==0&&size_t(s.st_size)==GoldOutbox::FrameBytes);} {const int f=open((path+".oversize").c_str(),O_RDWR|O_CREAT,0600);assert(f>=0&&ftruncate(f,GoldOutbox::MaxBytes+1)==0);close(f);} {GoldOutbox out;assert(!out.Open(path+".oversize"));} puts("PASS durable outbox codec, exclusive ownership, immutable events, sequence history, short/EINTR writes, torn tail, sync failures and unsafe links"); {GoldOutbox out;assert(out.Open(path+".claim-crash"));} waitWorker(spawnWorker("claim-crash"),91); {GoldOutbox out;assert(out.Open(path+".claim-crash")&&out.Pending().size()==1);} {GoldOutbox out;assert(out.Open(path+".ack-crash")&&out.Claim(d));} waitWorker(spawnWorker("ack-crash"),91); {GoldOutbox out;assert(out.Open(path+".ack-crash")&&out.Pending().empty()&&out.Next(b.token)==2);} puts("PASS separate-process source fsync / ACK fsync exits; restart recovers pending or acknowledged sequence without reissuing an event"); TestDB db;initialize(db); initializeGoldAddon(db);sql(db,"DELETE FROM port_cube_gold_sources");sql(db,"DELETE FROM port_cube_gold_intents");reset(db,b); G store(db,proto);G::Receipt receipt; {GoldOutbox out;assert(out.Open(path+".pipeline")&&out.Claim(d));db.ambiguous=true;assert(store.Enqueue(b,d.intent)==G::Retry);assert(out.Pending().size()==1);} {GoldOutbox out;assert(out.Open(path+".pipeline")&&out.Pending().size()==1);const auto saved=out.Pending()[0];assert(store.Enqueue(b,saved.intent)==G::Stored);assert(number(db,"SELECT COUNT(*) FROM port_cube_gold_intents")==1);assert(out.Acknowledge(saved));} {GoldOutbox out;assert(out.Open(path+".pipeline")&&out.Pending().empty()&&out.Next(b.token)==2);} {GoldOutbox out;assert(out.Open(path+".pipeline")&&out.CloseSource(GoldSourceFor(d)));auto seals=out.PendingSources();assert(seals.size()==1&&store.CloseSource(b,seals[0])==G::Stored&&out.AcknowledgeSource(seals[0]));} Store craft(db,proto);Store::Receipt result;assert(craft.Prepare(b,[]{return 1u;})==Store::Prepared&&craft.Execute(b.token,1,1,result)==Store::Complete); assert(store.Apply(d.intent.event,1,1,receipt)==G::Applied&&number(db,"SELECT gold FROM player")==950); assert(store.Apply(d.intent.event,1,1,receipt)==G::Applied&&number(db,"SELECT gold FROM player")==950); FenceStore fence(db,proto);std::string image;assert(fence.Resync(b,image)==FenceStore::Done&&fence.Release(b,image)==FenceStore::Done); auto f=ArmFrame(b,d.session);f.message=Message::GoldEnqueue;f.cookie=d.cookie;f.body=EncodeGoldIntent(d.intent); const auto wire=EncodeHandshake(f);Handshake decoded;assert(wire.size()==HandshakeFixed+GoldIntentBytes&&DecodeHandshake(wire.data(),wire.size(),decoded)); f.player++;assert(!ValidHandshake(f));f.player--;f.message=Message::GoldAck;f.status=Status::GoldStored;assert(ValidHandshake(f)); ActorHandshake actor;assert(actor.Start(b,d.session,1,true));assert(!actor.Answer(f)&&actor.BlocksActions()); puts("PASS source journal -> ambiguous SQL enqueue -> restart/retry -> durable ACK -> verified craft DONE -> single credit -> exact-image release; explicit SQL/actor substitutes, not live freeze acceptance"); }