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 bf3fc5c95cb4e6acedf242c00b7a1c3b455062bb (but really
1f22fc995a36193cd67e8190858bb33614d149f4 and
855c4484693015cb6498f2183d95de1377d49898, which each duplicated the
initial pattern without refactoring).

Pick-to: 6.8 6.5 6.2
Change-Id: Ifb2a3589f8aed9017fbdff20e4edb64e8c9e2488
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Marc Mutz 2024-10-30 12:34:49 +01:00
parent 32d12c2eba
commit 9d0da873f0

View File

@ -103,6 +103,31 @@ struct ThreadWrapper
}
};
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) {
@ -545,20 +570,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()
@ -573,24 +593,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()
@ -611,10 +622,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>) {
@ -622,7 +630,7 @@ static inline void testContinuationsRunWhenFinished()
}
promise.finish();
QVERIFY(thenCalled);
QVERIFY(r.thenCalled);
}
void tst_QPromise::continuationsRunWhenFinished()