Skip to content

Commit

Permalink
Update machine state on unhandled exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Sep 1, 2022
1 parent 3066a9a commit 17bbfcf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions plover/machine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,18 @@ class ThreadedStenotypeBase(StenotypeBase, threading.Thread):
"""
def __init__(self):
threading.Thread.__init__(self)
self._on_unhandled_exception(self._error)
self.name += '-machine'
StenotypeBase.__init__(self)
self.finished = threading.Event()

def _on_unhandled_exception(self, action):
super_invoke_excepthook = self._invoke_excepthook
def invoke_excepthook(self):
action()
super_invoke_excepthook(self)
self._invoke_excepthook = invoke_excepthook

def run(self):
"""This method should be overridden by a subclass."""
pass
Expand Down
14 changes: 14 additions & 0 deletions test/test_machine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from unittest.mock import Mock
from plover.machine.base import ThreadedStenotypeBase

class MyMachine(ThreadedStenotypeBase):
def run(self):
raise "some unexpected error"

def test_update_machine_staten_on_unhandled_exception():
machine = MyMachine()
callback = Mock()
machine.add_state_callback(callback)
machine.start_capture()
machine.join()
callback.assert_called_with('disconnected')

0 comments on commit 17bbfcf

Please sign in to comment.