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