From a6add5fb062fb710921774f359c8acb2382fd39d Mon Sep 17 00:00:00 2001 From: Jacques Guillou Date: Sat, 19 Sep 2020 22:22:42 +0200 Subject: [PATCH] Add IPC property consistency test --- tests/CMakeLists.txt | 1 + tests/propertyconsistency/CMakeLists.txt | 27 ++++++ tests/propertyconsistency/client.cpp | 74 +++++++++++++++ tests/propertyconsistency/client.h | 49 ++++++++++ .../interface/interfaces.qface | 39 ++++++++ tests/propertyconsistency/server.cpp | 91 +++++++++++++++++++ tests/propertyconsistency/server.h | 72 +++++++++++++++ 7 files changed, 353 insertions(+) create mode 100644 tests/propertyconsistency/CMakeLists.txt create mode 100644 tests/propertyconsistency/client.cpp create mode 100644 tests/propertyconsistency/client.h create mode 100644 tests/propertyconsistency/interface/interfaces.qface create mode 100644 tests/propertyconsistency/server.cpp create mode 100644 tests/propertyconsistency/server.h diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 6dafed20..4ab6fc6f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -79,3 +79,4 @@ if(NOT ${FACELIFT_DISABLE_GTEST}) endif() add_subdirectory(objectregistry) +add_subdirectory(propertyconsistency) diff --git a/tests/propertyconsistency/CMakeLists.txt b/tests/propertyconsistency/CMakeLists.txt new file mode 100644 index 00000000..c1c3636f --- /dev/null +++ b/tests/propertyconsistency/CMakeLists.txt @@ -0,0 +1,27 @@ +if (TARGET FaceliftIPCLibDBus) + + facelift_add_interface(property_consistency_gen + INTERFACE_DEFINITION_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/interface) + + facelift_add_executable(property-consistency-test-client + HEADERS + client.h + SOURCES + client.cpp + LINK_LIBRARIES + Qt5::Core + property_consistency_gen + ) + + facelift_add_test(property-consistency-test + HEADERS + server.h + SOURCES + server.cpp + PRIVATE_DEFINITIONS CLIENT_EXECUTABLE_LOCATION="$" + LINK_LIBRARIES + Qt5::Core + property_consistency_gen + ) + +endif() diff --git a/tests/propertyconsistency/client.cpp b/tests/propertyconsistency/client.cpp new file mode 100644 index 00000000..37df7de1 --- /dev/null +++ b/tests/propertyconsistency/client.cpp @@ -0,0 +1,74 @@ +/********************************************************************** +** +** Copyright (C) 2020 Luxoft Sweden AB +** +** This file is part of the FaceLift project +** +** Permission is hereby granted, free of charge, to any person +** obtaining a copy of this software and associated documentation files +** (the "Software"), to deal in the Software without restriction, +** including without limitation the rights to use, copy, modify, merge, +** publish, distribute, sublicense, and/or sell copies of the Software, +** and to permit persons to whom the Software is furnished to do so, +** subject to the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +** BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +** ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +** SOFTWARE. +** +** SPDX-License-Identifier: MIT +** +**********************************************************************/ +#include "client.h" +#include + +void tests::ipc::Tester::start() +{ + m_async = std::make_unique(); + + auto checkConsistency = [this]() { + if (m_async->boolProperty1() != m_async->boolProperty2()) { + qCritical() << "Inconsistency detected"; + qApp->exit(10); + } + }; + + connect(m_async.get(), &facelift::InterfaceBase::readyChanged, this, [this, checkConsistency]() { + if (m_async->ready()) { + checkConsistency(); + m_async->toggle(); + } + }); + + connect(m_async.get(), &IPCConsistencyTestInterfaceAsyncIPCProxy::boolProperty1Changed, this, checkConsistency); + connect(m_async.get(), &IPCConsistencyTestInterfaceAsyncIPCProxy::boolProperty2Changed, this, checkConsistency); + + QTimer::singleShot(3000, [&]() { + if (!m_async->ready()) { + qCritical() << "Server not found. path:" << m_async->objectPath(); + qApp->exit(2); + } else { + qApp->exit(0); + } + }); + + m_async->connectToServer(); +} + +int main(int argc, char** argv) +{ + QCoreApplication app(argc, argv); + + tests::ipc::Tester tester; + tester.start(); + + return app.exec(); +} diff --git a/tests/propertyconsistency/client.h b/tests/propertyconsistency/client.h new file mode 100644 index 00000000..d6e4c5db --- /dev/null +++ b/tests/propertyconsistency/client.h @@ -0,0 +1,49 @@ +/********************************************************************** +** +** Copyright (C) 2020 Luxoft Sweden AB +** +** This file is part of the FaceLift project +** +** Permission is hereby granted, free of charge, to any person +** obtaining a copy of this software and associated documentation files +** (the "Software"), to deal in the Software without restriction, +** including without limitation the rights to use, copy, modify, merge, +** publish, distribute, sublicense, and/or sell copies of the Software, +** and to permit persons to whom the Software is furnished to do so, +** subject to the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +** BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +** ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +** SOFTWARE. +** +** SPDX-License-Identifier: MIT +** +**********************************************************************/ +#pragma once + +#include +#include +#include "tests/ipc/IPCConsistencyTestInterfaceAsyncIPCProxy.h" + +namespace tests { +namespace ipc { + +class Tester : public QObject { + Q_OBJECT +public: + + void start(); + + std::unique_ptr m_async; +}; + +} // end namespace ipc +} // end namespace tests diff --git a/tests/propertyconsistency/interface/interfaces.qface b/tests/propertyconsistency/interface/interfaces.qface new file mode 100644 index 00000000..a9497f3b --- /dev/null +++ b/tests/propertyconsistency/interface/interfaces.qface @@ -0,0 +1,39 @@ +/********************************************************************** +** +** Copyright (C) 2020 Luxoft Sweden AB +** +** This file is part of the FaceLift project +** +** Permission is hereby granted, free of charge, to any person +** obtaining a copy of this software and associated documentation files +** (the "Software"), to deal in the Software without restriction, +** including without limitation the rights to use, copy, modify, merge, +** publish, distribute, sublicense, and/or sell copies of the Software, +** and to permit persons to whom the Software is furnished to do so, +** subject to the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +** BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +** ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +** SOFTWARE. +** +** SPDX-License-Identifier: MIT +** +**********************************************************************/ + +module tests.ipc 1.0 + +@ipc-async: true +@ipc-sync: true +interface IPCConsistencyTestInterface { + readonly bool boolProperty1; + readonly bool boolProperty2; + void toggle(); +} diff --git a/tests/propertyconsistency/server.cpp b/tests/propertyconsistency/server.cpp new file mode 100644 index 00000000..5c77f915 --- /dev/null +++ b/tests/propertyconsistency/server.cpp @@ -0,0 +1,91 @@ +/********************************************************************** +** +** Copyright (C) 2020 Luxoft Sweden AB +** +** This file is part of the FaceLift project +** +** Permission is hereby granted, free of charge, to any person +** obtaining a copy of this software and associated documentation files +** (the "Software"), to deal in the Software without restriction, +** including without limitation the rights to use, copy, modify, merge, +** publish, distribute, sublicense, and/or sell copies of the Software, +** and to permit persons to whom the Software is furnished to do so, +** subject to the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +** BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +** ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +** SOFTWARE. +** +** SPDX-License-Identifier: MIT +** +**********************************************************************/ +#include "server.h" +#include +#include + +namespace tests { +namespace ipc { + +IPCTestInterfaceImpl::IPCTestInterfaceImpl() +{ + m_adapter.registerService(this); + + connect(&m_timer, &QTimer::timeout, this, &IPCTestInterfaceImpl::toggle); + m_timer.start(100); +} + +void IPCTestInterfaceImpl::toggle() +{ + m_boolProperty = !m_boolProperty; + boolProperty1Changed(); + boolProperty2Changed(); +} + +} // end namespace ipc +} // end namespace tests + +int main(int argc, char** argv) { + + QCoreApplication app(argc, argv); + + tests::ipc::IPCTestInterfaceImpl server; + QProcess client; + + auto exitWithCode = [&app](int code) { + qCritical() << "Exiting with code" << code; + app.exit(code); + }; + + // Terminate with error after 10 seconds + QTimer::singleShot(10000, &app, [&]() { + qDebug() << "Test failed: timeout"; + client.close(); + exitWithCode(1); + }); + + // We terminate if the client process terminates + QObject::connect(&client, &QProcess::stateChanged, [&] (QProcess::ProcessState state) { + qWarning() << "Client process state" << state; + if (state == QProcess::ProcessState::NotRunning) { + qWarning() << "Client terminated with status" << client.exitStatus() << client.exitCode(); + if (client.exitStatus() != QProcess::ExitStatus::NormalExit) + exitWithCode(1); + else { + exitWithCode(client.exitCode()); + } + + } + }); + + client.start(CLIENT_EXECUTABLE_LOCATION); + + return app.exec(); +} diff --git a/tests/propertyconsistency/server.h b/tests/propertyconsistency/server.h new file mode 100644 index 00000000..6e8d389f --- /dev/null +++ b/tests/propertyconsistency/server.h @@ -0,0 +1,72 @@ +/********************************************************************** +** +** Copyright (C) 2020 Luxoft Sweden AB +** +** This file is part of the FaceLift project +** +** Permission is hereby granted, free of charge, to any person +** obtaining a copy of this software and associated documentation files +** (the "Software"), to deal in the Software without restriction, +** including without limitation the rights to use, copy, modify, merge, +** publish, distribute, sublicense, and/or sell copies of the Software, +** and to permit persons to whom the Software is furnished to do so, +** subject to the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +** BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +** ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +** SOFTWARE. +** +** SPDX-License-Identifier: MIT +** +**********************************************************************/ +#pragma once + +#ifndef CLIENT_EXECUTABLE_LOCATION +#error "CLIENT_EXECUTABLE_LOCATION must be be defined! Check CMakeLists.txt" +#endif + +#include +#include +#include "tests/ipc/IPCConsistencyTestInterfaceIPCAdapter.h" + +namespace tests { +namespace ipc { + +/** + * This implementation exposes 2 properties which always contain identical values + * The IPC framework ensures that client side proxy objects also provide the same values + */ +class IPCTestInterfaceImpl : public IPCConsistencyTestInterface { + Q_OBJECT +public: + IPCTestInterfaceImpl(); + + void toggle() override; + + const bool& boolProperty1() const override { + return m_boolProperty; + } + + const bool& boolProperty2() const override { + return m_boolProperty; + } + + bool ready() const override { + return true; + } + + IPCConsistencyTestInterfaceIPCAdapter m_adapter; + QTimer m_timer; + bool m_boolProperty = false; +}; + +} // end namespace ipc +} // end namespace tests