Repository navigation
Expand file tree
/
Copy pathtest_mqtt_manager.cpp
More file actions
73 lines (60 loc) · 2.32 KB
/
Copy pathtest_mqtt_manager.cpp
File metadata and controls
73 lines (60 loc) · 2.32 KB
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
#include "network/MqttManager.h"
#include <QSignalSpy>
#include <QTimer>
#include <QtTest>
class TestMqttManager : public QObject {
Q_OBJECT
private slots:
void testIdentifierRejectedErrorMentionsEngineRegistration() {
#ifndef RM_HAS_MQTT
QSKIP("Paho MQTT support is not enabled in this build");
#else
MqttManager manager;
QSignalSpy completedSpy(&manager, &MqttManager::connectCompleted);
QSignalSpy errorSpy(&manager, &MqttManager::errorOccurred);
QVERIFY(QMetaObject::invokeMethod(&manager, "onConnectCompleted",
Qt::DirectConnection, Q_ARG(int, 2)));
QCOMPARE(completedSpy.count(), 1);
QCOMPARE(errorSpy.count(), 1);
const QString completedError = completedSpy.at(0).at(1).toString();
QVERIFY(completedError.contains(QStringLiteral("CONNACK(2)")));
QVERIFY(completedError.contains(QStringLiteral("custom-client registration")));
#endif
}
void testInvalidClientIdDoesNotRetry() {
#ifndef RM_HAS_MQTT
QSKIP("Paho MQTT support is not enabled in this build");
#else
MqttManager manager;
QSignalSpy completedSpy(&manager, &MqttManager::connectCompleted);
QVERIFY(QMetaObject::invokeMethod(&manager, "onConnectCompleted",
Qt::DirectConnection, Q_ARG(int, 133)));
QCOMPARE(completedSpy.count(), 1);
QTimer *reconnectTimer = manager.findChild<QTimer *>();
QVERIFY(reconnectTimer != nullptr);
QVERIFY(!reconnectTimer->isActive());
#endif
}
void testDisconnectInFailureCallbackCancelsRetryTimer() {
#ifndef RM_HAS_MQTT
QSKIP("Paho MQTT support is not enabled in this build");
#else
MqttManager manager;
QSignalSpy completedSpy(&manager, &MqttManager::connectCompleted);
connect(&manager, &MqttManager::connectCompleted, &manager,
[&manager](bool success, const QString &) {
if (!success) {
manager.disconnect();
}
});
QVERIFY(QMetaObject::invokeMethod(&manager, "onConnectCompleted",
Qt::DirectConnection, Q_ARG(int, -1)));
QCOMPARE(completedSpy.count(), 1);
QTimer *reconnectTimer = manager.findChild<QTimer *>();
QVERIFY(reconnectTimer != nullptr);
QVERIFY(!reconnectTimer->isActive());
#endif
}
};
QTEST_MAIN(TestMqttManager)
#include "test_mqtt_manager.moc"