summaryrefslogtreecommitdiffstats
path: root/src/testlib/qtestcase.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/testlib/qtestcase.cpp')
-rw-r--r--src/testlib/qtestcase.cpp115
1 files changed, 70 insertions, 45 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp
index 1c7518a85e..2c023a8ee1 100644
--- a/src/testlib/qtestcase.cpp
+++ b/src/testlib/qtestcase.cpp
@@ -1,13 +1,12 @@
/****************************************************************************
**
-** Copyright (C) 2021 The Qt Company Ltd.
+** Copyright (C) 2020 The Qt Company Ltd.
** Copyright (C) 2016 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtTest module of the Qt Toolkit.
**
-** $QT_BEGIN_LICENSE:COMM$
-**
+** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
@@ -16,25 +15,26 @@
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
-** $QT_END_LICENSE$
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
-**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
**
+** $QT_END_LICENSE$
**
****************************************************************************/
@@ -82,10 +82,10 @@
#include <QtTest/private/qappletestlogger_p.h>
#endif
+#include <atomic>
#include <cmath>
#include <numeric>
#include <algorithm>
-#include <condition_variable>
#include <mutex>
#include <chrono>
@@ -1007,16 +1007,30 @@ void TestMethods::invokeTestOnData(int index) const
class WatchDog : public QThread
{
- enum Expectation {
+ enum Expectation : std::size_t {
+ // bits 0..1: state
ThreadStart,
TestFunctionStart,
TestFunctionEnd,
ThreadEnd,
+ ExpectationMask = ThreadStart | TestFunctionStart | TestFunctionEnd | ThreadEnd,
+
+ // bits 2..: generation
};
+ Q_STATIC_ASSERT(size_t(ExpectationMask) == 0x3);
+ // static constexpr size_t GenerationShift = 2; // C++17-ism, so inline in combine and generation.
+
+ static constexpr Expectation state(Expectation e) noexcept
+ { return static_cast<Expectation>(e & ExpectationMask); }
+ static constexpr size_t generation(Expectation e) noexcept
+ { return e >> 2; }
+ static constexpr Expectation combine(Expectation e, size_t gen) noexcept
+ { return static_cast<Expectation>(e | (gen << 2)); }
- bool waitFor(std::unique_lock<QtPrivate::mutex> &m, Expectation e) {
- auto expectationChanged = [this, e] { return expecting != e; };
- switch (e) {
+ bool waitFor(std::unique_lock<QtPrivate::mutex> &m, Expectation e)
+ {
+ auto expectationChanged = [this, e] { return expecting.load(std::memory_order_relaxed) != e; };
+ switch (state(e)) {
case TestFunctionEnd:
return waitCondition.wait_for(m, defaultTimeout(), expectationChanged);
case ThreadStart:
@@ -1029,48 +1043,59 @@ class WatchDog : public QThread
return false;
}
+ void setExpectation(Expectation e)
+ {
+ Q_ASSERT(generation(e) == 0); // no embedded generation allowed
+ const auto locker = qt_scoped_lock(mutex);
+ auto cur = expecting.load(std::memory_order_relaxed);
+ auto gen = generation(cur);
+ if (e == TestFunctionStart)
+ ++gen;
+ e = combine(e, gen);
+ expecting.store(e, std::memory_order_relaxed);
+ waitCondition.notify_all();
+ }
+
public:
WatchDog()
{
auto locker = qt_unique_lock(mutex);
- expecting = ThreadStart;
+ expecting.store(ThreadStart, std::memory_order_relaxed);
start();
waitFor(locker, ThreadStart);
}
- ~WatchDog() {
- {
- const auto locker = qt_scoped_lock(mutex);
- expecting = ThreadEnd;
- waitCondition.notify_all();
- }
+
+ ~WatchDog()
+ {
+ setExpectation(ThreadEnd);
wait();
}
- void beginTest() {
- const auto locker = qt_scoped_lock(mutex);
- expecting = TestFunctionEnd;
- waitCondition.notify_all();
+ void beginTest()
+ {
+ setExpectation(TestFunctionEnd);
}
- void testFinished() {
- const auto locker = qt_scoped_lock(mutex);
- expecting = TestFunctionStart;
- waitCondition.notify_all();
+ void testFinished()
+ {
+ setExpectation(TestFunctionStart);
}
- void run() override {
+ void run() override
+ {
auto locker = qt_unique_lock(mutex);
- expecting = TestFunctionStart;
+ expecting.store(TestFunctionStart, std::memory_order_release);
waitCondition.notify_all();
while (true) {
- switch (expecting) {
+ Expectation e = expecting.load(std::memory_order_acquire);
+ switch (state(e)) {
case ThreadEnd:
return;
case ThreadStart:
Q_UNREACHABLE();
case TestFunctionStart:
case TestFunctionEnd:
- if (Q_UNLIKELY(!waitFor(locker, expecting))) {
+ if (Q_UNLIKELY(!waitFor(locker, e))) {
stackTrace();
qFatal("Test function timed out");
}
@@ -1081,7 +1106,7 @@ public:
private:
QtPrivate::mutex mutex;
QtPrivate::condition_variable waitCondition;
- Expectation expecting;
+ std::atomic<Expectation> expecting;
};
#else // !QT_CONFIG(thread)