Skip to content

Commit

Permalink
QTest: allow passing chrono literal as QTRY_ timeout
Browse files Browse the repository at this point in the history
By wrapping the use of the timeout value in QTRY_IMPL in a lambda that
feeds the user input through the std::chrono::milliseconds constructor
with std::chrono_literals in-scope, the macros continue to work with
raw integral values as well as chrono literals not finer than
millisecond granularity.

Port all higher-level macros to pass a chrono literal and port some
uses in tst_selftests.

[ChangeLog][QtTest] The QTRY_*_WITH_TIMEOUT macros now also accept
chrono literals (was: int milliseconds).

Fixes: QTBUG-121746
Change-Id: Ib38406fc005a0a2c4ae3fd009760f73ad8bed355
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
  • Loading branch information
marcmutz committed Feb 7, 2024
1 parent fb22626 commit d4bb448
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
29 changes: 17 additions & 12 deletions src/testlib/qtestcase.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,14 @@ inline void useVerifyThrowsException() {}
} \
}

#define QTRY_IMPL(expr, timeout)\
const int qt_test_step = timeout < 350 ? timeout / 7 + 1 : 50; \
const int qt_test_timeoutValue = timeout; \
#define QTRY_IMPL(expr, timeoutAsGiven)\
const auto timeout = [&] { \
/* make 5s work w/o user action: */ \
using namespace std::chrono_literals; \
return std::chrono::milliseconds{timeoutAsGiven}; \
}(); \
const int qt_test_step = timeout.count() < 350 ? timeout.count() / 7 + 1 : 50; \
const int qt_test_timeoutValue = timeout.count(); \
{ QTRY_LOOP_IMPL(expr, qt_test_timeoutValue, qt_test_step) } \
QTRY_TIMEOUT_DEBUG_IMPL(expr, qt_test_timeoutValue, qt_test_step)
// Ends with an if-block, so doesn't want a following semicolon.
Expand All @@ -212,7 +217,7 @@ do { \
QVERIFY(expr); \
} while (false)

#define QTRY_VERIFY(expr) QTRY_VERIFY_WITH_TIMEOUT(expr, 5000)
#define QTRY_VERIFY(expr) QTRY_VERIFY_WITH_TIMEOUT(expr, 5s)

// Will try to wait for the expression to become true while allowing event processing
#define QTRY_VERIFY2_WITH_TIMEOUT(expr, messageExpression, timeout) \
Expand All @@ -221,7 +226,7 @@ do { \
QVERIFY2(expr, messageExpression); \
} while (false)

#define QTRY_VERIFY2(expr, messageExpression) QTRY_VERIFY2_WITH_TIMEOUT(expr, messageExpression, 5000)
#define QTRY_VERIFY2(expr, messageExpression) QTRY_VERIFY2_WITH_TIMEOUT(expr, messageExpression, 5s)

// Will try to wait for the comparison to become successful while allowing event processing
#define QTRY_COMPARE_WITH_TIMEOUT(expr, expected, timeout) \
Expand All @@ -230,7 +235,7 @@ do { \
QCOMPARE(expr, expected); \
} while (false)

#define QTRY_COMPARE(expr, expected) QTRY_COMPARE_WITH_TIMEOUT(expr, expected, 5000)
#define QTRY_COMPARE(expr, expected) QTRY_COMPARE_WITH_TIMEOUT(expr, expected, 5s)

#define QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, op, opId, timeout) \
do { \
Expand All @@ -241,32 +246,32 @@ do { \
#define QTRY_COMPARE_EQ_WITH_TIMEOUT(computed, baseline, timeout) \
QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, ==, Equal, timeout)

#define QTRY_COMPARE_EQ(computed, baseline) QTRY_COMPARE_EQ_WITH_TIMEOUT(computed, baseline, 5000)
#define QTRY_COMPARE_EQ(computed, baseline) QTRY_COMPARE_EQ_WITH_TIMEOUT(computed, baseline, 5s)

#define QTRY_COMPARE_NE_WITH_TIMEOUT(computed, baseline, timeout) \
QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, !=, NotEqual, timeout)

#define QTRY_COMPARE_NE(computed, baseline) QTRY_COMPARE_NE_WITH_TIMEOUT(computed, baseline, 5000)
#define QTRY_COMPARE_NE(computed, baseline) QTRY_COMPARE_NE_WITH_TIMEOUT(computed, baseline, 5s)

#define QTRY_COMPARE_LT_WITH_TIMEOUT(computed, baseline, timeout) \
QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, <, LessThan, timeout)

#define QTRY_COMPARE_LT(computed, baseline) QTRY_COMPARE_LT_WITH_TIMEOUT(computed, baseline, 5000)
#define QTRY_COMPARE_LT(computed, baseline) QTRY_COMPARE_LT_WITH_TIMEOUT(computed, baseline, 5s)

#define QTRY_COMPARE_LE_WITH_TIMEOUT(computed, baseline, timeout) \
QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, <=, LessThanOrEqual, timeout)

#define QTRY_COMPARE_LE(computed, baseline) QTRY_COMPARE_LE_WITH_TIMEOUT(computed, baseline, 5000)
#define QTRY_COMPARE_LE(computed, baseline) QTRY_COMPARE_LE_WITH_TIMEOUT(computed, baseline, 5s)

#define QTRY_COMPARE_GT_WITH_TIMEOUT(computed, baseline, timeout) \
QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, >, GreaterThan, timeout)

#define QTRY_COMPARE_GT(computed, baseline) QTRY_COMPARE_GT_WITH_TIMEOUT(computed, baseline, 5000)
#define QTRY_COMPARE_GT(computed, baseline) QTRY_COMPARE_GT_WITH_TIMEOUT(computed, baseline, 5s)

