diff options
author | David Faure <david.faure@kdab.com> | 2014-06-08 21:46:24 +0200 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2014-06-20 21:54:04 +0200 |
commit | 884b38157689a893ace0a4c793fab921167abb2c (patch) | |
tree | c0f4a079a79c760ad08506017ed296880f529da1 /tests | |
parent | d98004cd2f33e8147cff9f3cb203fdef5e6dd00e (diff) |
Fix data race on QLoggingCategory when using qDebug from multiple threads
setEnabled() would race with isEnabled()/isDebugEnabled()/etc.
Change-Id: I2004cba81d5417a634b97f5c2f98d3a4ab71770d
Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/io/qdebug/qdebug.pro | 2 | ||||
-rw-r--r-- | tests/auto/corelib/io/qdebug/tst_qdebug.cpp | 40 |
2 files changed, 41 insertions, 1 deletions
diff --git a/tests/auto/corelib/io/qdebug/qdebug.pro b/tests/auto/corelib/io/qdebug/qdebug.pro index 820c17fc69..5e902bb105 100644 --- a/tests/auto/corelib/io/qdebug/qdebug.pro +++ b/tests/auto/corelib/io/qdebug/qdebug.pro @@ -1,5 +1,5 @@ CONFIG += testcase parallel_test TARGET = tst_qdebug -QT = core testlib +QT = core testlib concurrent SOURCES = tst_qdebug.cpp DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 80144dba20..8fd830a839 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -44,6 +44,9 @@ #include <QtCore/QtDebug> #include <QtTest/QtTest> +#include <QtConcurrentRun> +#include <QFutureSynchronizer> + class tst_QDebug: public QObject { Q_OBJECT @@ -59,6 +62,7 @@ private slots: void qDebugQLatin1String() const; void textStreamModifiers() const; void defaultMessagehandler() const; + void threadSafety() const; }; void tst_QDebug::assignment() const @@ -305,5 +309,41 @@ void tst_QDebug::defaultMessagehandler() const QVERIFY(same); } +QMutex s_mutex; +QStringList s_messages; +QSemaphore s_sema; + +static void threadSafeMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) +{ + QMutexLocker lock(&s_mutex); + s_messages.append(msg); + Q_UNUSED(type); + Q_UNUSED(context); +} + +static void doDebug() // called in each thread +{ + s_sema.acquire(); + qDebug() << "doDebug"; +} + +void tst_QDebug::threadSafety() const +{ + MessageHandlerSetter mhs(threadSafeMessageHandler); + const int numThreads = 10; + QThreadPool::globalInstance()->setMaxThreadCount(numThreads); + QFutureSynchronizer<void> sync; + for (int i = 0; i < numThreads; ++i) { + sync.addFuture(QtConcurrent::run(&doDebug)); + } + s_sema.release(numThreads); + sync.waitForFinished(); + QMutexLocker lock(&s_mutex); + QCOMPARE(s_messages.count(), numThreads); + for (int i = 0; i < numThreads; ++i) { + QCOMPARE(s_messages.at(i), QStringLiteral("doDebug")); + } +} + QTEST_MAIN(tst_QDebug); #include "tst_qdebug.moc" |