[Component] Add pause and resume events methods

For future use add ability to handle pause and resume events

Co-authored-by: Calum Lind <calumlind+deluge@gmail.com>
This commit is contained in:
DjLegolas 2023-03-05 15:56:34 +00:00 committed by Calum Lind
parent c38b4c72d0
commit 25a2b113e2
No known key found for this signature in database
GPG Key ID: 90597A687B836BA3
2 changed files with 36 additions and 7 deletions

View File

@ -64,6 +64,11 @@ class Component:
paused by instructing the :class:`ComponentRegistry` to pause
this Component.
**pause()** - This method is called when the component is being paused.
**resume()** - This method is called when the component resumes from a Paused
state.
**shutdown()** - This method is called when the client is exiting. If the
Component is in a "Started" state when this is called, a
call to stop() will be issued prior to shutdown().
@ -175,13 +180,12 @@ class Component:
def _component_pause(self):
def on_pause(result):
self._component_state = 'Paused'
if self._component_timer and self._component_timer.running:
self._component_timer.stop()
if self._component_state == 'Started':
if self._component_timer and self._component_timer.running:
d = maybeDeferred(self._component_timer.stop)
d.addCallback(on_pause)
else:
d = succeed(None)
d = maybeDeferred(self.pause)
d.addCallback(on_pause)
elif self._component_state == 'Paused':
d = succeed(None)
else:
@ -198,9 +202,10 @@ class Component:
def _component_resume(self):
def on_resume(result):
self._component_state = 'Started'
self._component_start_timer()
if self._component_state == 'Paused':
d = maybeDeferred(self._component_start_timer)
d = maybeDeferred(self.resume)
d.addCallback(on_resume)
else:
d = fail(
@ -236,6 +241,12 @@ class Component:
def shutdown(self):
pass
def pause(self):
pass
def resume(self):
pass
class ComponentRegistry:
"""The ComponentRegistry holds a list of currently registered :class:`Component` objects.

View File

@ -17,7 +17,7 @@ import deluge.component as component
class ComponentTester(component.Component):
def __init__(self, name, depend=None):
super().__init__(name, depend=depend)
event_methods = ('start', 'update', 'stop', 'shutdown')
event_methods = ('start', 'update', 'pause', 'resume', 'stop', 'shutdown')
for event_method in event_methods:
setattr(self, event_method, Mock())
@ -145,9 +145,27 @@ class TestComponent:
await component.pause(['test_pause'])
assert c._component_state == 'Paused'
assert c.pause.call_count == 1
assert c.update.call_count != init_update_count
assert not c._component_timer.running
async def test_resume(self):
c = ComponentTester('test_resume')
await component.start(['test_resume'])
assert c._component_state == 'Started'
await component.pause(['test_resume'])
assert c._component_state == 'Paused'
await component.resume(['test_resume'])
assert c._component_state == 'Started'
assert c.resume.call_count == 1
assert c._component_timer.running
async def test_component_start_error(self):
ComponentTester('test_start_error')
await component.start(['test_start_error'])