mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2025-07-01 14:54:02 +08:00
std::chrono overloads for .timeout() and .delay() (#30)
Add convenience overloads accepting durations from the C++ Standard Library.
This commit is contained in:
@ -13,6 +13,9 @@
|
||||
// Qt
|
||||
#include <QtTest>
|
||||
|
||||
// C++ Standard Library
|
||||
#include <chrono>
|
||||
|
||||
using namespace QtPromise;
|
||||
|
||||
class tst_qpromise_delay : public QObject
|
||||
@ -22,6 +25,9 @@ class tst_qpromise_delay : public QObject
|
||||
private Q_SLOTS:
|
||||
void fulfilled();
|
||||
void rejected();
|
||||
|
||||
void fulfilledStdChrono();
|
||||
void rejectedStdChrono();
|
||||
};
|
||||
|
||||
QTEST_MAIN(tst_qpromise_delay)
|
||||
@ -63,3 +69,40 @@ void tst_qpromise_delay::rejected()
|
||||
QCOMPARE(p.isRejected(), true);
|
||||
QVERIFY(elapsed <= 10);
|
||||
}
|
||||
|
||||
void tst_qpromise_delay::fulfilledStdChrono()
|
||||
{
|
||||
QElapsedTimer timer;
|
||||
qint64 elapsed = -1;
|
||||
|
||||
timer.start();
|
||||
|
||||
auto p = QPromise<int>::resolve(42).delay(std::chrono::seconds{1}).finally([&]() {
|
||||
elapsed = timer.elapsed();
|
||||
});
|
||||
|
||||
QCOMPARE(waitForValue(p, -1), 42);
|
||||
QCOMPARE(p.isFulfilled(), true);
|
||||
|
||||
// Qt::CoarseTimer (default) Coarse timers try to
|
||||
// keep accuracy within 5% of the desired interval.
|
||||
// Require accuracy within 6% for passing the test.
|
||||
QVERIFY(elapsed >= static_cast<qint64>(1000 * 0.94));
|
||||
QVERIFY(elapsed <= static_cast<qint64>(1000 * 1.06));
|
||||
}
|
||||
|
||||
void tst_qpromise_delay::rejectedStdChrono()
|
||||
{
|
||||
QElapsedTimer timer;
|
||||
qint64 elapsed = -1;
|
||||
|
||||
timer.start();
|
||||
|
||||
auto p = QPromise<int>::reject(QString("foo")).delay(std::chrono::seconds{1}).finally([&]() {
|
||||
elapsed = timer.elapsed();
|
||||
});
|
||||
|
||||
QCOMPARE(waitForError(p, QString()), QString("foo"));
|
||||
QCOMPARE(p.isRejected(), true);
|
||||
QVERIFY(elapsed <= 10);
|
||||
}
|
||||
|
@ -13,6 +13,9 @@
|
||||
// Qt
|
||||
#include <QtTest>
|
||||
|
||||
// C++ Standard Library
|
||||
#include <chrono>
|
||||
|
||||
using namespace QtPromise;
|
||||
|
||||
class tst_qpromise_timeout : public QObject
|
||||
@ -23,6 +26,10 @@ private Q_SLOTS:
|
||||
void fulfilled();
|
||||
void rejected();
|
||||
void timeout();
|
||||
|
||||
void fulfilledStdChrono();
|
||||
void rejectedStdChrono();
|
||||
void timeoutStdChrono();
|
||||
};
|
||||
|
||||
QTEST_MAIN(tst_qpromise_timeout)
|
||||
@ -100,3 +107,76 @@ void tst_qpromise_timeout::timeout()
|
||||
QVERIFY(elapsed >= static_cast<qint64>(2000 * 0.94));
|
||||
QVERIFY(elapsed <= static_cast<qint64>(2000 * 1.06));
|
||||
}
|
||||
|
||||
void tst_qpromise_timeout::fulfilledStdChrono()
|
||||
{
|
||||
QElapsedTimer timer;
|
||||
qint64 elapsed = -1;
|
||||
|
||||
timer.start();
|
||||
|
||||
auto p = QPromise<int>([](const QPromiseResolve<int>& resolve) {
|
||||
QTimer::singleShot(1000, [=]() {
|
||||
resolve(42);
|
||||
});
|
||||
}).timeout(std::chrono::seconds{2}).finally([&]() {
|
||||
elapsed = timer.elapsed();
|
||||
});
|
||||
|
||||
QCOMPARE(waitForValue(p, -1), 42);
|
||||
QCOMPARE(p.isFulfilled(), true);
|
||||
QVERIFY(elapsed < 2000);
|
||||
}
|
||||
|
||||
void tst_qpromise_timeout::rejectedStdChrono()
|
||||
{
|
||||
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(std::chrono::seconds{2}).finally([&]() {
|
||||
elapsed = timer.elapsed();
|
||||
});
|
||||
|
||||
|
||||
QCOMPARE(waitForError(p, QString()), QString("foo"));
|
||||
QCOMPARE(p.isRejected(), true);
|
||||
QVERIFY(elapsed < 2000);
|
||||
}
|
||||
|
||||
void tst_qpromise_timeout::timeoutStdChrono()
|
||||
{
|
||||
QElapsedTimer timer;
|
||||
qint64 elapsed = -1;
|
||||
bool failed = false;
|
||||
|
||||
timer.start();
|
||||
|
||||
auto p = QPromise<int>([](const QPromiseResolve<int>& resolve) {
|
||||
QTimer::singleShot(4000, [=]() {
|
||||
resolve(42);
|
||||
});
|
||||
}).timeout(std::chrono::seconds{2}).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);
|
||||
|
||||
// Qt::CoarseTimer (default) Coarse timers try to
|
||||
// keep accuracy within 5% of the desired interval.
|
||||
// Require accuracy within 6% for passing the test.
|
||||
QVERIFY(elapsed >= static_cast<qint64>(2000 * 0.94));
|
||||
QVERIFY(elapsed <= static_cast<qint64>(2000 * 1.06));
|
||||
}
|
||||
|
Reference in New Issue
Block a user