From 25a2b113e290600043338e22b4f116cd2dbf2886 Mon Sep 17 00:00:00 2001 From: DjLegolas Date: Sun, 5 Mar 2023 15:56:34 +0000 Subject: [PATCH] [Component] Add pause and resume events methods For future use add ability to handle pause and resume events Co-authored-by: Calum Lind --- deluge/component.py | 23 +++++++++++++++++------ deluge/tests/test_component.py | 20 +++++++++++++++++++- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/deluge/component.py b/deluge/component.py index 585cd04bb..fec510903 100644 --- a/deluge/component.py +++ b/deluge/component.py @@ -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. diff --git a/deluge/tests/test_component.py b/deluge/tests/test_component.py index 7655eef11..ea492565a 100644 --- a/deluge/tests/test_component.py +++ b/deluge/tests/test_component.py @@ -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'])