32 lines
2.0 KiB
Python
32 lines
2.0 KiB
Python
"""A GM command can cancel its own test without destroying the running Lua VM."""
|
|
from pathlib import Path
|
|
import tempfile,subprocess
|
|
root=Path(__file__).resolve().parents[2]
|
|
s=(root/'src/server/game/World/ClientlessTestBotMgr.cpp').read_text();a=s.index('void Finish(Run& r,');b=s.index('Run& Context(',a)
|
|
code=r'''
|
|
#include <cassert>
|
|
#include <map>
|
|
#include <set>
|
|
#include <string>
|
|
#include <vector>
|
|
using uint32=unsigned;
|
|
#define TC_LOG_INFO(...) ((void)0)
|
|
struct Run{std::string Id,State="running",Error,PendingState,PendingError;bool Executing=false;void*Lua=nullptr,*Thread=nullptr;unsigned Checks=0,Failures=0;std::set<unsigned>Bots{148};std::vector<std::string>Events{"before"};};
|
|
struct Travel{unsigned cancellations=0;void CancelTravel(unsigned){++cancellations;}}travel;auto sClientlessTravelMgr=&travel;
|
|
struct Manager{unsigned stops=0;void StopPlayer(unsigned){++stops;}}manager;auto sClientlessPlayerMgr=&manager;
|
|
std::map<unsigned,std::string>leases{{148,"test"}};unsigned closes=0,publishes=0;
|
|
void Publish(Run const&){++publishes;}void lua_close(void*){++closes;}
|
|
'''+s[a:b]+r'''
|
|
int main(){Run r;r.Lua=r.Thread=&r;r.Executing=true;
|
|
Finish(r,"cancelled","GM requested cancellation");assert(closes==0&&manager.stops==0&&r.Lua&&r.State=="running");
|
|
assert(r.PendingState=="cancelled"&&r.PendingError=="GM requested cancellation");
|
|
r.Executing=false;auto state=std::move(r.PendingState),error=std::move(r.PendingError);r.PendingState.clear();Finish(r,std::move(state),std::move(error));
|
|
assert(closes==1&&manager.stops==1&&travel.cancellations==1&&publishes==1&&r.Lua==nullptr&&r.Thread==nullptr&&r.Events.empty()&&leases.empty()&&r.State=="cancelled");
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory()as d:
|
|
p=Path(d);(p/'test.cpp').write_text(code)
|
|
subprocess.run(['g++','-std=c++17','-fsanitize=address,undefined',str(p/'test.cpp'),'-o',str(p/'test')],check=True)
|
|
subprocess.run([str(p/'test')],check=True)
|
|
print('Native test finalization: reentrant GM cancellation defers Lua close until execution returns.')
|