From de570ae53618b4beb3dca53efdc13ce6152c175e Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Mon, 29 May 2023 12:05:56 +0100 Subject: [PATCH] [Tests] Fix waiting for lt alert state change Tests are fragile when waiting for lt alert so allow incrementally waiting for status change, along with a timeout. --- deluge/tests/test_torrent.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/deluge/tests/test_torrent.py b/deluge/tests/test_torrent.py index 8cd2be992..62886159e 100644 --- a/deluge/tests/test_torrent.py +++ b/deluge/tests/test_torrent.py @@ -66,9 +66,22 @@ class TestTorrent(BaseTestCase): print(tmp) def assert_state(self, torrent, state): + """Assert torrent state matches expected state""" torrent.update_state() assert torrent.state == state + def assert_state_wait(self, torrent, expected, timeout=1, interval=0.2): + """Assert state but retry with timeout e.g. Allow for async lt alerts""" + start = time.time() + + while time.time() - start < timeout: + torrent.update_state() + time.sleep(interval) + if torrent.state == expected: + break + else: + assert torrent.state == expected + def get_torrent_atp(self, filename): filename = common.get_test_data_file(filename) with open(filename, 'rb') as _file: @@ -193,13 +206,12 @@ class TestTorrent(BaseTestCase): torrent_id = self.core.add_torrent_file(filename, filedump, options) torrent = self.core.torrentmanager.torrents[torrent_id] - # time.sleep(0.5) # Delay to wait for lt to finish check on Travis. - # self.assert_state(torrent, 'Seeding') + # Inital check will fail and return to download state + self.assert_state_wait(torrent, 'Downloading') # Force an error by reading (non-existant) piece from disk torrent.handle.read_piece(0) - time.sleep(0.2) # Delay to wait for alert from lt - self.assert_state(torrent, 'Error') + self.assert_state_wait(torrent, 'Error') def test_torrent_error_resume_original_state(self): options = {'seed_mode': True, 'add_paused': True} @@ -214,8 +226,7 @@ class TestTorrent(BaseTestCase): # Force an error by reading (non-existant) piece from disk torrent.handle.read_piece(0) - time.sleep(0.2) # Delay to wait for alert from lt - self.assert_state(torrent, 'Error') + self.assert_state_wait(torrent, 'Error') # Clear error and verify returned to original state torrent.force_recheck()