Skip to content

Commit cd0a9a4

Browse files
committed
Review remarks (improve tests)
1 parent 5e89b95 commit cd0a9a4

7 files changed

Lines changed: 58 additions & 37 deletions

File tree

src/ProtocolAdapter/adapterclient.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ void AdapterClient::onResponseReceived(int id, const QString& method, const QJso
114114
{
115115
qCWarning(scopeComm) << "AdapterClient: unexpected non-object result for" << method;
116116
_handshakeTimer.stop();
117+
/* Set IDLE before stop() so onProcessFinished's IDLE guard suppresses any
118+
duplicate sessionError emission when the process exits asynchronously. */
117119
_state = State::IDLE;
118120
_pProcess->stop();
119121
emit sessionError(QString("Unexpected non-object result for %1").arg(method));
@@ -128,6 +130,8 @@ void AdapterClient::onErrorReceived(int id, const QString& method, const QJsonOb
128130
qCWarning(scopeComm) << "AdapterClient: error for" << method << ":" << errorMsg;
129131

130132
State previousState = _state;
133+
/* Set IDLE before stop() so onProcessFinished's IDLE guard suppresses any
134+
duplicate sessionError emission when the process exits asynchronously. */
131135
_state = State::IDLE;
132136
_pProcess->stop();
133137

src/ProtocolAdapter/adapterclient.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ class AdapterClient : public QObject
2626
Q_OBJECT
2727

2828
public:
29-
explicit AdapterClient(AdapterProcess* pProcess,
30-
QObject* parent = nullptr,
31-
int handshakeTimeoutMs = cHandshakeTimeoutMs);
29+
explicit AdapterClient(AdapterProcess* pProcess, QObject* parent = nullptr, int handshakeTimeoutMs = 10000);
3230
~AdapterClient();
3331

3432
/*!

src/communication/modbuspoll.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ void ModbusPoll::startCommunication(QList<DataPoint>& registerList)
5454
_registerList = registerList;
5555
_bPollActive = true;
5656

57+
/* Re-establish auto-restart in case it was disconnected by a prior session error */
58+
connect(_pAdapterClient, &AdapterClient::sessionStopped, this, &ModbusPoll::initAdapter, Qt::UniqueConnection);
59+
5760
qCInfo(scopeComm) << QString("Start logging: %1").arg(FormatDateTime::currentDateTime());
5861

5962
resetCommunicationStats();

src/communication/modbuspoll.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ class ModbusPoll : public QObject
2727
bool isActive();
2828
void resetCommunicationStats();
2929

30+
void onAdapterDiagnostic(const QString& level, const QString& message);
31+
3032
signals:
3133
void registerDataReady(ResultDoubleList registers);
3234

3335
private slots:
3436
void triggerRegisterRead();
3537
void onReadDataResult(ResultDoubleList results);
3638
void onDescribeResult(const QJsonObject& description);
37-
void onAdapterDiagnostic(const QString& level, const QString& message);
3839

3940
private:
4041
QStringList buildRegisterExpressions(const QList<DataPoint>& registerList);

tests/communication/tst_modbuspoll.cpp

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@
77
#include <QTest>
88

99
namespace {
10-
QtMsgType g_capturedType{};
11-
QString g_capturedMessage{};
10+
QtMsgType& capturedType()
11+
{
12+
static QtMsgType t{};
13+
return t;
14+
}
15+
QString& capturedMessage()
16+
{
17+
static QString m;
18+
return m;
19+
}
1220

1321
void captureHandler(QtMsgType type, const QMessageLogContext&, const QString& msg)
1422
{
15-
g_capturedType = type;
16-
g_capturedMessage = msg;
23+
capturedType() = type;
24+
capturedMessage() = msg;
1725
}
1826
} // namespace
1927

@@ -34,56 +42,62 @@ void TestModbusPoll::cleanup()
3442

3543
void TestModbusPoll::diagnosticDebugLevel()
3644
{
37-
g_capturedType = QtMsgType{};
38-
g_capturedMessage = QString{};
45+
capturedType() = QtMsgType{};
46+
capturedMessage() = QString{};
3947
QtMessageHandler previous = qInstallMessageHandler(captureHandler);
40-
bool invoked =
41-
QMetaObject::invokeMethod(_pModbusPoll, "onAdapterDiagnostic", Q_ARG(QString, QStringLiteral("debug")),
42-
Q_ARG(QString, QStringLiteral("polling started")));
43-
QVERIFY(invoked);
48+
_pModbusPoll->onAdapterDiagnostic(QStringLiteral("debug"), QStringLiteral("polling started"));
4449
qInstallMessageHandler(previous);
4550

46-
QCOMPARE(g_capturedType, QtDebugMsg);
47-
QVERIFY(g_capturedMessage.contains(QStringLiteral("polling started")));
51+
QCOMPARE(capturedType(), QtDebugMsg);
52+
QVERIFY(capturedMessage().contains(QStringLiteral("polling started")));
4853
}
4954

5055
void TestModbusPoll::diagnosticInfoLevel()
5156
{
52-
g_capturedType = QtMsgType{};
53-
g_capturedMessage = QString{};
57+
capturedType() = QtMsgType{};
58+
capturedMessage() = QString{};
5459
QtMessageHandler previous = qInstallMessageHandler(captureHandler);
55-
QMetaObject::invokeMethod(_pModbusPoll, "onAdapterDiagnostic", Q_ARG(QString, QStringLiteral("info")),
56-
Q_ARG(QString, QStringLiteral("session active")));
60+
_pModbusPoll->onAdapterDiagnostic(QStringLiteral("info"), QStringLiteral("session active"));
5761
qInstallMessageHandler(previous);
5862

59-
QCOMPARE(g_capturedType, QtInfoMsg);
60-
QVERIFY(g_capturedMessage.contains(QStringLiteral("session active")));
63+
QCOMPARE(capturedType(), QtInfoMsg);
64+
QVERIFY(capturedMessage().contains(QStringLiteral("session active")));
6165
}
6266

6367
void TestModbusPoll::diagnosticWarningLevel()
6468
{
65-
g_capturedType = QtMsgType{};
66-
g_capturedMessage = QString{};
69+
capturedType() = QtMsgType{};
70+
capturedMessage() = QString{};
71+
QtMessageHandler previous = qInstallMessageHandler(captureHandler);
72+
_pModbusPoll->onAdapterDiagnostic(QStringLiteral("warning"), QStringLiteral("register read failed"));
73+
qInstallMessageHandler(previous);
74+
75+
QCOMPARE(capturedType(), QtWarningMsg);
76+
QVERIFY(capturedMessage().contains(QStringLiteral("register read failed")));
77+
}
78+
79+
void TestModbusPoll::diagnosticErrorLevel()
80+
{
81+
capturedType() = QtMsgType{};
82+
capturedMessage() = QString{};
6783
QtMessageHandler previous = qInstallMessageHandler(captureHandler);
68-
QMetaObject::invokeMethod(_pModbusPoll, "onAdapterDiagnostic", Q_ARG(QString, QStringLiteral("warning")),
69-
Q_ARG(QString, QStringLiteral("register read failed")));
84+
_pModbusPoll->onAdapterDiagnostic(QStringLiteral("error"), QStringLiteral("fatal adapter fault"));
7085
qInstallMessageHandler(previous);
7186

72-
QCOMPARE(g_capturedType, QtWarningMsg);
73-
QVERIFY(g_capturedMessage.contains(QStringLiteral("register read failed")));
87+
QCOMPARE(capturedType(), QtCriticalMsg);
88+
QVERIFY(capturedMessage().contains(QStringLiteral("fatal adapter fault")));
7489
}
7590

7691
void TestModbusPoll::diagnosticUnknownLevel()
7792
{
78-
g_capturedType = QtMsgType{};
79-
g_capturedMessage = QString{};
93+
capturedType() = QtMsgType{};
94+
capturedMessage() = QString{};
8095
QtMessageHandler previous = qInstallMessageHandler(captureHandler);
81-
QMetaObject::invokeMethod(_pModbusPoll, "onAdapterDiagnostic", Q_ARG(QString, QStringLiteral("critical")),
82-
Q_ARG(QString, QStringLiteral("unexpected error")));
96+
_pModbusPoll->onAdapterDiagnostic(QStringLiteral("critical"), QStringLiteral("unexpected error"));
8397
qInstallMessageHandler(previous);
8498

85-
QCOMPARE(g_capturedType, QtWarningMsg);
86-
QVERIFY(g_capturedMessage.contains(QStringLiteral("unknown diagnostic level")));
99+
QCOMPARE(capturedType(), QtWarningMsg);
100+
QVERIFY(capturedMessage().contains(QStringLiteral("unknown diagnostic level")));
87101
}
88102

89103
QTEST_GUILESS_MAIN(TestModbusPoll)

tests/communication/tst_modbuspoll.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ private slots:
1616
void diagnosticDebugLevel();
1717
void diagnosticInfoLevel();
1818
void diagnosticWarningLevel();
19+
void diagnosticErrorLevel();
1920
void diagnosticUnknownLevel();
2021

2122
private:

tests/models/tst_diagnosticmodel.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
#ifndef TEST_DIAGNOSTICMODEL_H__
3-
#define TEST_DIAGNOSTICMODEL_H__
2+
#ifndef TST_DIAGNOSTICMODEL_H
3+
#define TST_DIAGNOSTICMODEL_H
44

55
#include <QObject>
66

@@ -26,4 +26,4 @@ private slots:
2626
QString _category;
2727
};
2828

29-
#endif /* TEST_DIAGNOSTICMODEL_H__ */
29+
#endif // TST_DIAGNOSTICMODEL_H

0 commit comments

Comments
 (0)