| 1 |
// Copyright (C) 2023 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QCoreApplication> |
| 5 |
#include <QDBusConnection> |
| 6 |
#include <QDBusMessage> |
| 7 |
#include <QObject> |
| 8 |
#include <QTest> |
| 9 |
#include <QTimer> |
| 10 |
|
| 11 |
using namespace Qt::StringLiterals; |
| 12 |
|
| 13 |
static constexpr int MAX_TEST_DURATION_MS = 10000; |
| 14 |
static constexpr int NUM_MESSAGES = 20; |
| 15 |
|
| 16 |
class SignalReceiver : public QObject |
| 17 |
{ |
| 18 |
Q_OBJECT |
| 19 |
public: |
| 20 |
explicit SignalReceiver(QDBusConnection &connection, const QString &serviceName); |
| 21 |
|
| 22 |
int nextValue() const { return m_nextValue; } |
| 23 |
bool inOrder() const { return m_inOrder; } |
| 24 |
|
| 25 |
Q_SIGNALS: |
| 26 |
void testSignal(int); |
| 27 |
void done(); |
| 28 |
|
| 29 |
private Q_SLOTS: |
| 30 |
void testSlot(int number); |
| 31 |
|
| 32 |
private: |
| 33 |
int m_nextValue = 0; |
| 34 |
bool m_inOrder = true; |
| 35 |
}; |
| 36 |
|
| 37 |
SignalReceiver::SignalReceiver(QDBusConnection &connection, const QString &serviceName) |
| 38 |
{ |
| 39 |
connection.connect(serviceName, "/", {}, "testSignal", this, SLOT(testSlot(int))); |
| 40 |
} |
| 41 |
|
| 42 |
void SignalReceiver::testSlot(int number) |
| 43 |
{ |
| 44 |
if (m_nextValue != number) { |
| 45 |
m_inOrder = false; |
| 46 |
qWarning("Message out of sequence, expected: %d, received: %d", m_nextValue, number); |
| 47 |
} |
| 48 |
|
| 49 |
m_nextValue++; |
| 50 |
|
| 51 |
if (m_nextValue == NUM_MESSAGES) { |
| 52 |
Q_EMIT done(); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
class tst_QDBusConnection_SignalOrder : public QObject |
| 57 |
{ |
| 58 |
Q_OBJECT |
| 59 |
private Q_SLOTS: |
| 60 |
void signalOrder(); |
| 61 |
}; |
| 62 |
|
| 63 |
// This is a regression test for QTBUG-105457. The bug is a race condition, |
| 64 |
// so it cannot be reliably triggered at each test execution. |
| 65 |
void tst_QDBusConnection_SignalOrder::signalOrder() |
| 66 |
{ |
| 67 |
int argc = 1; |
| 68 |
static char appName[] = "tst_qdbusconnection_signalorder"; |
| 69 |
char *argv[] = { appName, 0 }; |
| 70 |
QCoreApplication app(argc, argv); |
| 71 |
|
| 72 |
const QString serviceName = |
| 73 |
u"org.qtproject.tst_dbusconnection_signalorder_%1"_s.arg(app.applicationPid()); |
| 74 |
|
| 75 |
auto connection = QDBusConnection::sessionBus(); |
| 76 |
|
| 77 |
QVERIFY(connection.isConnected()); |
| 78 |
QVERIFY(connection.registerService(serviceName)); |
| 79 |
|
| 80 |
// Limit the test execution time in case if something goes wrong inside |
| 81 |
// the signal receiver. |
| 82 |
QTimer::singleShot(MAX_TEST_DURATION_MS, &app, &QCoreApplication::quit); |
| 83 |
|
| 84 |
SignalReceiver signalReceiver(connection, serviceName); |
| 85 |
connect(&signalReceiver, &SignalReceiver::done, &app, &QCoreApplication::quit); |
| 86 |
|
| 87 |
QVERIFY(connection.registerObject("/", &signalReceiver, QDBusConnection::ExportAllSlots)); |
| 88 |
|
| 89 |
for (int i = 0; i < NUM_MESSAGES; i++) { |
| 90 |
auto testSignal = QDBusMessage::createSignal("/", serviceName, "testSignal"); |
| 91 |
testSignal << i; |
| 92 |
QVERIFY(connection.send(testSignal)); |
| 93 |
} |
| 94 |
|
| 95 |
app.exec(); |
| 96 |
|
| 97 |
QVERIFY(signalReceiver.inOrder()); |
| 98 |
QCOMPARE(signalReceiver.nextValue(), NUM_MESSAGES); |
| 99 |
} |
| 100 |
|
| 101 |
QTEST_APPLESS_MAIN(tst_QDBusConnection_SignalOrder) |
| 102 |
|
| 103 |
#include "tst_qdbusconnection_signalorder.moc" |