[Core] Fix get_eta returning float instead of int

Floor division will return a float if a float is provided so ensure int
when dividing by the stop_ratio. All other status values from libtorrent
are ints.

Added tests.
This commit is contained in:
Calum Lind 2018-07-16 16:09:51 +01:00
parent b2e1f850d8
commit 1e6c02ae83
2 changed files with 47 additions and 3 deletions

View File

@ -699,11 +699,15 @@ class Torrent(object):
"""
status = self.status
eta = 0
if self.is_finished and self.options['stop_at_ratio'] and status.upload_payload_rate:
if (
self.is_finished
and self.options['stop_at_ratio']
and status.upload_payload_rate
):
# We're a seed, so calculate the time to the 'stop_share_ratio'
eta = (
(status.all_time_download * self.options['stop_ratio']) -
status.all_time_upload
int(status.all_time_download * self.options['stop_ratio'])
- status.all_time_upload
) // status.upload_payload_rate
elif status.download_payload_rate:
left = status.total_wanted - status.total_wanted_done

View File

@ -11,6 +11,7 @@ import os
import time
from base64 import b64encode
import mock
from twisted.internet import reactor
from twisted.internet.task import deferLater
from twisted.trial import unittest
@ -206,3 +207,42 @@ class TorrentTestCase(BaseTestCase):
self.assertEqual(tm_resume_data, resume_data)
return deferLater(reactor, 0.5, assert_resume_data)
def test_get_eta_seeding(self):
atp = self.get_torrent_atp('test_torrent.file.torrent')
handle = self.session.add_torrent(atp)
self.torrent = Torrent(handle, {})
self.assertEqual(self.torrent.get_eta(), 0)
self.torrent.status = mock.MagicMock()
self.torrent.status.upload_payload_rate = 5000
self.torrent.status.download_payload_rate = 0
self.torrent.status.all_time_download = 10000
self.torrent.status.all_time_upload = 500
self.torrent.is_finished = True
self.torrent.options = {'stop_at_ratio': False}
# Test finished and uploading but no stop_at_ratio set.
self.assertEqual(self.torrent.get_eta(), 0)
self.torrent.options = {
'stop_at_ratio': True,
'stop_ratio': 1.5,
}
result = self.torrent.get_eta()
self.assertEqual(result, 2)
self.assertIsInstance(result, int)
def test_get_eta_downloading(self):
atp = self.get_torrent_atp('test_torrent.file.torrent')
handle = self.session.add_torrent(atp)
self.torrent = Torrent(handle, {})
self.assertEqual(self.torrent.get_eta(), 0)
self.torrent.status = mock.MagicMock()
self.torrent.status.download_payload_rate = 50
self.torrent.status.total_wanted = 10000
self.torrent.status.total_wanted_done = 5000
result = self.torrent.get_eta()
self.assertEqual(result, 100)
self.assertIsInstance(result, int)