Compare commits

...
Author SHA1 Message Date
Claudio Cambra fac58e9bc3 fix: prevent overflow on progress ETA
Assisted-by: Codex:GPT-6
Signed-off-by: Claudio Cambra <[email protected]>
2026-11-02 17:13:45 +08:00
3 changed files with 151 additions and 1 deletions

No files matched your search

+8 -1
View File
@@ -10,6 +10,8 @@
#include <QMetaType>
#include <QCoreApplication>
#include <limits>
namespace OCC {
ProgressDispatcher *ProgressDispatcher::_instance = nullptr;
@@ -375,7 +377,12 @@ ProgressInfo::Estimates ProgressInfo::Progress::estimates() const
Estimates est{};
est.estimatedBandwidth = _progressPerSec;
if (_progressPerSec != 0) {
est.estimatedEta = qRound64(static_cast<double>(_total - _completed) / _progressPerSec) * 1000;
constexpr auto millisecondsPerSecond = 1000;
constexpr auto maxEta = std::numeric_limits<qint64>::max();
constexpr auto maxSeconds = static_cast<double>(maxEta / millisecondsPerSecond);
const auto seconds = static_cast<double>(_total - _completed) / _progressPerSec;
// The strict bound also accounts for rounding the limit to double.
est.estimatedEta = seconds < maxSeconds ? qRound64(seconds) * millisecondsPerSecond : maxEta;
} else {
est.estimatedEta = 0; // looks better than qint64 max
}
+1
View File
@@ -48,6 +48,7 @@ nextcloud_add_test(NetrcParser)
nextcloud_add_test(OwnSql)
nextcloud_add_test(SyncJournalDB)
nextcloud_add_test(SyncFileItem)
nextcloud_add_test(ProgressDispatcher)
nextcloud_add_test(ConcatUrl)
nextcloud_add_test(Cookies)
nextcloud_add_test(XmlParse)
+142
View File
@@ -0,0 +1,142 @@
/*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "progressdispatcher.h"
#include <QtTest>
#include <limits>
using namespace OCC;
using namespace Qt::StringLiterals;
class TestProgressDispatcher : public QObject
{
Q_OBJECT
static SyncFileItem makeItem(qint64 size)
{
auto item = SyncFileItem{};
item._file = u"file"_s;
item._type = ItemTypeFile;
item._instruction = CSYNC_INSTRUCTION_NEW;
item._size = size;
return item;
}
static void updateEstimates(ProgressInfo &progress, int count = 1)
{
for (auto tick = 0; tick < count; ++tick) {
QVERIFY(QMetaObject::invokeMethod(&progress, "updateEstimates", Qt::DirectConnection));
}
}
static constexpr auto maximumEta = quint64(std::numeric_limits<qint64>::max());
private Q_SLOTS:
void estimates_data()
{
QTest::addColumn<qint64>("total");
QTest::addColumn<qint64>("completed");
QTest::addColumn<quint64>("expectedEta");
const auto maximumSeconds = std::numeric_limits<qint64>::max() / 1000;
QTest::newRow("zero-rate") << qint64(100) << qint64(0) << quint64(0);
QTest::newRow("round-down") << qint64(24) << qint64(10) << quint64(1000);
QTest::newRow("round-up") << qint64(25) << qint64(10) << quint64(2000);
QTest::newRow("completed") << qint64(100) << qint64(100) << quint64(0);
QTest::newRow("large-representable") << qint64(9000000000000001) << qint64(1) << quint64(9000000000000000000);
QTest::newRow("below-limit") << maximumSeconds << qint64(1) << quint64((maximumSeconds - 1) * 1000);
QTest::newRow("rounded-to-limit") << maximumSeconds + 1 << qint64(1) << maximumEta;
QTest::newRow("millisecond-overflow") << qint64(10000000000000001) << qint64(1) << maximumEta;
QTest::newRow("second-overflow") << std::numeric_limits<qint64>::max() << qint64(1) << maximumEta;
}
void estimates()
{
QFETCH(qint64, total);
QFETCH(qint64, completed);
QFETCH(quint64, expectedEta);
auto progress = ProgressInfo{};
const auto item = makeItem(total);
progress.adjustTotalsForFile(item);
progress.setProgressItem(item, completed);
updateEstimates(progress);
const auto estimate = progress.fileProgress(item);
QCOMPARE(estimate.estimatedBandwidth, completed);
QCOMPARE(estimate.estimatedEta, expectedEta);
}
void stalledTransfer_data()
{
QTest::addColumn<int>("idleUpdates");
QTest::newRow("millisecond-overflow") << 254;
QTest::newRow("second-overflow") << 319;
QTest::newRow("infinite-seconds") << 8000;
}
void stalledTransfer()
{
QFETCH(int, idleUpdates);
auto progress = ProgressInfo{};
const auto item = makeItem(1001000000);
progress.adjustTotalsForFile(item);
progress.setProgressItem(item, 1000000);
updateEstimates(progress);
updateEstimates(progress, idleUpdates);
QCOMPARE(progress.fileProgress(item).estimatedEta, maximumEta);
QVERIFY(progress.totalProgress().estimatedEta >= maximumEta);
QVERIFY(!progress.trustEta());
progress.setProgressItem(item, 2000000);
updateEstimates(progress);
const auto resumed = progress.totalProgress();
QVERIFY(resumed.estimatedBandwidth > 0);
QVERIFY(resumed.estimatedEta > 0);
QVERIFY(resumed.estimatedEta < maximumEta);
QVERIFY(progress.trustEta());
progress.setProgressComplete(item);
updateEstimates(progress);
QCOMPARE(progress.totalProgress().estimatedEta, quint64(0));
QCOMPARE(progress.completedSize(), progress.totalSize());
QCOMPARE(progress.completedFiles(), progress.totalFiles());
}
void stalledFileCount()
{
auto progress = ProgressInfo{};
auto completed = makeItem(0);
completed._instruction = CSYNC_INSTRUCTION_RENAME;
auto pending = completed;
pending._file = u"pending"_s;
pending._affectedItems = 10;
progress.adjustTotalsForFile(completed);
progress.adjustTotalsForFile(pending);
progress.setProgressComplete(completed);
updateEstimates(progress);
updateEstimates(progress, 400);
QCOMPARE(progress.totalSize(), qint64(0));
QCOMPARE(progress.totalProgress().estimatedEta, maximumEta);
QVERIFY(!progress.trustEta());
progress.setProgressComplete(pending);
updateEstimates(progress);
QCOMPARE(progress.totalProgress().estimatedEta, quint64(0));
QCOMPARE(progress.completedFiles(), progress.totalFiles());
}
};
QTEST_GUILESS_MAIN(TestProgressDispatcher)
#include "testprogressdispatcher.moc"