tst_QPromise: DRY the inspection of QFuture's handlers

Not all test cases inspect all of failed, then, and canceled, but it
doesn't hurt to always collect all three.

Avoids having to write the same type of code over and over again.

Amends bf3fc5c95c (but really
1f22fc995a and
855c448469, which each duplicated the
initial pattern without refactoring).

Pick-to: 6.5 6.2
Change-Id: Ifb2a3589f8aed9017fbdff20e4edb64e8c9e2488
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
(cherry picked from commit 9d0da873f0ddadb60e61fbd6c96c8b00f026e99f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
bb10
Marc Mutz 2024-10-30 12:34:49 +01:00 committed by Qt Cherry-pick Bot
parent ab4919f316
commit 7be419bb23
1 changed files with 35 additions and 27 deletions

View File

@ -105,6 +105,31 @@ struct ThreadWrapper
};
#endif
struct FutureWatcher
{
bool thenCalled = false;
bool onFailedCalled = false;
bool onCanceledCalled = false;
FutureWatcher() = default;
Q_DISABLE_COPY_MOVE(FutureWatcher)
template <typename T>
explicit FutureWatcher(QFuture<T> &f) { setFuture(f); }
template <typename T>
void setFuture(QFuture<T> &f)
{
f.onFailed([&]{
onFailedCalled = true;
if constexpr (!std::is_void_v<T>)
return T{};
})
.then([&](auto&&...) { thenCalled = true; })
.onCanceled([&]{ onCanceledCalled = true; });
}
};
void tst_QPromise::promise()
{
const auto testCanCreatePromise = [] (auto promise) {
@ -540,20 +565,15 @@ template <typename T>
static inline void testCancelWhenDestroyedRunsContinuations()
{
QFuture<T> future;
bool onCanceledCalled = false;
bool thenCalled = false;
FutureWatcher r;
{
QPromise<T> promise;
future = promise.future();
future.then([&] (auto&&) {
thenCalled = true;
}).onCanceled([&] () {
onCanceledCalled = true;
});
r.setFuture(future);
}
QVERIFY(future.isFinished());
QVERIFY(!thenCalled);
QVERIFY(onCanceledCalled);
QVERIFY(!r.thenCalled);
QVERIFY(r.onCanceledCalled);
}
void tst_QPromise::cancelWhenDestroyedRunsContinuations()
@ -568,24 +588,15 @@ template <typename T>
static inline void testCancelWhenDestroyedWithFailureHandler()
{
QFuture<T> future;
bool onFailedCalled = false;
bool thenCalled = false;
FutureWatcher r;
{
QPromise<T> promise;
future = promise.future();
future
.onFailed([&] () {
onFailedCalled = true;
if constexpr (!std::is_same_v<void, T>)
return T{};
})
.then([&] (auto&&) {
thenCalled = true;
});
r.setFuture(future);
}
QVERIFY(future.isFinished());
QVERIFY(!onFailedCalled);
QVERIFY(!thenCalled);
QVERIFY(!r.onFailedCalled);
QVERIFY(!r.thenCalled);
}
void tst_QPromise::cancelWhenDestroyedWithFailureHandler()
@ -606,10 +617,7 @@ static inline void testContinuationsRunWhenFinished()
QPromise<T> promise;
QFuture<T> future = promise.future();
bool thenCalled = false;
future.then([&] (auto&&) {
thenCalled = true;
});
FutureWatcher r(future);
promise.start();
if constexpr (!std::is_void_v<T>) {
@ -617,7 +625,7 @@ static inline void testContinuationsRunWhenFinished()
}
promise.finish();
QVERIFY(thenCalled);
QVERIFY(r.thenCalled);
}
void tst_QPromise::continuationsRunWhenFinished()