Implement QPromise::timeout(msec, error)

This commit is contained in:
Simon Brunel 2017-09-02 12:23:42 +02:00
parent b47ca0569e
commit 18324d3f44
4 changed files with 109 additions and 0 deletions

View File

@ -46,6 +46,9 @@ public:
template <typename THandler>
inline QPromise<T> tap(THandler handler) const;
template <typename E = QPromiseTimeoutException>
inline QPromise<T> timeout(int msec, E&& error = E()) const;
inline QPromise<T> delay(int msec) const;
inline QPromise<T> wait() const;

View File

@ -150,6 +150,26 @@ inline QPromise<T> QPromiseBase<T>::tap(THandler handler) const
});
}
template <typename T>
template <typename E>
inline QPromise<T> QPromiseBase<T>::timeout(int msec, E&& error) const
{
QPromise<T> p = *this;
return QPromise<T>([&](
const QPromiseResolve<T>& resolve,
const QPromiseReject<T>& reject) {
QTimer::singleShot(msec, [=]() {
// we don't need to verify the current promise state, reject()
// takes care of checking if the promise is already resolved,
// and thus will ignore this rejection.
reject(std::move(error));
});
QtPromisePrivate::PromiseFulfill<QPromise<T> >::call(p, resolve, reject);
});
}
template <typename T>
inline QPromise<T> QPromiseBase<T>::delay(int msec) const
{

View File

@ -4,6 +4,9 @@
// QtPromise
#include "qpromiseglobal.h"
// Qt
#include <QException>
namespace QtPromise {
class QPromiseError
@ -53,6 +56,16 @@ private:
std::exception_ptr m_exception;
};
class QPromiseTimeoutException : public QException
{
public:
void raise() const Q_DECL_OVERRIDE { throw *this; }
QPromiseTimeoutException* clone() const Q_DECL_OVERRIDE
{
return new QPromiseTimeoutException(*this);
}
};
} // namespace QtPromise
#endif // QTPROMISE_QPROMISEERROR_H

View File

@ -49,6 +49,10 @@ private Q_SLOTS:
void tapDelayedResolved();
void tapDelayedRejected();
void timeoutFulfilled();
void timeoutRejected();
void timeoutReject();
void delayFulfilled();
void delayRejected();
@ -659,6 +663,75 @@ void tst_qpromise::tapDelayedRejected()
QCOMPARE(values, QVector<int>({2, 3}));
}
void tst_qpromise::timeoutFulfilled()
{
QElapsedTimer timer;
qint64 elapsed = -1;
timer.start();
auto p = QPromise<int>([](const QPromiseResolve<int>& resolve) {
QTimer::singleShot(1000, [=]() {
resolve(42);
});
}).timeout(2000).finally([&]() {
elapsed = timer.elapsed();
});
QCOMPARE(waitForValue(p, -1), 42);
QCOMPARE(p.isFulfilled(), true);
QVERIFY(elapsed < 2000);
}
void tst_qpromise::timeoutRejected()
{
QElapsedTimer timer;
qint64 elapsed = -1;
timer.start();
auto p = QPromise<int>([](const QPromiseResolve<int>&, const QPromiseReject<int>& reject) {
QTimer::singleShot(1000, [=]() {
reject(QString("foo"));
});
}).timeout(2000).finally([&]() {
elapsed = timer.elapsed();
});
QCOMPARE(waitForError(p, QString()), QString("foo"));
QCOMPARE(p.isRejected(), true);
QVERIFY(elapsed < 2000);
}
void tst_qpromise::timeoutReject()
{
QElapsedTimer timer;
qint64 elapsed = -1;
bool failed = false;
timer.start();
auto p = QPromise<int>([](const QPromiseResolve<int>& resolve) {
QTimer::singleShot(4000, [=]() {
resolve(42);
});
}).timeout(2000).finally([&]() {
elapsed = timer.elapsed();
});
p.fail([&](const QPromiseTimeoutException&) {
failed = true;
return -1;
}).wait();
QCOMPARE(waitForValue(p, -1), -1);
QCOMPARE(p.isRejected(), true);
QCOMPARE(failed, true);
QVERIFY(elapsed >= 2000 * 0.95); // Qt::CoarseTimer (default) Coarse timers try to
QVERIFY(elapsed <= 2000 * 1.05); // keep accuracy within 5% of the desired interval.
}
void tst_qpromise::delayFulfilled()
{
QElapsedTimer timer;