Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtestsupport_core.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <thread>
8
9using namespace std::chrono_literals;
10
11// Assert that this instantiation of std::atomic is always lock-free so we
12// know that no code will execute on destruction.
13static_assert(std::atomic<std::chrono::milliseconds>::is_always_lock_free);
14
15QT_BEGIN_NAMESPACE
16
17// ### Qt 7: reduce the default: QTBUG-138160
18Q_CONSTINIT std::atomic<std::chrono::milliseconds> QTest::defaultTryTimeout{5s};
19
20/*!
21 \variable QTest::defaultTryTimeout
22 \since 6.11
23
24 This global variable stores the default timeout used by the \c {QTRY_*}
25 functions and \l qWait.
26
27 The most typical use case for this variable is to modify the timeout
28 for an entire test:
29
30 \snippet code/src_qtestlib_qtestcase.cpp set defaultTryTimeout
31
32 However, you can also set it for a specific scope, using
33 \l QAtomicScopedValueRollback:
34
35 \snippet code/src_qtestlib_qtestcase.cpp rollback defaultTryTimeout
36
37 To access the value, call \c load():
38
39 \snippet code/src_qtestlib_qtestcase.cpp get defaultTryTimeout
40*/
41
42/*!
43 \overload
44
45 Sleeps for \a ms milliseconds, blocking execution of the test.
46
47 Equivalent to calling:
48 \code
49 QTest::qSleep(std::chrono::milliseconds{ms});
50 \endcode
51*/
52void QTest::qSleep(int ms)
53{
54 QTest::qSleep(std::chrono::milliseconds{ms});
55}
56
57/*!
58 \since 6.7
59
60 Sleeps for \a msecs, blocking execution of the test.
61
62 This method will not do any event processing and will leave your test
63 unresponsive. Network communication might time out while sleeping.
64 Use \l {QTest::qWait()} to do non-blocking sleeping.
65
66 \a msecs must be greater than 0ms.
67
68 \note Starting from Qt 6.7, this function is implemented using
69 \c {std::this_thread::sleep_for}, so the accuracy of time spent depends
70 on the Standard Library implementation. Before Qt 6.7 this function called
71 either \c nanosleep() on Unix or \c Sleep() on Windows, so the accuracy of
72 time spent in this function depended on the operating system.
73
74 Example:
75 \snippet code/src_qtestlib_qtestcase.cpp 23
76
77 \sa {QTest::qWait()}
78*/
79void QTest::qSleep(std::chrono::milliseconds msecs)
80{
81 Q_ASSERT(msecs > 0ms);
82 std::this_thread::sleep_for(msecs);
83}
84
85/*! \fn template <typename Functor> bool QTest::qWaitFor(Functor predicate, int timeout)
86
87 \since 5.10
88 \overload
89
90 Waits for \a timeout milliseconds or until the \a predicate returns true.
91
92 This is equivalent to calling:
93 \code
94 qWaitFor(predicate, QDeadlineTimer(timeout));
95 \endcode
96*/
97
98/*! \fn template <typename Functor> bool QTest::qWaitFor(Functor predicate, QDeadlineTimer deadline)
99 \since 6.7
100
101 Waits until \a deadline has expired, or until \a predicate returns true, whichever
102 happens first.
103
104 Returns \c true if \a predicate returned true at any point, otherwise returns \c false.
105
106 Example:
107
108 \snippet code/src_corelib_kernel_qtestsupport_core.cpp 2
109
110 The code above will wait for the object to become ready, for a
111 maximum of three seconds.
112*/
113
114/*!
115 \overload
116
117 Waits for \a msecs. Equivalent to calling:
118 \code
119 QTest::qWait(std::chrono::milliseconds{msecs});
120 \endcode
121*/
122Q_CORE_EXPORT void QTest::qWait(int msecs)
123{
124 qWait(std::chrono::milliseconds{msecs});
125}
126
127/*!
128 \since 6.7
129
130 Waits for \a msecs. While waiting, events will be processed and
131 your test will stay responsive to user interface events or network communication.
132
133 Example:
134
135 \snippet code/src_corelib_kernel_qtestsupport_core.cpp 1
136
137 The code above will wait until the network server is responding for a
138 maximum of about 12.5 seconds.
139
140 The \l{QTRY_COMPARE()}{QTRY_*} macros are usually a better choice than
141 qWait(). qWait() always pauses for the full timeout, which can leave the
142 test idle and slow down execution.
143
144 The \c {QTRY_*} macros poll the condition until it succeeds or the timeout
145 expires. Your test therefore continues as soon as possible and stays more
146 reliable. If the condition still fails, the macros double the timeout once
147 and report the new value so that you can adjust it.
148
149 For example, rewrite the code above as:
150
151 \code
152 QTRY_VERIFY_WITH_TIMEOUT(!myNetworkServerNotResponding(), 12.5s);
153 \endcode
154
155 \sa QTest::qSleep(), QSignalSpy::wait(), QTRY_VERIFY_WITH_TIMEOUT()
156*/
157Q_CORE_EXPORT void QTest::qWait(std::chrono::milliseconds msecs)
158{
159 // Ideally this method would be implemented in terms of qWaitFor(), with a
160 // predicate that always returns false, but qWaitFor() uses the 1-arg overload
161 // of processEvents(), which doesn't handle events posted in this round of event
162 // processing, which, together with the 10ms qSleep() after every processEvents(),
163 // lead to a 10x slow-down in some webengine tests.
164
165 Q_ASSERT(QCoreApplication::instance());
166
167 using namespace std::chrono;
168
169 QDeadlineTimer deadline(msecs, Qt::PreciseTimer);
170
171 do {
172 QCoreApplication::processEvents(QEventLoop::AllEvents, deadline);
173 QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
174
175 // If dealine is Forever, processEvents() has already looped forever
176 if (deadline.isForever())
177 break;
178
179 msecs = ceil<milliseconds>(deadline.remainingTimeAsDuration());
180 if (msecs == 0ms)
181 break;
182
183 QTest::qSleep(std::min(10ms, msecs));
184 } while (!deadline.hasExpired());
185}
186
187QT_END_NAMESPACE
Q_CORE_EXPORT void qWait(std::chrono::milliseconds msecs)
Q_CORE_EXPORT void qSleep(int ms)
\variable QTest::defaultTryTimeout
Q_CORE_EXPORT void qSleep(std::chrono::milliseconds msecs)
Q_CORE_EXPORT void qWait(int ms)
This is an overloaded member function, provided for convenience. It differs from the above function o...