diff --git a/deluge/core/torrent.py b/deluge/core/torrent.py index e9d3e540c..9139cf972 100644 --- a/deluge/core/torrent.py +++ b/deluge/core/torrent.py @@ -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 diff --git a/deluge/tests/test_torrent.py b/deluge/tests/test_torrent.py index 4d54c2659..773dd8b04 100644 --- a/deluge/tests/test_torrent.py +++ b/deluge/tests/test_torrent.py @@ -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)