mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2025-07-01 06:41:55 +08:00
Implement QPromise::delay(msec)
This method returns a promise that will be fulfilled with the same value as the `input` promise and after at least `msec` milliseconds. If the `input` promise is rejected, the `output` promise is immediately rejected with the same reason.
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
|
||||
// Qt
|
||||
#include <QtTest>
|
||||
#include <QElapsedTimer>
|
||||
|
||||
using namespace QtPromise;
|
||||
using namespace QtPromisePrivate;
|
||||
@ -48,6 +49,9 @@ private Q_SLOTS:
|
||||
void tapDelayedResolved();
|
||||
void tapDelayedRejected();
|
||||
|
||||
void delayFulfilled();
|
||||
void delayRejected();
|
||||
|
||||
}; // class tst_qpromise
|
||||
|
||||
QTEST_MAIN(tst_qpromise)
|
||||
@ -654,3 +658,36 @@ void tst_qpromise::tapDelayedRejected()
|
||||
QCOMPARE(p.isRejected(), true);
|
||||
QCOMPARE(values, QVector<int>({2, 3}));
|
||||
}
|
||||
|
||||
void tst_qpromise::delayFulfilled()
|
||||
{
|
||||
QElapsedTimer timer;
|
||||
qint64 elapsed = -1;
|
||||
|
||||
timer.start();
|
||||
|
||||
auto p = QPromise<int>::resolve(42).delay(1000).finally([&]() {
|
||||
elapsed = timer.elapsed();
|
||||
});
|
||||
|
||||
QCOMPARE(waitForValue(p, -1), 42);
|
||||
QCOMPARE(p.isFulfilled(), true);
|
||||
QVERIFY(elapsed >= 1000 * 0.95); // Qt::CoarseTimer (default) Coarse timers try to
|
||||
QVERIFY(elapsed <= 1000 * 1.05); // keep accuracy within 5% of the desired interval.
|
||||
}
|
||||
|
||||
void tst_qpromise::delayRejected()
|
||||
{
|
||||
QElapsedTimer timer;
|
||||
qint64 elapsed = -1;
|
||||
|
||||
timer.start();
|
||||
|
||||
auto p = QPromise<int>::reject(QString("foo")).delay(1000).finally([&]() {
|
||||
elapsed = timer.elapsed();
|
||||
});
|
||||
|
||||
QCOMPARE(waitForError(p, QString()), QString("foo"));
|
||||
QCOMPARE(p.isRejected(), true);
|
||||
QVERIFY(elapsed < 5);
|
||||
}
|
||||
|
Reference in New Issue
Block a user