From f7bec78a1f391177b4a5054e4362dab8fee1cceb Mon Sep 17 00:00:00 2001 From: iakov Date: Sat, 5 Oct 2024 16:26:17 +0300 Subject: [PATCH] Fix event handling: DO NOT USE EVENT-LOOPS in run/runDirect --- tests/common.pri | 3 ++ tests/mainTest.cpp | 7 ++-- .../testUtils/include/testUtils/eventFilter.h | 28 +++++++++++++++ tests/testUtils/src/eventFilter.cpp | 35 +++++++++++++++++++ tests/testUtils/testUtils.pro | 4 ++- tests/trikPyRunnerTests/trikPyRunnerTest.cpp | 8 ++--- trikScriptRunner/src/trikScriptRunner.cpp | 7 ---- 7 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 tests/testUtils/include/testUtils/eventFilter.h create mode 100644 tests/testUtils/src/eventFilter.cpp diff --git a/tests/common.pri b/tests/common.pri index 26c5d7c6e..962fbb74a 100644 --- a/tests/common.pri +++ b/tests/common.pri @@ -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) diff --git a/tests/mainTest.cpp b/tests/mainTest.cpp index 9a8b929fb..b7a847f33 100644 --- a/tests/mainTest.cpp +++ b/tests/mainTest.cpp @@ -17,17 +17,20 @@ #include #include - #include #include +#include + 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); diff --git a/tests/testUtils/include/testUtils/eventFilter.h b/tests/testUtils/include/testUtils/eventFilter.h new file mode 100644 index 000000000..f08ed3117 --- /dev/null +++ b/tests/testUtils/include/testUtils/eventFilter.h @@ -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 +#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; +}; + +} +} diff --git a/tests/testUtils/src/eventFilter.cpp b/tests/testUtils/src/eventFilter.cpp new file mode 100644 index 000000000..324d126b4 --- /dev/null +++ b/tests/testUtils/src/eventFilter.cpp @@ -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 +#include +#include + +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(e); + QMetaMethod slot = o->metaObject()->method(mev->id()); + print << slot.methodSignature() << slot.methodType() << slot.name() << slot.typeName(); + break; + } + + + return false; +} diff --git a/tests/testUtils/testUtils.pro b/tests/testUtils/testUtils.pro index a342bfd03..78ae04f91 100644 --- a/tests/testUtils/testUtils.pro +++ b/tests/testUtils/testUtils.pro @@ -18,7 +18,7 @@ TEMPLATE = lib DEFINES += TESTUTILS_LIBRARY -QT += network +QT += network core-private interfaceIncludes(trikNetwork) links(trikNetwork) @@ -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 \ diff --git a/tests/trikPyRunnerTests/trikPyRunnerTest.cpp b/tests/trikPyRunnerTests/trikPyRunnerTest.cpp index a9622b27e..00d34f3f6 100644 --- a/tests/trikPyRunnerTests/trikPyRunnerTest.cpp +++ b/tests/trikPyRunnerTests/trikPyRunnerTest.cpp @@ -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; } @@ -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; } diff --git a/trikScriptRunner/src/trikScriptRunner.cpp b/trikScriptRunner/src/trikScriptRunner.cpp index ad778b779..ef3d3ce09 100644 --- a/trikScriptRunner/src/trikScriptRunner.cpp +++ b/trikScriptRunner/src/trikScriptRunner.cpp @@ -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()