Skip to content

Commit

Permalink
Fix ZeroDivisionError in progress bar callback
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky authored and ichorid committed Dec 5, 2020
1 parent 798b1ef commit 0c841fd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/tribler-core/tribler_core/upgrade/db8_to_db10.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def fts_callback_handler():


def calc_progress(duration_now, duration_half=60.0):
result = 100 * ( 1 - 1 / (1 + duration_now / duration_half) ** 2)
result = 100 * ( 1 - 1 / (1 + duration_now / (duration_half + 1)) ** 2)
return result


Expand Down
17 changes: 17 additions & 0 deletions src/tribler-core/tribler_core/upgrade/tests/test_upgrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from tribler_core.modules.metadata_store.orm_bindings.channel_metadata import CHANNEL_DIR_NAME_LENGTH
from tribler_core.modules.metadata_store.store import CURRENT_DB_VERSION, MetadataStore
from tribler_core.tests.tools.common import TESTS_DATA_DIR
from tribler_core.upgrade.db8_to_db10 import calc_progress
from tribler_core.upgrade.upgrade import cleanup_noncompliant_channel_torrents
from tribler_core.utilities.configparser import CallbackConfigParser

Expand Down Expand Up @@ -154,3 +155,19 @@ async def test_upgrade_pony_8to10(upgrader, session):
assert int(mds.MiscData.get(name="db_version").value) == CURRENT_DB_VERSION
assert mds.ChannelNode.select().count() == 23
mds.shutdown()

def test_calc_progress():
EPSILON = 0.001
assert calc_progress(0) == pytest.approx(0.0, abs=EPSILON)
assert calc_progress(0, 1) == pytest.approx(0.0, abs=EPSILON)

assert calc_progress(1, 0) == pytest.approx(75.0, abs=EPSILON)
assert calc_progress(10, 0) == pytest.approx(99.173553, abs=EPSILON)

assert calc_progress(0, 100) == pytest.approx(0.0, abs=EPSILON)
assert calc_progress(10, 100) == pytest.approx(17.206395, abs=EPSILON)
assert calc_progress(50, 100) == pytest.approx(55.260734, abs=EPSILON)
assert calc_progress(80, 100) == pytest.approx(68.862366, abs=EPSILON)
assert calc_progress(100, 100) == pytest.approx(74.750624, abs=EPSILON)
assert calc_progress(200, 100) == pytest.approx(88.740742, abs=EPSILON)
assert calc_progress(1000, 100) == pytest.approx(99.158472, abs=EPSILON)

0 comments on commit 0c841fd

Please sign in to comment.