#define QTRY_COMPARE_GE_WITH_TIMEOUT(computed, baseline, timeout) \
QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, >=, GreaterThanOrEqual, timeout)

#define QTRY_COMPARE_GE(computed, baseline) QTRY_COMPARE_GE_WITH_TIMEOUT(computed, baseline, 5000)
#define QTRY_COMPARE_GE(computed, baseline) QTRY_COMPARE_GE_WITH_TIMEOUT(computed, baseline, 5s)

#define QSKIP_INTERNAL(statement) \
do {\
Expand Down
22 changes: 21 additions & 1 deletion src/testlib/qtestcase.qdoc
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@
is reached, a failure is recorded in the test log and the test won't be
executed further.

//![chrono-timeout]
Since Qt 6.8, the \a timeout can also be a \c{std::chrono} literal such as \c{2s}.
//![chrono-timeout]

\note This macro can only be used in a test function that is invoked
by the test framework.

Expand Down Expand Up @@ -390,9 +394,11 @@
except that it outputs a verbose \a message when \a condition is still false
after the specified \a timeout (in milliseconds). The \a message is a plain C string.

\include qtestcase.qdoc chrono-timeout

Example:
\code
QTRY_VERIFY2_WITH_TIMEOUT(list.size() > 2, QByteArray::number(list.size()).constData(), 10000);
QTRY_VERIFY2_WITH_TIMEOUT(list.size() > 2, QByteArray::number(list.size()).constData(), 10s);
\endcode

\note This macro can only be used in a test function that is invoked
Expand Down Expand Up @@ -434,6 +440,8 @@
will be processed. If the timeout is reached, a failure is recorded in the
test log and the test won't be executed further.

\include qtestcase.qdoc chrono-timeout

\note This macro can only be used in a test function that is invoked
by the test framework.

Expand Down Expand Up @@ -465,6 +473,8 @@
comparison, events will be processed. If the timeout is reached, a failure
is recorded in the test log and the test won't be executed further.

\include qtestcase.qdoc chrono-timeout

\include qtestcase.qdoc macro-usage-limitation

\sa QCOMPARE_EQ(), QTRY_COMPARE_EQ()
Expand Down Expand Up @@ -492,6 +502,8 @@
comparison, events will be processed. If the timeout is reached, a failure
is recorded in the test log and the test won't be executed further.

\include qtestcase.qdoc chrono-timeout

\include qtestcase.qdoc macro-usage-limitation

\sa QCOMPARE_NE(), QTRY_COMPARE_NE()
Expand Down Expand Up @@ -519,6 +531,8 @@
comparison, events will be processed. If the timeout is reached, a failure
is recorded in the test log and the test won't be executed further.

\include qtestcase.qdoc chrono-timeout

\include qtestcase.qdoc macro-usage-limitation

\sa QCOMPARE_LT(), QTRY_COMPARE_LT()
Expand Down Expand Up @@ -546,6 +560,8 @@
comparison, events will be processed. If the timeout is reached, a failure
is recorded in the test log and the test won't be executed further.

\include qtestcase.qdoc chrono-timeout

\include qtestcase.qdoc macro-usage-limitation

\sa QCOMPARE_LE(), QTRY_COMPARE_LE()
Expand Down Expand Up @@ -573,6 +589,8 @@
comparison, events will be processed. If the timeout is reached, a failure
is recorded in the test log and the test won't be executed further.

\include qtestcase.qdoc chrono-timeout

\include qtestcase.qdoc macro-usage-limitation

\sa QCOMPARE_GT(), QTRY_COMPARE_GT()
Expand Down Expand Up @@ -600,6 +618,8 @@
comparison, events will be processed. If the timeout is reached, a failure
is recorded in the test log and the test won't be executed further.

\include qtestcase.qdoc chrono-timeout

\include qtestcase.qdoc macro-usage-limitation

\sa QCOMPARE_GE(), QTRY_COMPARE_GE()
Expand Down
8 changes: 4 additions & 4 deletions tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,9 +771,9 @@ void tst_Cmptest::tryCompare()
}
{
DeferredFlag c;
QTRY_COMPARE_WITH_TIMEOUT(c, DeferredFlag(), 300);
QTRY_COMPARE_WITH_TIMEOUT(c, DeferredFlag(), 300ms);
QVERIFY(!c); // Instantly equal, so succeeded without delay.
QTRY_COMPARE_WITH_TIMEOUT(c, trueAlready, 200);
QTRY_COMPARE_WITH_TIMEOUT(c, trueAlready, 1s);
qInfo("Should now time out and fail");
QTRY_COMPARE_WITH_TIMEOUT(c, DeferredFlag(), 200);
}
Expand All @@ -788,7 +788,7 @@ void tst_Cmptest::tryVerify()
}
{
DeferredFlag c;
QTRY_VERIFY_WITH_TIMEOUT(!c, 300);
QTRY_VERIFY_WITH_TIMEOUT(!c, 300ms);
QTRY_VERIFY_WITH_TIMEOUT(c, 200);
qInfo("Should now time out and fail");
QTRY_VERIFY_WITH_TIMEOUT(!c, 200);
Expand All @@ -804,7 +804,7 @@ void tst_Cmptest::tryVerify2()
}
{
DeferredFlag c;
QTRY_VERIFY2_WITH_TIMEOUT(!c, "Failed to check before looping", 300);
QTRY_VERIFY2_WITH_TIMEOUT(!c, "Failed to check before looping", 300ms);
QTRY_VERIFY2_WITH_TIMEOUT(c, "Failed to trigger single-shot", 200);
QTRY_VERIFY2_WITH_TIMEOUT(!c, "Should time out and fail", 200);
}
Expand Down

0 comments on commit d4bb448

Please sign in to comment.