Compare commits

...
3 changed files with 28 additions and 7 deletions

No files matched your search

+3 -1
View File
@@ -22,7 +22,9 @@ a bounded subprocess so library failures cannot crash the importer or client.
The resulting snapshot is stored under that account's `userdata/<id>/config` and The resulting snapshot is stored under that account's `userdata/<id>/config` and
refreshed every 30 seconds while logged in. Title lookups retry every five minutes and refreshed every 30 seconds while logged in. Title lookups retry every five minutes and
when the source changes, so an early network failure does not end the watcher. Changed when the source changes, so an early network failure does not end the watcher. Changed
status and probe failures are written to the session log. Startup and post-exit import status and probe failures are written to the session log. While a game runs, the check still runs until it has
succeeded once for the current folders, so a game started right after the client does not keep its owned titles
unconfirmed for the whole session. Startup and post-exit import
use the last successful snapshot for the selected account. The client reads manifests use the last successful snapshot for the selected account. The client reads manifests
and shortcuts only at start, so when a snapshot confirms owned titles that are not yet and shortcuts only at start, so when a snapshot confirms owned titles that are not yet
imported, the watcher restarts the client once (not while a game runs or a download is in imported, the watcher restarts the client once (not while a game runs or a download is in
@@ -236,7 +236,7 @@ def watch(steam_root, listing):
candidates, sources = {}, {} candidates, sources = {}, {}
next_lookup = 0 next_lookup = 0
last_status = None last_status = None
probed, next_probe, failures = None, 0, 0 probed, next_probe, failures, confirmed = None, 0, 0, None
pending, requested, account = [], [], None pending, requested, account = [], [], None
while True: while True:
now = time.monotonic() now = time.monotonic()
@@ -254,7 +254,7 @@ def watch(steam_root, listing):
next_lookup = now + 300 next_lookup = now + 300
status = "waiting for a matching Steam title" status = "waiting for a matching Steam title"
key = (dict(candidates), dict(sources)) key = (dict(candidates), dict(sources))
if candidates and (key != probed or now >= next_probe) and game_running(): if candidates and (key != probed or now >= next_probe) and key == confirmed and game_running():
status = last_status status = last_status
elif candidates and (key != probed or now >= next_probe): elif candidates and (key != probed or now >= next_probe):
probed, next_probe = key, now + 300 probed, next_probe = key, now + 300
@@ -278,6 +278,7 @@ def watch(steam_root, listing):
save_json(Path(steam_root) / "userdata" / snapshot["account"] / "config" / STATE, snapshot) save_json(Path(steam_root) / "userdata" / snapshot["account"] / "config" / STATE, snapshot)
pending = unimported(steam_root, snapshot, candidates) pending = unimported(steam_root, snapshot, candidates)
account = snapshot["account"] account = snapshot["account"]
confirmed = key
status = "matched %d/%d games; %d owned" % (len(candidates), len(games), sum(snapshot["owned"].values())) status = "matched %d/%d games; %d owned" % (len(candidates), len(games), sum(snapshot["owned"].values()))
except (OSError, ValueError, KeyError, StopIteration, subprocess.TimeoutExpired) as error: except (OSError, ValueError, KeyError, StopIteration, subprocess.TimeoutExpired) as error:
status = "ownership check unavailable: " + str(error) status = "ownership check unavailable: " + str(error)
+22 -4
View File
@@ -306,7 +306,7 @@ class ImportsTest(unittest.TestCase):
snapshot = imports.read_json(self.acct / 'config' / imports.STATE, {}) snapshot = imports.read_json(self.acct / 'config' / imports.STATE, {})
self.assertTrue(snapshot['owned']['42']) self.assertTrue(snapshot['owned']['42'])
def run_watch(self, beats, routes=None, busy=False, answer=True, started=False): def run_watch(self, beats, routes=None, busy=False, answer=True, started=False, running=False, times=None, results=None):
listing = self.root / 'games.json' listing = self.root / 'games.json'
listing.write_text(json.dumps([self.game])) listing.write_text(json.dumps([self.game]))
if routes is not None: if routes is not None:
@@ -317,19 +317,37 @@ class ImportsTest(unittest.TestCase):
idle = imports.downloads_idle idle = imports.downloads_idle
result = Mock(returncode=0, stdout='DROIDDECK_OWNERSHIP=' + json.dumps(dict(account='123', owned={'42': True}))) result = Mock(returncode=0, stdout='DROIDDECK_OWNERSHIP=' + json.dumps(dict(account='123', owned={'42': True})))
with patch.object(imports, 'identify', return_value=42), \ with patch.object(imports, 'identify', return_value=42), \
patch.object(imports, 'game_running', return_value=False), \ patch.object(imports, 'game_running', return_value=running), \
patch.object(imports, 'request_restart') as restart, \ patch.object(imports, 'request_restart') as restart, \
patch.object(imports, 'ask_restart', return_value=answer) as asked, \ patch.object(imports, 'ask_restart', return_value=answer) as asked, \
patch.object(imports, 'downloads_idle', side_effect=lambda root: idle(root) and not (started and asked.called)), \ patch.object(imports, 'downloads_idle', side_effect=lambda root: idle(root) and not (started and asked.called)), \
patch.object(imports, 'time', Mock(monotonic=Mock(side_effect=list(range(0, 30 * beats, 30))), patch.object(imports, 'time', Mock(monotonic=Mock(side_effect=times or list(range(0, 30 * beats, 30))),
sleep=Mock(side_effect=[None] * (beats - 1) + [RuntimeError('stop')]))), \ sleep=Mock(side_effect=[None] * (beats - 1) + [RuntimeError('stop')]))), \
patch.object(imports.subprocess, 'run', return_value=result), \ patch.object(imports.subprocess, 'run', side_effect=results or [result] * beats) as probes, \
patch('builtins.print'): patch('builtins.print'):
with self.assertRaisesRegex(RuntimeError, 'stop'): with self.assertRaisesRegex(RuntimeError, 'stop'):
imports.watch(self.steam, listing) imports.watch(self.steam, listing)
self.asked = asked self.asked = asked
self.probes = probes
return restart return restart
def test_ownership_is_checked_while_a_game_runs_until_it_is_known(self):
unavailable = Mock(returncode=1, stderr='Steam pipe unavailable', stdout='')
owned = Mock(returncode=0, stdout='DROIDDECK_OWNERSHIP=' + json.dumps(dict(account='123', owned={'42': True})))
restart = self.run_watch(3, running=True, times=[0, 30, 90], results=[unavailable, owned])
self.assertEqual(2, self.probes.call_count)
self.assertTrue(imports.read_json(self.acct / 'config' / imports.STATE, {})['owned']['42'])
self.assertEqual(0, restart.call_count)
self.assertEqual(0, self.asked.call_count)
def test_known_ownership_is_not_checked_again_while_a_game_runs(self):
self.run_watch(3, running=True, times=[0, 301, 602])
self.assertEqual(1, self.probes.call_count)
def test_known_ownership_is_checked_again_when_no_game_runs(self):
self.run_watch(3, answer=False, times=[0, 301, 602])
self.assertEqual(3, self.probes.call_count)
def test_newly_owned_game_restarts_the_client_once(self): def test_newly_owned_game_restarts_the_client_once(self):
self.assertEqual(1, self.run_watch(3).call_count) self.assertEqual(1, self.run_watch(3).call_count)