[Tests] Make file priority test more consistent.

Our file priority test was using time.sleep to wait until libtorrent
had processed the command. This was sometimes not long enough and the
test would fail. On libtorrent 2.0.3+ there is an alert when the
process has finished, switch to waiting for that in this test to make
the test more consistent. On older libtorrent, make the delay a bit
longer, to try to make the test more consistent there as well.

Closes: https://github.com/deluge-torrent/deluge/pull/373
This commit is contained in:
Chase Sterling 2022-02-11 11:44:29 -05:00 committed by Calum Lind
parent dabb505376
commit 374997a8d7
No known key found for this signature in database
GPG Key ID: 90597A687B836BA3
1 changed files with 15 additions and 2 deletions

View File

@ -10,6 +10,7 @@ from base64 import b64encode
from unittest import mock
import pytest
import pytest_twisted
from twisted.internet import defer, reactor
from twisted.internet.task import deferLater
@ -80,7 +81,19 @@ class TestTorrent(BaseTestCase):
}
return atp
def test_set_file_priorities(self):
@pytest_twisted.ensureDeferred
async def test_set_file_priorities(self):
if getattr(lt, 'file_prio_alert', None):
# Libtorrent 2.0.3 and later has a file_prio_alert
prios_set = defer.Deferred()
prios_set.addTimeout(1.5, reactor)
component.get('AlertManager').register_handler(
'file_prio_alert', lambda a: prios_set.callback(True)
)
else:
# On older libtorrent, we just wait a while
prios_set = deferLater(reactor, 0.8)
atp = self.get_torrent_atp('dir_with_6_files.torrent')
handle = self.session.add_torrent(atp)
torrent = Torrent(handle, {})
@ -95,7 +108,7 @@ class TestTorrent(BaseTestCase):
# Test with handle.piece_priorities as handle.file_priorities async
# updates and will return old value. Also need to remove a priority
# value as one file is much smaller than piece size so doesn't show.
time.sleep(0.6) # Delay to wait for alert from lt
await prios_set # Delay to wait for alert from lt
piece_prio = handle.piece_priorities()
result = all(p in piece_prio for p in [3, 2, 0, 5, 6, 7])
assert result