| 1 |
|
| 2 |
|
| 3 |
''' |
| 4 |
Test script for record/replay. Only tested on Unix. |
| 5 |
|
| 6 |
E.g.: |
| 7 |
|
| 8 |
./flightgear/scripts/python/recordreplay.py -f run_fgfs.sh |
| 9 |
|
| 10 |
Args: |
| 11 |
--all |
| 12 |
Run all tests (this is default). |
| 13 |
--continuous BOOLS |
| 14 |
--extra-properties BOOLS |
| 15 |
--it-max <it> |
| 16 |
Set min iteration (inclusive) to run; useful when fixing tests and |
| 17 |
retrying. |
| 18 |
--it-min <it> |
| 19 |
Set max iteration (exclusive) to run; useful when fixing tests and |
| 20 |
retrying. |
| 21 |
--main-view BOOLS |
| 22 |
--multiplayer |
| 23 |
-f <fgfs> |
| 24 |
The fgfs executable to use. Default assumes the Walk build system. |
| 25 |
--f-old <fgfs-old> |
| 26 |
A second fgfs executable. If specified we run all tests twice, first |
| 27 |
using <fgfs-old> to create the recording and <fgfs> to replay it, |
| 28 |
second the other way round. |
| 29 |
--test-motion |
| 30 |
Checks that speed of aircraft on replay is not affected by frame rate. |
| 31 |
|
| 32 |
We deliberately change frame rate while recording UFO moving at |
| 33 |
constant speed. |
| 34 |
|
| 35 |
--test-motion-mp |
| 36 |
Checks that speed of MP on replay is not affected by frame rate. |
| 37 |
|
| 38 |
We deliberately change frame rate while recording UFO moving at |
| 39 |
constant speed. |
| 40 |
|
| 41 |
BOOLS is comma-sparated list of 0 or 1, with 1 activating the particular |
| 42 |
feature. So for example '--continuous 0' tests normal recording/replay', |
| 43 |
'--continuous 1' tests continuous recording/replay, and continuous 0,1' |
| 44 |
tests both. |
| 45 |
|
| 46 |
We test all combinations of continuous, extra-properties, main-view and |
| 47 |
multiplayer recordings. For each test we check that we can create a |
| 48 |
recording, and replay it in a new fgfs instance. When replaying we check |
| 49 |
a small number of basic things such as the recording length, and whether |
| 50 |
extra-properties are replayed. |
| 51 |
''' |
| 52 |
|
| 53 |
import os |
| 54 |
import signal |
| 55 |
import subprocess |
| 56 |
import sys |
| 57 |
import time |
| 58 |
|
| 59 |
try: |
| 60 |
import resource |
| 61 |
except Exception: |
| 62 |
|
| 63 |
resource = None |
| 64 |
|
| 65 |
import FlightGear |
| 66 |
|
| 67 |
def log(text): |
| 68 |
print(text, file=sys.stderr) |
| 69 |
sys.stderr.flush() |
| 70 |
|
| 71 |
g_cleanup = [] |
| 72 |
g_tapedir = './recordreplay.py.tapes' |
| 73 |
|
| 74 |
|
| 75 |
def remove(path): |
| 76 |
''' |
| 77 |
Removes file, ignoring any error. |
| 78 |
''' |
| 79 |
log(f'Removing: {path}') |
| 80 |
try: |
| 81 |
os.remove(path) |
| 82 |
except Exception as e: |
| 83 |
log(f'Failed to remove {path}: {e}') |
| 84 |
|
| 85 |
|
| 86 |
def readlink(path): |
| 87 |
''' |
| 88 |
Returns absolute path destination of link. |
| 89 |
''' |
| 90 |
ret = os.readlink(path) |
| 91 |
if not os.path.isabs(ret): |
| 92 |
ret = os.path.join(os.path.dirname(path), ret) |
| 93 |
return ret |
| 94 |
|
| 95 |
|
| 96 |
class Fg: |
| 97 |
''' |
| 98 |
Runs flightgear, with support for setting/getting properties etc. |
| 99 |
|
| 100 |
self.fg is a FlightGear.FlightGear instance, which uses telnet to |
| 101 |
communicate with Flightgear. |
| 102 |
''' |
| 103 |
def __init__(self, aircraft, args, env=None, telnet_port=None, telnet_hz=None, out=None, screensaver_suspend=True): |
| 104 |
''' |
| 105 |
aircraft: |
| 106 |
Specified as: --aircraft={aircraft}. This is separate from <args> |
| 107 |
because we need to know the name of recording files. |
| 108 |
args: |
| 109 |
Miscellenous args either space-separated name=value strings or a |
| 110 |
dict. |
| 111 |
env: |
| 112 |
Environment to set. If DISPLAY is not in <env> we add 'DISPLAY=:0'. |
| 113 |
telnet_port: |
| 114 |
telnet_hz: |
| 115 |
. |
| 116 |
''' |
| 117 |
self.child = None |
| 118 |
self.aircraft = aircraft |
| 119 |
if aircraft: |
| 120 |
args += f' --aircraft={aircraft}' |
| 121 |
|
| 122 |
if telnet_port is None: |
| 123 |
telnet_port = 5500 |
| 124 |
if telnet_hz is None: |
| 125 |
args += f' --telnet={telnet_port}' |
| 126 |
else: |
| 127 |
args += f' --telnet=_,_,{telnet_hz},_,{telnet_port},_' |
| 128 |
args += f' --prop:/sim/replay/tape-directory={g_tapedir}' |
| 129 |
args += f' --prop:bool:/sim/startup/screensaver-suspend={"true" if screensaver_suspend else "false"}' |
| 130 |
|
| 131 |
args2 = args.split() |
| 132 |
|
| 133 |
environ = os.environ.copy() |
| 134 |
if isinstance(env, str): |
| 135 |
for nv in env.split(): |
| 136 |
n, v = nv.split('=', 1) |
| 137 |
environ[n] = v |
| 138 |
if 'DISPLAY' not in environ: |
| 139 |
environ['DISPLAY'] = ':0' |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
log(f'Command is: {args}') |
| 148 |
log(f'Running: {args2}') |
| 149 |
if resource: |
| 150 |
def preexec(): |
| 151 |
try: |
| 152 |
resource.setrlimit(resource.RLIMIT_CORE, (resource.RLIM_INFINITY, resource.RLIM_INFINITY)) |
| 153 |
except Exception as e: |
| 154 |
log(f'*** preexec failed with e={e}') |
| 155 |
raise |
| 156 |
else: |
| 157 |
preexec = None |
| 158 |
if out: |
| 159 |
out = open(out, 'w') |
| 160 |
self.child = subprocess.Popen( |
| 161 |
args2, |
| 162 |
env=environ, |
| 163 |
preexec_fn=preexec, |
| 164 |
stdout=out, |
| 165 |
stderr=subprocess.STDOUT, |
| 166 |
) |
| 167 |
|
| 168 |
|
| 169 |
timeout = 15 |
| 170 |
t0 = time.time() |
| 171 |
while 1: |
| 172 |
time.sleep(1) |
| 173 |
dt = time.time() - t0 |
| 174 |
if dt > timeout: |
| 175 |
text = f'Timeout trying to connect. timeout={timeout}' |
| 176 |
log(text) |
| 177 |
raise Exception(text) |
| 178 |
try: |
| 179 |
log('Connecting... ') |
| 180 |
self.fg = FlightGear.FlightGear('localhost', telnet_port) |
| 181 |
log(f'Connected. timeout={timeout} dt={dt:.1f}') |
| 182 |
return |
| 183 |
except Exception as e: |
| 184 |
log(f'Failed to connect timeout={timeout} dt={dt:.1f}: {e}') |
| 185 |
|
| 186 |
def waitfor(self, name, value, timeout=30): |
| 187 |
''' |
| 188 |
Waits for specified property to be <value>. Returns time taken. |
| 189 |
''' |
| 190 |
t0 = time.time() |
| 191 |
while 1: |
| 192 |
time.sleep(1) |
| 193 |
dt = time.time() - t0 |
| 194 |
try: |
| 195 |
v = self.fg[name] |
| 196 |
log(f'Waiting for {name}={value} current value: {v}. timeout={timeout} dt={dt:.1f}') |
| 197 |
except Exception as e: |
| 198 |
log(f'Failed to get value of property {name}: {e}. timeout={timeout} dt={dt:.1f}') |
| 199 |
v = None |
| 200 |
if v == value: |
| 201 |
return dt |
| 202 |
if dt > timeout: |
| 203 |
raise Exception(f'Timeout waiting for {name}={value}; current value: {v}. timeout={timeout}') |
| 204 |
|
| 205 |
def run_command(self, command): |
| 206 |
self.fg.telnet._putcmd(command) |
| 207 |
ret = self.fg.telnet._getresp() |
| 208 |
log(f'command={command!r} ret: {ret}') |
| 209 |
return ret |
| 210 |
|
| 211 |
def close(self): |
| 212 |
assert self.child |
| 213 |
log(f'close(): stopping flightgear pid={self.child.pid}') |
| 214 |
if 1: |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
try: |
| 220 |
child_pids = subprocess.check_output(f'pgrep -P {self.child.pid}', shell=True) |
| 221 |
except Exception: |
| 222 |
|
| 223 |
child_pids = b'' |
| 224 |
child_pids = child_pids.decode('utf-8') |
| 225 |
child_pids = child_pids.split() |
| 226 |
for child_pid in child_pids: |
| 227 |
|
| 228 |
child_pid = int(child_pid) |
| 229 |
|
| 230 |
os.kill(child_pid, signal.SIGTERM) |
| 231 |
self.child.terminate() |
| 232 |
self.child.wait() |
| 233 |
self.child = None |
| 234 |
|
| 235 |
|
| 236 |
def __del__(self): |
| 237 |
if self.child: |
| 238 |
log('*** Fg.__del__() calling self.close()') |
| 239 |
self.close() |
| 240 |
|
| 241 |
def make_recording( |
| 242 |
fg, |
| 243 |
continuous=0, |
| 244 |
extra_properties=0, |
| 245 |
main_view=0, |
| 246 |
length=5, |
| 247 |
): |
| 248 |
''' |
| 249 |
Makes a recording, and returns its path. |
| 250 |
|
| 251 |
We check that the recording file is newly created. |
| 252 |
''' |
| 253 |
t = time.time() |
| 254 |
fg.fg['/sim/replay/record-signals'] = True |
| 255 |
if continuous: |
| 256 |
assert not fg.fg['/sim/replay/record-continuous'] |
| 257 |
if continuous == 2: |
| 258 |
fg.fg['/sim/replay/record-continuous-compression'] = 1 |
| 259 |
fg.fg['/sim/replay/record-continuous'] = 1 |
| 260 |
t0 = time.time() |
| 261 |
while 1: |
| 262 |
if time.time() > t0 + length: |
| 263 |
break |
| 264 |
time.sleep(1) |
| 265 |
fg.run_command('run view-step step=1') |
| 266 |
fg.fg['/sim/replay/record-continuous'] = 0 |
| 267 |
path = f'{g_tapedir}/{fg.aircraft}-continuous.fgtape' |
| 268 |
time.sleep(1) |
| 269 |
else: |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
while 1: |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
t_record_begin = fg.fg['sim/replay/record-normal-begin'] |
| 284 |
|
| 285 |
t_record_end = fg.fg['sim/replay/record-normal-end'] |
| 286 |
|
| 287 |
t_delta = t_record_end - t_record_begin |
| 288 |
log(f't_record_begin={t_record_begin} t_record_end={t_record_end} t_delta={t_delta}') |
| 289 |
if t_delta >= length: |
| 290 |
break |
| 291 |
ts = max(length - t_delta - 1, 0.2) |
| 292 |
log(f'd: ts={ts}') |
| 293 |
time.sleep(ts) |
| 294 |
log(f'/sim/time/elapsed-sec={t}') |
| 295 |
log(f'/sim/replay/start-time={fg.fg["/sim/replay/start-time"]}') |
| 296 |
log(f'/sim/replay/end-time={fg.fg["/sim/replay/end-time"]}') |
| 297 |
fg.fg.telnet._putcmd('run save-tape tape-data/starttime= tape-data/stoptime=') |
| 298 |
response = fg.fg.telnet._getresp() |
| 299 |
log(f'response: {response!r}') |
| 300 |
path = f'{g_tapedir}/{fg.aircraft}.fgtape' |
| 301 |
|
| 302 |
|
| 303 |
os.system(f'ls -lL {path}') |
| 304 |
s = os.stat(path, follow_symlinks=True) |
| 305 |
assert s.st_mtime > t |
| 306 |
path2 = readlink(path) |
| 307 |
log(f'path={path} path2={path2}') |
| 308 |
return path |
| 309 |
|
| 310 |
|
| 311 |
def test_record_replay( |
| 312 |
fgfs_save, |
| 313 |
fgfs_load, |
| 314 |
multiplayer, |
| 315 |
continuous, |
| 316 |
extra_properties, |
| 317 |
main_view, |
| 318 |
length, |
| 319 |
): |
| 320 |
if not fgfs_load: |
| 321 |
fgfs_load = fgfs_save |
| 322 |
log(f'=== save: {fgfs_save}') |
| 323 |
log(f'=== load: {fgfs_load}') |
| 324 |
log(f'=== --multiplayer {multiplayer} --continuous {continuous} --extra-properties {extra_properties} --main-view {main_view}') |
| 325 |
log(f'===') |
| 326 |
|
| 327 |
aircraft = 'harrier-gr3' |
| 328 |
args = f'--state=vto --airport=egtk' |
| 329 |
args += f' --prop:bool:/sim/replay/record-extra-properties={extra_properties}' |
| 330 |
args += f' --prop:bool:/sim/replay/record-main-view={main_view}' |
| 331 |
args += f' --prop:bool:/sim/replay/record-main-window=0' |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
fg = Fg(aircraft, f'{fgfs_save} {args}', |
| 336 |
|
| 337 |
telnet_hz=100, |
| 338 |
) |
| 339 |
fg.waitfor('/sim/fdm-initialized', 1, timeout=45) |
| 340 |
|
| 341 |
assert fg.fg['sim/replay/record-extra-properties'] == extra_properties |
| 342 |
assert fg.fg['sim/replay/record-main-view'] == main_view |
| 343 |
log(f'sim/replay/record-extra-properties={fg.fg["sim/replay/record-extra-properties"]}') |
| 344 |
|
| 345 |
|
| 346 |
path = make_recording(fg, |
| 347 |
continuous=continuous, |
| 348 |
extra_properties=extra_properties, |
| 349 |
main_view=main_view, |
| 350 |
length=length, |
| 351 |
) |
| 352 |
|
| 353 |
g_cleanup.append(lambda: remove(path)) |
| 354 |
fg.close() |
| 355 |
|
| 356 |
|
| 357 |
path = f'{g_tapedir}/{aircraft}-continuous.fgtape' if continuous else f'{g_tapedir}/{aircraft}.fgtape' |
| 358 |
fg = Fg(aircraft, f'{fgfs_load} {args} --load-tape={path}') |
| 359 |
fg.waitfor('/sim/fdm-initialized', 1, timeout=45) |
| 360 |
fg.waitfor('/sim/replay/replay-state', 1) |
| 361 |
|
| 362 |
t0 = time.time() |
| 363 |
|
| 364 |
|
| 365 |
rtime_begin = fg.fg['/sim/replay/start-time'] |
| 366 |
rtime_end = fg.fg['/sim/replay/end-time'] |
| 367 |
rtime = rtime_end - rtime_begin |
| 368 |
log(f'rtime={rtime_begin}..{rtime_end}, recording length: {rtime}, length={length}') |
| 369 |
assert rtime > length-1 and rtime <= length+2, \ |
| 370 |
f'length={length} rtime_begin={rtime_begin} rtime_end={rtime_end} rtime={rtime}' |
| 371 |
|
| 372 |
num_frames_extra_properties = fg.fg['/sim/replay/continuous-stats-num-frames-extra-properties'] |
| 373 |
log(f'num_frames_extra_properties={num_frames_extra_properties}') |
| 374 |
if continuous: |
| 375 |
if main_view: |
| 376 |
assert num_frames_extra_properties > 1, f'num_frames_extra_properties={num_frames_extra_properties}' |
| 377 |
else: |
| 378 |
assert num_frames_extra_properties == 0 |
| 379 |
else: |
| 380 |
assert num_frames_extra_properties in (0, None), \ |
| 381 |
f'num_frames_extra_properties={num_frames_extra_properties}' |
| 382 |
|
| 383 |
fg.run_command('run dialog-show dialog-name=replay') |
| 384 |
|
| 385 |
while 1: |
| 386 |
t = time.time() |
| 387 |
if t < t0 + length - 1: |
| 388 |
pass |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
if t > t0 + length + 1: |
| 395 |
assert fg.fg['/sim/replay/replay-state-eof'], f'Replay has not finished on time; lenth={length} t-t0={t-t0}' |
| 396 |
break |
| 397 |
e = fg.fg['sim/replay/replay-error'] |
| 398 |
assert not e, f'Replay failed: e={e}' |
| 399 |
time.sleep(1) |
| 400 |
|
| 401 |
fg.close() |
| 402 |
|
| 403 |
remove(path) |
| 404 |
|
| 405 |
log('Test passed') |
| 406 |
|
| 407 |
|
| 408 |
def test_motion(fgfs, multiplayer=False): |
| 409 |
''' |
| 410 |
Records UFO moving with constant velocity with varying framerates, then |
| 411 |
replays with varying framerates and checks that replayed UFO moves with |
| 412 |
expected constant speed. |
| 413 |
|
| 414 |
If <multiplayer> is true we also record MP UFO running in second Flightgear |
| 415 |
instance and check that it too moves at constant speed when replaying. |
| 416 |
''' |
| 417 |
log('') |
| 418 |
log('='*80) |
| 419 |
log('== Record') |
| 420 |
|
| 421 |
aircraft = 'ufo' |
| 422 |
fgfs += ' --prop:bool:/sim/time/simple-time/enabled=true' |
| 423 |
if multiplayer: |
| 424 |
fg = Fg( aircraft, f'{fgfs} --prop:/sim/replay/log-raw-speed-multiplayer=cgdae-t') |
| 425 |
else: |
| 426 |
fg = Fg( aircraft, f'{fgfs}') |
| 427 |
path = f'{g_tapedir}/{fg.aircraft}-continuous.fgtape' |
| 428 |
|
| 429 |
fg.waitfor('/sim/fdm-initialized', 1, timeout=45) |
| 430 |
|
| 431 |
fg.fg['/controls/engines/engine[0]/throttle'] = 0 |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
speed_max = 2000 |
| 436 |
fixed_speed = 100 |
| 437 |
throttle = fixed_speed / speed_max |
| 438 |
|
| 439 |
if multiplayer: |
| 440 |
fg.fg['/sim/replay/record-multiplayer'] = True |
| 441 |
fg2 = Fg( aircraft, f'{fgfs} --callsign=cgdae-t --multiplay=in,4,,5033 --read-only', telnet_port=5501) |
| 442 |
fg2.waitfor('/sim/fdm-initialized', 1, timeout=45) |
| 443 |
fg.fg['/controls/engines/engine[0]/throttle'] = throttle |
| 444 |
fg2.fg['/controls/engines/engine[0]/throttle'] = throttle |
| 445 |
time.sleep(1) |
| 446 |
fgt = fg.fg['/controls/engines/engine[0]/throttle'] |
| 447 |
fg2t = fg2.fg['/controls/engines/engine[0]/throttle'] |
| 448 |
log(f'fgt={fgt} fg2t={fg2t}') |
| 449 |
else: |
| 450 |
fg.fg['/controls/engines/engine[0]/throttle'] = throttle |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
fg.fg['/sim/frame-rate-throttle-hz'] = 5 |
| 456 |
if multiplayer: |
| 457 |
fg2.fg['/sim/frame-rate-throttle-hz'] = 5 |
| 458 |
|
| 459 |
|
| 460 |
time.sleep(10) |
| 461 |
|
| 462 |
|
| 463 |
fg.fg['/sim/replay/record-continuous'] = 1 |
| 464 |
time.sleep(5) |
| 465 |
|
| 466 |
|
| 467 |
fg.fg['/sim/frame-rate-throttle-hz'] = 2 |
| 468 |
time.sleep(5) |
| 469 |
|
| 470 |
|
| 471 |
fg.fg['/sim/frame-rate-throttle-hz'] = 5 |
| 472 |
if multiplayer: |
| 473 |
fg2.fg['/sim/frame-rate-throttle-hz'] = 2 |
| 474 |
time.sleep(5) |
| 475 |
|
| 476 |
|
| 477 |
fg.fg['/sim/replay/record-continuous'] = 0 |
| 478 |
|
| 479 |
fg.close() |
| 480 |
if multiplayer: |
| 481 |
fg2.close() |
| 482 |
time.sleep(2) |
| 483 |
|
| 484 |
path2 = readlink( path) |
| 485 |
log(f'*** path={path} path2={path2}') |
| 486 |
g_cleanup.append(lambda: remove(path2)) |
| 487 |
|
| 488 |
log('') |
| 489 |
log('='*80) |
| 490 |
log('== Replay') |
| 491 |
|
| 492 |
if multiplayer: |
| 493 |
fg = Fg( aircraft, f'{fgfs} --load-tape={path}' |
| 494 |
f' --prop:/sim/replay/log-raw-speed-multiplayer=cgdae-t' |
| 495 |
f' --prop:/sim/replay/log-raw-speed=true' |
| 496 |
) |
| 497 |
else: |
| 498 |
fg = Fg( aircraft, |
| 499 |
f'{fgfs} --load-tape={path} --prop:/sim/replay/log-raw-speed=true', |
| 500 |
|
| 501 |
) |
| 502 |
fg.waitfor('/sim/fdm-initialized', 1, timeout=45) |
| 503 |
fg.fg['/sim/frame-rate-throttle-hz'] = 10 |
| 504 |
fg.waitfor('/sim/replay/replay-state', 1) |
| 505 |
|
| 506 |
time.sleep(3) |
| 507 |
fg.fg['/sim/frame-rate-throttle-hz'] = 2 |
| 508 |
time.sleep(5) |
| 509 |
fg.fg['/sim/frame-rate-throttle-hz'] = 5 |
| 510 |
time.sleep(3) |
| 511 |
fg.fg['/sim/frame-rate-throttle-hz'] = 7 |
| 512 |
|
| 513 |
fg.waitfor('/sim/replay/replay-state-eof', 1) |
| 514 |
|
| 515 |
errors = [] |
| 516 |
def examine_values(infix=''): |
| 517 |
''' |
| 518 |
Looks at /sim/replay/log-raw-speed{infix}-values/value[], which will |
| 519 |
contain measured speed of user/MP UFO. We check that the values are all |
| 520 |
as expected - constant speed. |
| 521 |
''' |
| 522 |
log(f'== Looking at /sim/replay/log-raw-speed{infix}-values/value[]') |
| 523 |
items0 = fg.fg.ls( f'/sim/replay/log-raw-speed{infix}-values') |
| 524 |
log(f'{infix} len(items0)={len(items0)}') |
| 525 |
assert items0, f'Failed to read items in /sim/replay/log-raw-speed{infix}-values/' |
| 526 |
items = [] |
| 527 |
descriptions = [] |
| 528 |
for item in items0: |
| 529 |
if item.name == 'value': |
| 530 |
|
| 531 |
items.append(item) |
| 532 |
elif item.name == 'description': |
| 533 |
descriptions.append(item) |
| 534 |
num_errors = 0 |
| 535 |
for i in range(len(items)-1): |
| 536 |
item = items[i] |
| 537 |
description = '' |
| 538 |
if i < len(descriptions): |
| 539 |
description = descriptions[i].value |
| 540 |
speed = float(item.value) |
| 541 |
prefix = ' ' |
| 542 |
if abs(speed - fixed_speed) > 0.1: |
| 543 |
num_errors += 1 |
| 544 |
prefix = '*' |
| 545 |
log( f' {infix} {prefix} speed={speed:12.4} details: {item}: {description}') |
| 546 |
if num_errors != 0: |
| 547 |
log( f'*** Replay showed uneven speed') |
| 548 |
errors.append('1') |
| 549 |
|
| 550 |
def show_values(paths): |
| 551 |
if isinstance(paths, str): |
| 552 |
paths = paths, |
| 553 |
log(f'Values in {paths}:') |
| 554 |
line2values = dict() |
| 555 |
for i, path in enumerate(paths): |
| 556 |
line = 0 |
| 557 |
for item in fg.fg.ls(path): |
| 558 |
if item.name == 'value': |
| 559 |
line2values.setdefault(line, []).append(item.value) |
| 560 |
line += 1 |
| 561 |
for line in sorted(line2values.keys()): |
| 562 |
t = '' |
| 563 |
for value in line2values[line]: |
| 564 |
t += f' {value}' |
| 565 |
log(f' {t}') |
| 566 |
|
| 567 |
if multiplayer: |
| 568 |
examine_values() |
| 569 |
examine_values('-multiplayer') |
| 570 |
examine_values('-multiplayer-post') |
| 571 |
|
| 572 |
if 0: |
| 573 |
show_values('/sim/replay/log-raw-speed-multiplayer-post-relative-distance') |
| 574 |
show_values('/sim/replay/log-raw-speed-multiplayer-post-relative-bearing') |
| 575 |
show_values('/sim/replay/log-raw-speed-multiplayer-post-absolute-distance') |
| 576 |
show_values('/sim/replay/log-raw-speed-multiplayer-post-user-absolute-distance') |
| 577 |
|
| 578 |
def get_values(path): |
| 579 |
''' |
| 580 |
Returns <path>/value[] as a list. |
| 581 |
''' |
| 582 |
ret = [] |
| 583 |
for item in fg.fg.ls(path): |
| 584 |
if item.name == 'value': |
| 585 |
ret.append(item.value) |
| 586 |
return ret |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
distances_mp = get_values('/sim/replay/log-raw-speed-multiplayer-post-absolute-distance') |
| 596 |
distances_user = get_values('/sim/replay/log-raw-speed-multiplayer-post-user-absolute-distance') |
| 597 |
log(f'len(distances_user)={len(distances_user)} len(distances_mp)={len(distances_mp)}') |
| 598 |
assert len(distances_user) == len(distances_mp) |
| 599 |
assert len(distances_user) > 20 |
| 600 |
for i in range(len(distances_user)): |
| 601 |
distance_mp = distances_mp[i] |
| 602 |
distance_user = distances_user[i] |
| 603 |
delta = distance_mp - distance_user |
| 604 |
if i == 0: |
| 605 |
delta_original = delta |
| 606 |
prefix = ' ' |
| 607 |
if abs(delta - delta_original) > 0.01: |
| 608 |
|
| 609 |
errors.append('1') |
| 610 |
prefix = '*' |
| 611 |
log(f' {prefix} user={distance_user} mp={distance_mp} delta={delta}') |
| 612 |
else: |
| 613 |
examine_values() |
| 614 |
|
| 615 |
fg.close() |
| 616 |
if errors: |
| 617 |
raise Exception('Failure') |
| 618 |
|
| 619 |
log('test_motion() passed') |
| 620 |
|
| 621 |
|
| 622 |
def test_carrier(fgfs): |
| 623 |
''' |
| 624 |
Checks that mp carrier motion is even. |
| 625 |
''' |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
simple_time = 'true' |
| 630 |
fg = Fg( 'harrier-gr3', |
| 631 |
f'{fgfs} --prop:int:/sim/mp-carriers/latch-always=1 --prop:bool:/sim/time/simple-time/enabled={simple_time} --callsign=cgdae3 --airport=ksfo', |
| 632 |
telnet_port=5500, |
| 633 |
telnet_hz=100, |
| 634 |
|
| 635 |
) |
| 636 |
fg.waitfor('/sim/fdm-initialized', 1, timeout=45) |
| 637 |
|
| 638 |
fg_carrier = Fg('Nimitz', |
| 639 |
f'{fgfs} --prop:int:/sim/mp-carriers/latch-always=1 --prop:bool:/sim/time/simple-time/enabled={simple_time} --callsign=cgdae4 --multiplay=in,1,,5033 --read-only', |
| 640 |
telnet_port=5501, |
| 641 |
|
| 642 |
) |
| 643 |
fg_carrier.waitfor('/sim/fdm-initialized', 1, timeout=45) |
| 644 |
|
| 645 |
fg.fg['/sim/replay/log-raw-speed-multiplayer'] = 'cgdae4' |
| 646 |
fg.fg['/sim/log-multiplayer-callsign'] = 'cgdae4' |
| 647 |
|
| 648 |
def get_items(path, leafname, out=None): |
| 649 |
''' |
| 650 |
Finds list of tuples from properties <path>/<leafname>[]/*. Appends new |
| 651 |
items to <out> and returns new items. |
| 652 |
|
| 653 |
Runs rather slowly because telnet commands appear to be throttled. |
| 654 |
''' |
| 655 |
if out is None: |
| 656 |
out = [] |
| 657 |
out_len_original = len(out) |
| 658 |
items = fg.fg.ls(path) |
| 659 |
i = 0 |
| 660 |
for item_i, item in enumerate(items): |
| 661 |
if item.name == leafname: |
| 662 |
if i == len(out): |
| 663 |
|
| 664 |
class Item: |
| 665 |
pass |
| 666 |
item2 = Item() |
| 667 |
item2.i = i |
| 668 |
for j in fg.fg.ls(f'{path}/{leafname}[{item.index}]'): |
| 669 |
setattr( item2, j.name, j) |
| 670 |
out.append(item2) |
| 671 |
i += 1 |
| 672 |
return out[out_len_original:] |
| 673 |
|
| 674 |
t0 = time.time() |
| 675 |
mps = [] |
| 676 |
mppackets = [] |
| 677 |
while 1: |
| 678 |
time.sleep(1) |
| 679 |
t = time.time() - t0 |
| 680 |
log(f'test_carrier(): t={t:.1f}') |
| 681 |
if t > 60: |
| 682 |
print(f'finished, t={t}') |
| 683 |
break |
| 684 |
mps_new = get_items( '/sim/log-multiplayer', 'mp', mps) |
| 685 |
mppackets_new = get_items( '/sim/log-multiplayer', 'mppacket', mppackets) |
| 686 |
for mp in mps_new: |
| 687 |
log(f'test_carrier(): mp: i={mp.i}:' |
| 688 |
f' speed={mp.speed.value:20}' |
| 689 |
f' distance={mp.distance.value:20}' |
| 690 |
f' t={mp.t.value:20}' |
| 691 |
f' dt={mp.dt.value:20}={mp.dt.value*120:20}/120' |
| 692 |
f' ubody={mp.ubody.value:20}' |
| 693 |
f' vbody={mp.vbody.value:20}' |
| 694 |
f' wbody={mp.wbody.value:20}' |
| 695 |
) |
| 696 |
for mppacket in mppackets_new: |
| 697 |
log(f'test_carrier(): mppacket: i={mppacket.i}:' |
| 698 |
f' speed={mppacket.speed.value:20}' |
| 699 |
f' distance={mppacket.distance.value:20}' |
| 700 |
f' t={mppacket.t.value:20}' |
| 701 |
f' linear_vel={mppacket.linear_vel.value:20}' |
| 702 |
f' dt={mppacket.dt.value:20}={mppacket.dt.value*120:20}/120' |
| 703 |
) |
| 704 |
|
| 705 |
|
| 706 |
knots2si = 1852.0/3600 |
| 707 |
speed_expected = 10 * knots2si |
| 708 |
num_incorrect = 0 |
| 709 |
for mp in mps[2:]: |
| 710 |
delta = mp.speed.value - speed_expected |
| 711 |
if abs(delta) > 0.001: |
| 712 |
num_incorrect += 1 |
| 713 |
print(f' * speed={mp.speed.value:20}') |
| 714 |
assert num_incorrect == 0, f'num_incorrect={num_incorrect}' |
| 715 |
fg.close() |
| 716 |
fg_carrier.close() |
| 717 |
|
| 718 |
if __name__ == '__main__': |
| 719 |
|
| 720 |
fgfs = f'./build-walk/fgfs.exe-run.sh' |
| 721 |
fgfs_old = None |
| 722 |
|
| 723 |
do_test = 'all' |
| 724 |
continuous_s = [0, 1, 2] |
| 725 |
extra_properties_s = [0, 1] |
| 726 |
main_view_s = [0, 1] |
| 727 |
multiplayer_s = [0, 1] |
| 728 |
fgfs_reverse_s = [0] |
| 729 |
it_min = None |
| 730 |
it_max = None |
| 731 |
|
| 732 |
if len(sys.argv) == 1: |
| 733 |
do_all = True |
| 734 |
|
| 735 |
args = iter(sys.argv[1:]) |
| 736 |
while 1: |
| 737 |
try: |
| 738 |
arg = next(args) |
| 739 |
except StopIteration: |
| 740 |
break |
| 741 |
if arg == '--all': |
| 742 |
do_all = True |
| 743 |
elif arg == '--carrier': |
| 744 |
do_test = 'carrier' |
| 745 |
elif arg == '--continuous': |
| 746 |
continuous_s = [int(x) for x in next(args).split(',')] |
| 747 |
log(f'continuous_s={continuous_s}') |
| 748 |
elif arg == '--tape-dir': |
| 749 |
g_tapedir = next(args) |
| 750 |
elif arg == '--extra-properties': |
| 751 |
extra_properties_s = [int(x) for x in next(args).split(',')] |
| 752 |
elif arg == '--it-max': |
| 753 |
it_max = int(next(args)) |
| 754 |
elif arg == '--it-min': |
| 755 |
it_min = int(next(args)) |
| 756 |
elif arg == '--main-view': |
| 757 |
main_view_s = [int(x) for x in next(args).split(',')] |
| 758 |
elif arg == '--multiplayer': |
| 759 |
multiplayer_s = [int(x) for x in next(args).split(',')] |
| 760 |
elif arg == '-f': |
| 761 |
fgfs = next(args) |
| 762 |
elif arg == '--f-old': |
| 763 |
fgfs_old = next(args) |
| 764 |
fgfs_reverse = [0, 1] |
| 765 |
elif arg == '--test-motion': |
| 766 |
do_test = 'motion' |
| 767 |
elif arg == '--test-motion-mp': |
| 768 |
do_test = 'motion-mp' |
| 769 |
else: |
| 770 |
raise Exception(f'Unrecognised arg: {arg!r}') |
| 771 |
|
| 772 |
g_tapedir = os.path.abspath(g_tapedir) |
| 773 |
os.makedirs( g_tapedir, exist_ok=True) |
| 774 |
if 0: |
| 775 |
pass |
| 776 |
elif do_test == 'carrier': |
| 777 |
test_carrier(fgfs) |
| 778 |
elif do_test == 'motion': |
| 779 |
test_motion( fgfs) |
| 780 |
elif do_test == 'motion-mp': |
| 781 |
test_motion( fgfs, True) |
| 782 |
elif do_test == 'all': |
| 783 |
try: |
| 784 |
if fgfs_old: |
| 785 |
for fgfs1, fgfs2 in [(fgfs, fgfs_old), (fgfs_old, fgfs)]: |
| 786 |
for multiplayer in 0, 1: |
| 787 |
test_record_replay( |
| 788 |
fgfs1, |
| 789 |
fgfs2, |
| 790 |
multiplayer, |
| 791 |
continuous=0, |
| 792 |
extra_properties=0, |
| 793 |
main_view=0, |
| 794 |
length=10, |
| 795 |
) |
| 796 |
else: |
| 797 |
log(f'continuous_s={continuous_s}') |
| 798 |
its_max = len(multiplayer_s) * len(continuous_s) * len(extra_properties_s) * len(main_view_s) * len(fgfs_reverse_s) |
| 799 |
it = 0 |
| 800 |
for multiplayer in multiplayer_s: |
| 801 |
for continuous in continuous_s: |
| 802 |
for extra_properties in extra_properties_s: |
| 803 |
for main_view in main_view_s: |
| 804 |
for fgfs_reverse in fgfs_reverse_s: |
| 805 |
if fgfs_reverse: |
| 806 |
fgfs_save = fgfs_old |
| 807 |
fgfs_load = fgfs |
| 808 |
else: |
| 809 |
fgfs_save = fgfs |
| 810 |
fgfs_load = fgfs_old |
| 811 |
|
| 812 |
ok = True |
| 813 |
if it_min is not None: |
| 814 |
if it < it_min: |
| 815 |
ok = False |
| 816 |
if it_max is not None: |
| 817 |
if it >= it_max: |
| 818 |
ok = False |
| 819 |
log('') |
| 820 |
log(f'===') |
| 821 |
log(f'=== {it}/{its_max}') |
| 822 |
if ok: |
| 823 |
test_record_replay( |
| 824 |
fgfs_save, |
| 825 |
fgfs_load, |
| 826 |
multiplayer=multiplayer, |
| 827 |
continuous=continuous, |
| 828 |
extra_properties=extra_properties, |
| 829 |
main_view=main_view, |
| 830 |
length=10 |
| 831 |
) |
| 832 |
it += 1 |
| 833 |
finally: |
| 834 |
pass |
| 835 |
else: |
| 836 |
assert 0, f'do_test={do_test}' |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
for f in g_cleanup: |
| 842 |
try: |
| 843 |
f() |
| 844 |
except Exception: |
| 845 |
pass |
| 846 |
|
| 847 |
log(f'{__file__}: Returning 0') |