-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial sustainml_cpp blackbox and sustainml_modules communication te…
…st suites (#22) * Refs #19980: BlackboxTests initial suite Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com> * Refs #19980: Convert sustainml_modules into a CMake package Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com> * Refs #19980: Communication tests infraestructure Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com> * Refs #19980: Communication tests Python Script and Test Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com> * Refs #19980: Build Linux sustainml_modules tests in CI Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com> * Refs #19980: Linter Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com> * Refs #19980: Rev suggestions Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com> --------- Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>
- Loading branch information
Showing
37 changed files
with
8,472 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
# | ||
# 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. | ||
|
||
|
||
file(GLOB BLACKBOXTESTS_TEST_SOURCE "common/BlackboxTests*.cpp") | ||
set(BLACKBOXTESTS_SOURCE ${BLACKBOXTESTS_TEST_SOURCE} | ||
${PROJECT_SOURCE_DIR}/src/cpp/types/typesImpl.cxx | ||
${PROJECT_SOURCE_DIR}/src/cpp/types/typesImplPubSubTypes.cxx | ||
|
||
helpers/data_generators.cpp | ||
) | ||
add_executable(BlackboxTests ${BLACKBOXTESTS_SOURCE}) | ||
|
||
target_link_libraries(BlackboxTests | ||
sustainml_cpp | ||
fastrtps | ||
fastcdr | ||
foonathan_memory | ||
GTest::gtest) | ||
|
||
gtest_discover_tests(BlackboxTests) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
// Copyright 2023 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// 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. | ||
|
||
/** | ||
* @file ManagedNode.hpp | ||
* | ||
*/ | ||
|
||
#ifndef _TEST_BLACKBOX_MANAGEDNODE_HPP_ | ||
#define _TEST_BLACKBOX_MANAGEDNODE_HPP_ | ||
|
||
#include <future> | ||
|
||
#include <sustainml_cpp/nodes/CarbonFootprintNode.hpp> | ||
#include <sustainml_cpp/nodes/HardwareResourcesNode.hpp> | ||
#include <sustainml_cpp/nodes/MLModelNode.hpp> | ||
#include <sustainml_cpp/nodes/TaskEncoderNode.hpp> | ||
|
||
template <size_t n_inputs, typename ... PACK> | ||
struct GenericTaskListener : public sustainml::core::Callable<PACK...> | ||
{ | ||
|
||
GenericTaskListener( | ||
std::function<void(PACK&...)>& fn) | ||
: functor_(fn) | ||
{ | ||
|
||
} | ||
|
||
virtual ~GenericTaskListener() | ||
{ | ||
} | ||
|
||
virtual void on_new_task_available( | ||
PACK&... args) override | ||
{ | ||
return functor_(std::forward<decltype(args)>(args)...); | ||
} | ||
|
||
std::function<void(PACK&...)> functor_; | ||
}; | ||
|
||
template <typename _NODE_TYPE, typename _LISTENER_TYPE, typename ... Args> | ||
class ManagedNode | ||
{ | ||
using functor_t = std::function<void (Args&...)>; | ||
friend class ManagedNodeListener; | ||
|
||
struct ManagedNodeListener : public _LISTENER_TYPE | ||
{ | ||
ManagedNodeListener( | ||
ManagedNode* parent, | ||
const functor_t& functor = nullptr) | ||
: functor_(functor) | ||
, managed_node_(parent) | ||
{ | ||
|
||
} | ||
|
||
virtual void on_new_task_available( | ||
Args&... args) override | ||
{ | ||
std::cout << "New task available in " << managed_node_->node_.name() << std::endl; | ||
managed_node_->received_samples_.fetch_add(1); | ||
|
||
if (functor_) | ||
{ | ||
functor_(std::forward<decltype(args)>(args)...); | ||
} | ||
|
||
managed_node_->cv_.notify_all(); | ||
} | ||
|
||
std::function<void(Args&...)> functor_; | ||
ManagedNode* managed_node_; | ||
}; | ||
|
||
public: | ||
|
||
ManagedNode( | ||
const functor_t& callback = nullptr) | ||
: listener_(this, callback) | ||
, node_(listener_) | ||
, received_samples_(0) | ||
, expected_samples_(0) | ||
{ | ||
|
||
} | ||
|
||
~ManagedNode() | ||
{ | ||
stop(); | ||
th_->join(); | ||
} | ||
|
||
void start() | ||
{ | ||
std::packaged_task<void()> task ([&]() | ||
{ | ||
node_.spin(); | ||
}); | ||
|
||
th_.reset(new std::thread(std::move(task))); | ||
} | ||
|
||
void stop() | ||
{ | ||
_NODE_TYPE::terminate(); | ||
} | ||
|
||
void prepare_expected_samples( | ||
const size_t& expected_samples) | ||
{ | ||
expected_samples_.store(expected_samples); | ||
} | ||
|
||
bool block_for_all() | ||
{ | ||
std::unique_lock<std::mutex> lock(mtx_); | ||
cv_.wait(lock, [this]() -> bool | ||
{ | ||
return expected_samples_ == received_samples_; | ||
}); | ||
|
||
return expected_samples_ == received_samples_; | ||
} | ||
|
||
template<class _Rep, | ||
class _Period | ||
> | ||
size_t block_for_all( | ||
const std::chrono::duration<_Rep, _Period>& max_wait) | ||
{ | ||
std::unique_lock<std::mutex> lock(mtx_); | ||
cv_.wait_for(lock, max_wait, [this]() -> bool | ||
{ | ||
return expected_samples_ == received_samples_; | ||
}); | ||
|
||
return expected_samples_ == received_samples_; | ||
} | ||
|
||
private: | ||
|
||
ManagedNodeListener listener_; | ||
_NODE_TYPE node_; | ||
|
||
std::mutex mtx_; | ||
std::condition_variable cv_; | ||
std::unique_ptr<std::thread> th_; | ||
|
||
std::atomic<size_t> received_samples_; | ||
std::atomic<size_t> expected_samples_; | ||
}; | ||
|
||
#endif // _TEST_BLACKBOX_MANAGEDNODE_HPP_ | ||
|
Oops, something went wrong.