summaryrefslogtreecommitdiffstats
path: root/tests/auto/other
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2022-07-10 22:22:13 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2022-07-12 17:16:37 +0200
commit0dadb951b5249d13e78b009759dab6a6ec9dc537 (patch)
tree0d8d78a84b17a98629e73e498cac04bc23508efe /tests/auto/other
parent7de0f3e9ccff3b9eea1c240530834c52ca56d1c0 (diff)
qobjectrace test: Don't hardcode the number of threads
Use QThread::idealThreadCount instead. This requires that we use QVarLengthArray, as MSVC doesn't allow us to allocate arrays with a non-constexpr size on the stack. By using as many threads as the system has cores, we are more likely to detect race conditions reliably. On systems with fewer cores (in particular on qemu platforms like QNX, where this test has been failing a lot), we'll less likely end up with false negatives due to timeouts. Pick-to: 6.4 6.3 6.2 Change-Id: Ie8631aef544ca7b53c06a0729d05459016745486 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/other')
-rw-r--r--tests/auto/other/qobjectrace/tst_qobjectrace.cpp29
1 files changed, 18 insertions, 11 deletions
diff --git a/tests/auto/other/qobjectrace/tst_qobjectrace.cpp b/tests/auto/other/qobjectrace/tst_qobjectrace.cpp
index 2c28e691d1..1dbaf33e32 100644
--- a/tests/auto/other/qobjectrace/tst_qobjectrace.cpp
+++ b/tests/auto/other/qobjectrace/tst_qobjectrace.cpp
@@ -1,4 +1,4 @@
-// Copyright (C) 2016 The Qt Company Ltd.
+// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
@@ -21,12 +21,20 @@ struct Functor
class tst_QObjectRace: public QObject
{
Q_OBJECT
+public:
+ tst_QObjectRace()
+ : ThreadCount(QThread::idealThreadCount())
+ {}
+
private slots:
void moveToThreadRace();
void destroyRace();
void blockingQueuedDestroyRace();
void disconnectRace();
void disconnectRace2();
+
+private:
+ const int ThreadCount;
};
class RaceObject : public QObject
@@ -110,8 +118,7 @@ void tst_QObjectRace::moveToThreadRace()
{
RaceObject *object = new RaceObject;
- enum { ThreadCount = 6 };
- RaceThread *threads[ThreadCount];
+ QVarLengthArray<RaceThread *, 16> threads(ThreadCount);
for (int i = 0; i < ThreadCount; ++i) {
threads[i] = new RaceThread;
threads[i]->setObject(object);
@@ -241,10 +248,10 @@ void tst_QObjectRace::destroyRace()
if (QTestPrivate::isRunningArmOnX86())
QSKIP("Test is too slow to run on emulator");
- enum { ThreadCount = 10, ObjectCountPerThread = 2777,
- ObjectCount = ThreadCount * ObjectCountPerThread };
+ constexpr int ObjectCountPerThread = 2777;
+ const int ObjectCount = ThreadCount * ObjectCountPerThread;
- MyObject *objects[ObjectCount];
+ QVarLengthArray<MyObject *, ObjectCountPerThread * 10> objects(ObjectCount);
for (int i = 0; i < ObjectCount; ++i)
objects[i] = new MyObject;
@@ -261,10 +268,10 @@ void tst_QObjectRace::destroyRace()
objects[((i+5)*79) % ObjectCount], Functor() );
}
- DestroyThread *threads[ThreadCount];
+ QVarLengthArray<DestroyThread *, 16> threads(ThreadCount);
for (int i = 0; i < ThreadCount; ++i) {
threads[i] = new DestroyThread;
- threads[i]->setObjects(objects + i*ObjectCountPerThread, ObjectCountPerThread);
+ threads[i]->setObjects(objects.data() + i*ObjectCountPerThread, ObjectCountPerThread);
}
for (int i = 0; i < ThreadCount; ++i)
@@ -477,7 +484,7 @@ public:
void tst_QObjectRace::disconnectRace()
{
- enum { ThreadCount = 20, TimeLimit = 3000 };
+ enum { TimeLimit = 3000 };
QCOMPARE(countedStructObjectsCount.loadRelaxed(), 0u);
@@ -487,7 +494,7 @@ void tst_QObjectRace::disconnectRace()
senderThread->start();
sender->moveToThread(senderThread.data());
- DisconnectRaceThread *threads[ThreadCount];
+ QVarLengthArray<DisconnectRaceThread *, 16> threads(ThreadCount);
for (int i = 0; i < ThreadCount; ++i) {
threads[i] = new DisconnectRaceThread(sender.data(), !(i % 10));
threads[i]->start();
@@ -513,7 +520,7 @@ void tst_QObjectRace::disconnectRace()
senderThread->start();
sender->moveToThread(senderThread.data());
- DeleteReceiverRaceReceiverThread *threads[ThreadCount];
+ QVarLengthArray<DeleteReceiverRaceReceiverThread *, 16> threads(ThreadCount);
for (int i = 0; i < ThreadCount; ++i) {
threads[i] = new DeleteReceiverRaceReceiverThread(sender.data());
threads[i]->start();