Files
WoW-Hybrid-gateway/tests/spell_readiness_deadline_test.py

110 lines
5.9 KiB
Python

"""The real readiness gate must not time out its already-proved delivery phase."""
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
class ReadinessDeadline(unittest.TestCase):
def test_pending_deadline_and_proved_delivery_are_distinct(self):
source = (ROOT / 'native/wine_pipe_host.c').read_text()
hold = source[source.index('static int spell_barrier_hold('):source.index('static int spell_barrier_classify(')]
poll = source[source.index('static void spell_barrier_poll('):source.index('static int drain_world_packets(')]
fixture = r'''
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "world_locator_fifo.h"
struct gw_pipe_spell_locator_barrier {
uint32_t state,generation,known_count,required_count,classified,probes,missing_id,map_reason,read_bytes,skipped_busy;
uint32_t held_bundles,held_records,held_bytes;uint64_t started_ms;
};
struct gw_spell_locator_diagnostics {uint32_t problem_spell_id,reason,read_bytes;};
static struct gw_pipe_spell_locator_barrier gw_wire_spell_locator_barrier;
static struct {uint32_t last_legacy;} gw_wire_world_data;
static struct {uint64_t connection;} gw_wire_pipe_data;
static struct gw_world_locator_fifo spell_barrier_fifo;
static unsigned char converted[12]={1,0,0,0,0x19,0,0x69,0,0,0,0,0};
static uint32_t spell_barrier_bypassed,spell_barrier_missing_count,spell_barrier_missing_ids[32],spell_barrier_required[8];
static uintptr_t spell_barrier_main;
static int lock,locked,identity_ok=1,main_ok=1,metadata_ready,fail_reason,flushed,closed;
static uint64_t now=1+GW_WORLD_LOCATOR_TIMEOUT_MS;
static uint64_t GetTickCount64(void){return now;}
static int spell_barrier_fail(uint32_t reason){fail_reason=reason;return -1;}
static int spell_barrier_begin(void){return 0;}
static int spell_barrier_initial_bundle(uint32_t bytes){(void)bytes;return 1;}
static int spell_barrier_identity(void){return identity_ok;}
static int spell_barrier_main_qualified(int initial){assert(!initial);return main_ok;}
static void* spell_barrier_alloc(void* unused,size_t bytes){(void)unused;return malloc(bytes);}
static void release(void* unused,void* p){(void)unused;free(p);}
static int TryAcquireSRWLockExclusive(int* p){assert(p==&lock&&!locked);locked=1;return 1;}
static void ReleaseSRWLockExclusive(int* p){assert(p==&lock&&locked);locked=0;}
static int spell_barrier_classify(void){return 1;}
static int gw_spell_locators_available(uintptr_t base,const uint32_t* ids,uint32_t count,
int (*read)(void),void* context,struct gw_spell_locator_diagnostics* out){
(void)base;(void)ids;(void)count;(void)read;(void)context;(void)out;return metadata_ready;
}
static int spell_barrier_read(void){return 0;}
static int spell_barrier_flush(void){
assert(locked&&gw_wire_spell_locator_barrier.state==2);
if(spell_barrier_fifo.head){
assert(!memcmp(spell_barrier_fifo.head+1,converted,sizeof(converted)));
gw_world_locator_fifo_pop(&spell_barrier_fifo,release,0);++flushed;
}
if(!spell_barrier_fifo.head)gw_wire_spell_locator_barrier.state=3;
return 0;
}
static void close_pipe(int code){assert(code==26);++closed;}
'''
fixture += hold + '\n' + poll
fixture += r'''
static void reset(unsigned state){
gw_world_locator_fifo_clear(&spell_barrier_fifo,release,0);
gw_wire_spell_locator_barrier=(struct gw_pipe_spell_locator_barrier){.state=state,.started_ms=1,.required_count=1};
fail_reason=flushed=closed=metadata_ready=0;identity_ok=main_ok=1;now=1+GW_WORLD_LOCATOR_TIMEOUT_MS;
}
int main(void){
/* Both hold and poll keep waiting one millisecond before the configured
* deadline; exactly at it the pending phase expires. */
reset(1);now=gw_wire_spell_locator_barrier.started_ms+GW_WORLD_LOCATOR_TIMEOUT_MS-1;
assert(spell_barrier_hold(0,sizeof(converted))==1);
spell_barrier_poll();assert(!fail_reason&&!flushed&&gw_wire_spell_locator_barrier.state==1);
++now;spell_barrier_poll();assert(fail_reason==7&&!flushed&&!locked);
reset(1);assert(spell_barrier_hold(0,sizeof(converted))==-1&&fail_reason==7);
reset(1);spell_barrier_poll();assert(fail_reason==7&&!flushed&&!locked);
/* Same expired clock, but readiness was already established. Real FIFO
* ownership/deep copies/bounds remain, and delivery can span polls. */
reset(2);assert(spell_barrier_hold(0,sizeof(converted))==1);
assert(spell_barrier_hold(0,sizeof(converted))==1);
assert(spell_barrier_fifo.bundles==2&&gw_wire_spell_locator_barrier.held_records==2);
spell_barrier_poll();assert(!fail_reason&&flushed==1&&gw_wire_spell_locator_barrier.state==2);
now+=20000;spell_barrier_poll();assert(!fail_reason&&flushed==2&&gw_wire_spell_locator_barrier.state==3);
/* Becoming ready before the deadline changes phase exactly once. */
reset(1);now=100;metadata_ready=1;
assert(spell_barrier_hold(0,sizeof(converted))==1);spell_barrier_poll();
assert(!fail_reason&&flushed==1&&gw_wire_spell_locator_barrier.state==3);
/* Identity and cap failures still reject proved delivery. */
reset(2);identity_ok=0;assert(spell_barrier_hold(0,sizeof(converted))==-1&&fail_reason==2);
reset(2);main_ok=0;spell_barrier_poll();assert(fail_reason==3&&!flushed);
reset(2);spell_barrier_fifo.records=GW_WORLD_LOCATOR_MAX_RECORDS;
assert(spell_barrier_hold(0,sizeof(converted))==-1&&fail_reason==6);spell_barrier_fifo.records=0;
reset(3);assert(spell_barrier_hold(0,sizeof(converted))==0);
reset(0);return 0;
}
'''
with tempfile.TemporaryDirectory(dir=ROOT / 'runtime', prefix='readiness-deadline-') as directory:
p = Path(directory)
(p / 'fixture.c').write_text(fixture)
subprocess.run(['cc', '-std=c11', '-Wall', '-Wextra', '-Werror',
'-fsanitize=address,undefined', '-fno-omit-frame-pointer',
'-I', str(ROOT / 'native'), str(p / 'fixture.c'),
'-o', str(p / 'fixture')], check=True)
subprocess.run([str(p / 'fixture')], check=True)
if __name__ == '__main__':
unittest.main()