QTestLib: fix crashes when using qDebug()s from threads with -junitxml

Other test loggers just output things immediately, but the junit test
logger appends messages to a vector, so this needs to be
mutex-protected.

In case of qDebug()s from long-running threads, we also need
to protect creation/destruction of systemOutputElement and
systemErrorElement -- and in case of qFatal(), currentTestCase.

Pick-to: 6.5
Change-Id: If35055fc232276a778951ebbfeaccd185b04f46b
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Jason McDonald <macadder1@gmail.com>
(cherry picked from commit b4d6892ba5a745c1836daf34c850d13ef61e7ae0)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 120a987544dc4d3824b02016dc837850b1fc8aef)
This commit is contained in:
David Faure 2025-03-26 12:03:02 +01:00 committed by Qt Cherry-pick Bot
parent 9878254088
commit b3ee8cda10
2 changed files with 16 additions and 7 deletions

View File

@ -130,6 +130,8 @@ void QJUnitTestLogger::enterTestFunction(const char *function)
void QJUnitTestLogger::enterTestCase(const char *name)
{
{
QMutexLocker locker(&mutex);
currentTestCase = new QTestElement(QTest::LET_TestCase);
currentTestCase->addAttribute(QTest::AI_Name, name);
currentTestCase->addAttribute(QTest::AI_Classname, QTestResult::currentTestObjectName());
@ -138,6 +140,7 @@ void QJUnitTestLogger::enterTestCase(const char *name)
Q_ASSERT(!systemOutputElement && !systemErrorElement);
systemOutputElement = new QTestElement(QTest::LET_SystemOutput);
systemErrorElement = new QTestElement(QTest::LET_SystemError);
}
// The element will be deleted when the suite is deleted
@ -174,6 +177,7 @@ void QJUnitTestLogger::leaveTestFunction()
void QJUnitTestLogger::leaveTestCase()
{
QMutexLocker locker(&mutex);
currentTestCase->addAttribute(QTest::AI_Time,
toSecondsFormat(elapsedTestCaseSeconds() * 1000).constData());
@ -257,6 +261,7 @@ void QJUnitTestLogger::addMessage(MessageTypes type, const QString &message, con
Q_UNUSED(file);
Q_UNUSED(line);
QMutexLocker locker(&mutex);
if (type == QFatal) {
addFailure(QTest::LET_Error, "qfatal", message);
return;

View File

@ -19,6 +19,7 @@
#include <QtTest/private/qabstracttestlogger_p.h>
#include <QtTest/private/qtestelementattribute_p.h>
#include <QtCore/qmutex.h>
#include <vector>
@ -61,6 +62,9 @@ class QJUnitTestLogger : public QAbstractTestLogger
QTestElement *systemOutputElement = nullptr;
QTestElement *systemErrorElement = nullptr;
QTestJUnitStreamer *logFormatter = nullptr;
// protects currentTestCase, systemOutputElement and systemErrorElement
// in case of qDebug()/qWarning() etc. from threads
QMutex mutex;
int testCounter = 0;
int failureCounter = 0;