From 36b96935c63dbddf716e3edd6a87644b66891727 Mon Sep 17 00:00:00 2001 From: sezanzeb Date: Thu, 29 Apr 2021 22:22:05 +0200 Subject: [PATCH] #107 fixed autoload --- keymapper/daemon.py | 7 ++----- keymapper/injection/injector.py | 2 +- keymapper/injection/macros.py | 14 +++++++------- tests/test.py | 8 ++++---- tests/testcases/test_daemon.py | 23 +++++++++++++++++++++++ 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/keymapper/daemon.py b/keymapper/daemon.py index e1cb133f..1f8b5fdc 100644 --- a/keymapper/daemon.py +++ b/keymapper/daemon.py @@ -361,11 +361,8 @@ class Daemon: logger.error('No presets configured to autoload') return - for device_key, _ in autoload_presets: - group = groups.find(key=device_key) - if group is None: - continue - self._autoload(group.key) + for group_key, _ in autoload_presets: + self._autoload(group_key) def start_injecting(self, group_key, preset): """Start injecting the preset for the device. diff --git a/keymapper/injection/injector.py b/keymapper/injection/injector.py index 77f7ad02..8b6dcb49 100644 --- a/keymapper/injection/injector.py +++ b/keymapper/injection/injector.py @@ -432,7 +432,7 @@ class Injector(multiprocessing.Process): events, so ideally they should be copied from source. """ logger.debug( - 'Started consumer to inject to %s, fd %s', + 'Started consumer to inject for %s, fd %s', source.path, source.fd ) diff --git a/keymapper/injection/macros.py b/keymapper/injection/macros.py index ce3bb6ed..5c61ce52 100644 --- a/keymapper/injection/macros.py +++ b/keymapper/injection/macros.py @@ -62,6 +62,10 @@ class SharedDict: atexit.register(self._stop) self._start() + # To avoid blocking forever if something goes wrong. The maximum + # observed time communication takes was 0.001 for me on a slow pc + self._timeout = 0.02 + def _start(self): """Ensure the process to manage the dictionary is running.""" if self.process is not None and self.process.is_alive(): @@ -99,10 +103,10 @@ class SharedDict: """Get a value from the dictionary.""" return self.__getitem__(key) - def is_alive(self): + def is_alive(self, timeout=None): """Check if the manager process is running.""" self.pipe[1].send(('ping',)) - select.select([self.pipe[1]], [], [], 0.1) + select.select([self.pipe[1]], [], [], timeout or self._timeout) if self.pipe[1].poll(): return self.pipe[1].recv() == 'pong' @@ -114,11 +118,7 @@ class SharedDict: def __getitem__(self, key): self.pipe[1].send(('get', key)) - # To avoid blocking forever if something goes wrong. - # The maximum observed time this takes was 0.001 for me on a slow pc - timeout = 0.02 - - select.select([self.pipe[1]], [], [], timeout) + select.select([self.pipe[1]], [], [], self._timeout) if self.pipe[1].poll(): return self.pipe[1].recv() diff --git a/tests/test.py b/tests/test.py index 3ba7d868..51295b02 100644 --- a/tests/test.py +++ b/tests/test.py @@ -559,6 +559,9 @@ def quick_cleanup(log=True): for task in asyncio.all_tasks(): task.cancel() + if not macro_variables.process.is_alive(): + raise AssertionError('the SharedDict manager is not running anymore') + macro_variables._stop() join_children() @@ -579,9 +582,6 @@ def quick_cleanup(log=True): clear_write_history() - if not macro_variables.process.is_alive(): - raise AssertionError('the SharedDict manager is not running anymore') - for name in list(uinputs.keys()): del uinputs[name] @@ -606,7 +606,7 @@ def quick_cleanup(log=True): for _, pipe in pending_events.values(): assert not pipe.poll() - assert macro_variables.is_alive() + assert macro_variables.is_alive(1) def cleanup(): diff --git a/tests/testcases/test_daemon.py b/tests/testcases/test_daemon.py index 287f3bd9..843a5ade 100644 --- a/tests/testcases/test_daemon.py +++ b/tests/testcases/test_daemon.py @@ -475,6 +475,29 @@ class TestDaemon(unittest.TestCase): self.assertEqual(len(history), 1) self.assertEqual(history[group.key][1], preset) + def test_autoload_3(self): + # based on a bug + preset = 'preset7' + group = groups.find(key='Foo Device 2') + + mapping = Mapping() + mapping.change(Key(3, 2, 1), 'a') + mapping.save(group.get_preset_path(preset)) + + config.set_autoload_preset(group.key, preset) + config.save_config() + + self.daemon = Daemon() + self.daemon.set_config_dir(get_config_path()) + groups.set_groups([]) + self.daemon.autoload() + + # it should try to refresh the groups because one of the + # group_keys is unknown at the moment + history = self.daemon.autoload_history._autoload_history + self.assertEqual(history[group.key][1], preset) + self.assertEqual(self.daemon.get_state(group.key), STARTING) + if __name__ == "__main__": unittest.main()