summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2024-01-31 16:17:40 +0100
committerMarc Mutz <marc.mutz@qt.io>2024-02-07 12:15:09 +0000
commitd4bb448cddce63e0c6a84a86020fa59dd32b2293 (patch)
tree785b84d33f9478c307184d8d5dba62193dfe33b0
parentfb226262e8b371372fdc66b832b9eab24741f70d (diff)
QTest: allow passing chrono literal as QTRY_ timeout
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>
-rw-r--r--src/testlib/qtestcase.h29
-rw-r--r--src/testlib/qtestcase.qdoc22
-rw-r--r--tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp8
3 files changed, 42 insertions, 17 deletions
diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h
index ae926ab632..7724e0504b 100644
--- a/src/testlib/qtestcase.h
+++ b/src/testlib/qtestcase.h
@@ -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.
@@ -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) \
@@ -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) \
@@ -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 { \
@@ -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 {\
diff --git a/src/testlib/qtestcase.qdoc b/src/testlib/qtestcase.qdoc
index 7503e8e709..37eacd98a8 100644
--- a/src/testlib/qtestcase.qdoc
+++ b/src/testlib/qtestcase.qdoc
@@ -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.
@@ -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
@@ -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.
@@ -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()
@@ -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()
@@ -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()
@@ -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()
@@ -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()
@@ -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()
diff --git a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp
index 5a8604e627..36e355c614 100644
--- a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp
+++ b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp
@@ -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);
}
@@ -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);
@@ -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);
}