Skip to content

Commit

Permalink
Fix event handling: DO NOT USE EVENT-LOOPS in run/runDirect
Browse files Browse the repository at this point in the history
  • Loading branch information
iakov committed Oct 5, 2024
1 parent 3818448 commit f7bec78
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 14 deletions.
3 changes: 3 additions & 0 deletions tests/common.pri
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ OTHER_FILES += \
$$PWD/test-system-config.xml \
$$PWD/test-model-config.xml \

implementationIncludes(tests/testUtils)
links(testUtils)

trik_new_age {
copyToDestdir($$PWD/kernel-4.14/test-system-config.xml, now)
copyToDestdir($$PWD/kernel-4.14/test-model-config.xml, now)
Expand Down
7 changes: 5 additions & 2 deletions tests/mainTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@
#include <QtCore/qglobal.h>
#include <QtCore/QCoreApplication>


#include <trikKernel/coreDumping.h>
#include <trikKernel/loggingHelper.h>

#include <testUtils/eventFilter.h>

int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);

QCoreApplication app(argc, argv);

Q_UNUSED(app);
tests::utils::EventFilter eventFilter;
// useful to debug events
// app.installEventFilter(&eventFilter);

trikKernel::LoggingHelper loggingHelper(".", QsLogging::Level::WarnLevel);
Q_UNUSED(loggingHelper);
Expand Down
28 changes: 28 additions & 0 deletions tests/testUtils/include/testUtils/eventFilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright 2024, Iakov Kirilenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */
#pragma once
#include <QObject>
#include "testUtilsDeclSpec.h"

namespace tests {
namespace utils {
class TESTUTILS_EXPORT EventFilter: public QObject {
Q_OBJECT
public:
explicit EventFilter(QObject *parent = nullptr):QObject(parent) {}
bool eventFilter(QObject *o, QEvent *e) override;
};

}
}
35 changes: 35 additions & 0 deletions tests/testUtils/src/eventFilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Copyright 2024, Iakov Kirilenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */

#include "eventFilter.h"
#include <QDebug>
#include <QtCore/private/qobject_p.h>
#include <QMetaMethod>

bool tests::utils::EventFilter::eventFilter(QObject *o, QEvent *e) {
QMessageLogger log;
auto print = log.debug();
print << o << e;
switch(e->type()) {
default:break;
case QEvent::Type::MetaCall:
QMetaCallEvent * mev = static_cast<QMetaCallEvent*>(e);
QMetaMethod slot = o->metaObject()->method(mev->id());
print << slot.methodSignature() << slot.methodType() << slot.name() << slot.typeName();
break;
}


return false;
}
4 changes: 3 additions & 1 deletion tests/testUtils/testUtils.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ TEMPLATE = lib

DEFINES += TESTUTILS_LIBRARY

QT += network
QT += network core-private

interfaceIncludes(trikNetwork)
links(trikNetwork)
Expand All @@ -27,7 +27,9 @@ HEADERS += \
$$PWD/include/testUtils/tcpClientSimulator.h \
$$PWD/include/testUtils/wait.h \
$$PWD/include/testUtils/testUtilsDeclSpec.h \
$$PWD/include/testUtils/eventFilter.h

SOURCES += \
$$PWD/src/tcpClientSimulator.cpp \
$$PWD/src/wait.cpp \
$$PWD/src/eventFilter.cpp \
8 changes: 4 additions & 4 deletions tests/trikPyRunnerTests/trikPyRunnerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ int TrikPyRunnerTest::run(const QString &script)
if (!e.isEmpty()) {
rc = EXIT_SCRIPT_ERROR;
std::cerr << qPrintable(e) << std::endl;
}
QCoreApplication::processEvents(); // for stdout messages
}
l.exit(rc);
}, Qt::QueuedConnection ) ; // prevent `exit` before `exec` via QueuedConnection
mStdOut.clear();
mScriptRunner->run(script, "_.py");
auto code = l.exec();
QCoreApplication::processEvents(); // for stdout messages
std::cout << qPrintable(mStdOut) << std::endl;
return code;
}
Expand All @@ -74,14 +74,14 @@ int TrikPyRunnerTest::runDirectCommandAndWaitForQuit(const QString &script)
{
QEventLoop l;
QObject::connect(&*mScriptRunner, &trikScriptRunner::TrikScriptRunnerInterface::completed
, &l, [&l](const QString &e) {
QCoreApplication::processEvents(); // dispatch events for print/stdout
, &l, [&l](const QString &e) {
l.exit(e.isEmpty() ? EXIT_SCRIPT_SUCCESS
: (qDebug() << e, EXIT_SCRIPT_ERROR));
}, Qt::QueuedConnection ) ; // prevent `exit` before `exec` via QueuedConnection
mStdOut.clear();
mScriptRunner->runDirectCommand(script);
auto code = l.exec();
QCoreApplication::processEvents(); // dispatch events for print/stdout
std::cout << mStdOut.toStdString() << std::endl;
return code;
}
Expand Down
7 changes: 0 additions & 7 deletions trikScriptRunner/src/trikScriptRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,11 @@ void TrikScriptRunner::run(const QString &script, ScriptType stype, const QStrin
abortAll(); // FIXME: or fetchRunner(stype)->abort()? or abort(/*last*/)?

fetchRunner(stype)->run(script, fileName);

for(QEventLoop l; l.processEvents(); ) {
// Enforce events dispatch before return, f.e. handling of stdout messages
}
}

void TrikScriptRunner::runDirectCommand(const QString &command)
{
fetchRunner(mLastRunner)->runDirectCommand(command);
for(QEventLoop l; l.processEvents();) {
// Enforce events dispatch before return, f.e. handling of stdout messages
}
}

void TrikScriptRunner::abort()
Expand Down

0 comments on commit f7bec78

Please sign in to comment.