From 64a494e44aee26645a4754622bc273370cec8b70 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Thu, 28 Nov 2019 20:47:55 +0100 Subject: [PATCH] Fix tests that didn't wait long enough for log entries to be written --- libnymea-core/logging/logengine.cpp | 8 ++++ libnymea-core/logging/logengine.h | 4 ++ tests/auto/jsonrpc/testjsonrpc.cpp | 62 +++++++++++++++++++++++++++-- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/libnymea-core/logging/logengine.cpp b/libnymea-core/logging/logengine.cpp index 57a6142d..59595366 100644 --- a/libnymea-core/logging/logengine.cpp +++ b/libnymea-core/logging/logengine.cpp @@ -274,6 +274,11 @@ DevicesFetchJob *LogEngine::fetchDevices() return fetchJob; } +bool LogEngine::jobsRunning() const +{ + return !m_jobQueue.isEmpty() || m_currentJob; +} + void LogEngine::setMaxLogEntries(int maxLogEntries, int overflow) { m_dbMaxSize = maxLogEntries; @@ -547,6 +552,7 @@ void LogEngine::enqueJob(DatabaseJob *job) void LogEngine::processQueue() { if (m_jobQueue.isEmpty()) { + emit jobsRunningChanged(); return; } @@ -554,6 +560,8 @@ void LogEngine::processQueue() return; } + emit jobsRunningChanged(); + if (m_dbMalformed) { qCWarning(dcLogEngine()) << "Database is malformed. Trying to recover..."; m_db.close(); diff --git a/libnymea-core/logging/logengine.h b/libnymea-core/logging/logengine.h index ef6113ff..b15e7ddd 100644 --- a/libnymea-core/logging/logengine.h +++ b/libnymea-core/logging/logengine.h @@ -54,6 +54,8 @@ public: LogEntriesFetchJob *fetchLogEntries(const LogFilter &filter = LogFilter()); DevicesFetchJob *fetchDevices(); + bool jobsRunning() const; + void setMaxLogEntries(int maxLogEntries, int overflow); void clearDatabase(); @@ -74,6 +76,8 @@ signals: void logEntryAdded(const LogEntry &logEntry); void logDatabaseUpdated(); + void jobsRunningChanged(); + private: bool initDB(const QString &username, const QString &password); void appendLogEntry(const LogEntry &entry); diff --git a/tests/auto/jsonrpc/testjsonrpc.cpp b/tests/auto/jsonrpc/testjsonrpc.cpp index 5da756d5..d783e15f 100644 --- a/tests/auto/jsonrpc/testjsonrpc.cpp +++ b/tests/auto/jsonrpc/testjsonrpc.cpp @@ -41,6 +41,7 @@ private: private slots: void initTestCase(); + void cleanup(); void testHandshake(); @@ -148,6 +149,13 @@ void TestJSONRPC::initTestCase() "Tests.debug=true"); } +void TestJSONRPC::cleanup() +{ + while (NymeaCore::instance()->logEngine()->jobsRunning()) { + qApp->processEvents(); + } +} + void TestJSONRPC::testHandshake() { QUuid newClientId = QUuid::createUuid(); @@ -846,16 +854,56 @@ void TestJSONRPC::ruleActiveChangedNotifications() QCOMPARE(notificationVariant.toMap().value("params").toMap().value("ruleId").toUuid().toString(), ruleId.toString()); QCOMPARE(notificationVariant.toMap().value("params").toMap().value("active").toBool(), true); + clientSpy.wait(); + + // Make sure the logg notification contains all the stuff we expect + QVariantList logEntryAddedVariants = checkNotifications(clientSpy, "Logging.LogEntryAdded"); + QVERIFY2(!logEntryAddedVariants.isEmpty(), "Did not get Logging.LogEntryAdded notification."); + bool found = false; + foreach (const QVariant &loggEntryAddedVariant, logEntryAddedVariants) { + if (loggEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("typeId").toUuid() == mockIntStateTypeId) { + found = true; + QCOMPARE(loggEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("source").toString(), QString("LoggingSourceStates")); + QCOMPARE(loggEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("value").toInt(), 20); + break; + } + } + QVERIFY2(found, "LogEntryAdded notification not received"); + spy.clear(); clientSpy.clear(); // set the rule inactive qDebug() << "setting mock int state to 42"; QNetworkRequest request2(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockIntStateTypeId.toString()).arg(42))); QNetworkReply *reply2 = nam.get(request2); - if (spy.count() == 0) spy.wait(); - QCOMPARE(spy.count(), 1); connect(reply2, SIGNAL(finished()), reply2, SLOT(deleteLater())); + // Waiting for notifications: + // Devices.StateChanged for the change we did + // Devices.EventTriggered + // Events.EventTriggered <-- deprecated + // Rules.RuleActiveChanged + // Logging.LogEntryAdded + // Devices.StateChanged for the change done by the rule + // Devices.EventTriggered + // Events.EventTriggered <-- deprecated + while (clientSpy.count() < 8) { + clientSpy.wait(); + } + + // Make sure the logg notification contains all the stuff we expect + logEntryAddedVariants = checkNotifications(clientSpy, "Logging.LogEntryAdded"); + QVERIFY2(!logEntryAddedVariants.isEmpty(), "Did not get Logging.LogEntryAdded notification."); + found = false; + foreach (const QVariant &loggEntryAddedVariant, logEntryAddedVariants) { + if (loggEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("typeId").toUuid() == mockIntStateTypeId) { + found = true; + QCOMPARE(loggEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("source").toString(), QString("LoggingSourceStates")); + QCOMPARE(loggEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("value").toInt(), 42); + break; + } + } + QVERIFY2(found, "LogEntryAdded notification not received"); if (clientSpy.count() == 0) clientSpy.wait(); notificationVariant = checkNotification(clientSpy, "Rules.RuleActiveChanged"); @@ -994,7 +1042,14 @@ void TestJSONRPC::stateChangeEmitsNotifications() QVERIFY2(found, "Could not find the correct Devices.StateChanged notification"); - clientSpy.wait(); + // Waiting for: + // Devices.StateChanged + // Devices.EventTriggered + // Events.EventTriggered <-- deprecated + // Logging.LogEntryAdded + while (clientSpy.count() < 4) { + clientSpy.wait(); + } // Make sure the logg notification contains all the stuff we expect QVariantList logEntryAddedVariants = checkNotifications(clientSpy, "Logging.LogEntryAdded"); @@ -1011,7 +1066,6 @@ void TestJSONRPC::stateChangeEmitsNotifications() QVERIFY2(found, "Could not find the corresponding Logging.LogEntryAdded notification"); - // Make sure the notification contains all the stuff we expect QVariantList eventTriggeredVariants = checkNotifications(clientSpy, "Events.EventTriggered"); QVERIFY2(!eventTriggeredVariants.isEmpty(), "Did not get Events.EventTriggered notification.");