Fix tests that didn't wait long enough for log entries to be written

This commit is contained in:
Michael Zanetti 2019-11-28 20:47:55 +01:00
parent 619bbf76f3
commit 64a494e44a
3 changed files with 70 additions and 4 deletions

View File

@ -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();

View File

@ -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);

View File

@ -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.");