50 lines
2.7 KiB
Python
50 lines
2.7 KiB
Python
"""Two real game clients; no GPIO, no live deployment database."""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import threading
|
|
import time
|
|
import urllib.request
|
|
from http.server import HTTPServer
|
|
from test_room import ROOT, server
|
|
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
fleet=server.Fleet(Path(temp)/'chain.db',json.loads((ROOT/'tools/room_controller/catalog.json').read_text()))
|
|
service=HTTPServer(('127.0.0.1',0),server.handler_for(fleet,'test-operator-token',{'cabinet_01':'test-token-one-123','cabinet_02':'test-token-two-123'}))
|
|
thread=threading.Thread(target=service.serve_forever,daemon=True)
|
|
thread.start()
|
|
url='http://127.0.0.1:'+str(service.server_port)
|
|
processes=[]
|
|
def post(path,data={}):
|
|
request=urllib.request.Request(url+path,data=json.dumps(data).encode(),headers={'Authorization':'Bearer test-operator-token'})
|
|
with urllib.request.urlopen(request,timeout=3) as response: return json.load(response)
|
|
def wait(predicate):
|
|
deadline=time.monotonic()+12
|
|
while time.monotonic()<deadline:
|
|
state=post('/operator/state')
|
|
if predicate(state['cabinets']): return state
|
|
time.sleep(.1)
|
|
raise AssertionError('Timed out waiting for chain state')
|
|
try:
|
|
for cabinet,token in [('cabinet_01','test-token-one-123'),('cabinet_02','test-token-two-123')]:
|
|
processes.append(subprocess.Popen([sys.argv[1],'--headless','--path',str(ROOT),'--log-file',str(Path(temp)/(cabinet+'.log')),'--script','tests/room_network/managed_agent.gd','--','--cabinet='+cabinet,'--test-no-reward'],env=dict(os.environ,ARCADE_ROOM_URL=url,ARCADE_CABINET_TOKEN=token)))
|
|
wait(lambda c:c['cabinet_01']['online'] and c['cabinet_02']['online'])
|
|
post('/operator/command',{'action':'prepare_scenario','scenario':'cabinet_chain_test'})
|
|
wait(lambda c:c['cabinet_01']['phase']=='ready' and c['cabinet_02']['phase']=='waiting')
|
|
post('/operator/command',{'action':'start','cabinet':'cabinet_01'})
|
|
state=wait(lambda c:c['cabinet_01']['phase']=='won' and c['cabinet_02']['phase']=='won')
|
|
commands=state['cabinets']['cabinet_02']['commands']
|
|
assert [c['type'] for c in commands]==['reset','prepare','start']
|
|
assert all(c['status']=='accepted' for c in commands)
|
|
for process in processes: assert process.wait(timeout=8)==0
|
|
print('PASS: Pac-Man win automatically loads and starts Galaga on the second client exactly once')
|
|
finally:
|
|
for process in processes:
|
|
if process.poll() is None:
|
|
process.terminate(); process.wait(timeout=5)
|
|
service.shutdown(); service.server_close(); thread.join()
|
|
for room in fleet.rooms.values(): room.db.close()
|