-
Notifications
You must be signed in to change notification settings - Fork 22
hmon: add C++ HMON unit tests #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arkjedrz
wants to merge
1
commit into
eclipse-score:main
Choose a base branch
from
qorix-group:arkjedrz_cpp-unit-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
111 changes: 111 additions & 0 deletions
111
score/health_monitor/src/cpp/tests/deadline_monitor_test.cpp
This file contains hidden or 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,111 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2026 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Apache License Version 2.0 which is available at | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| ********************************************************************************/ | ||
|
|
||
| #include "score/mw/health/deadline_monitor.h" | ||
| #include "score/mw/health/health_monitor.h" | ||
| #include <gtest/gtest.h> | ||
|
|
||
| using namespace score::mw::health; | ||
| using namespace score::mw::health::deadline; | ||
|
|
||
| TEST(DeadlineMonitorBuilder, New_Succeeds) | ||
| { | ||
| DeadlineMonitorBuilder deadline_monitor_builder; | ||
| } | ||
|
|
||
| TEST(DeadlineMonitorBuilder, AddDeadline_Succeeds) | ||
| { | ||
| using namespace std::chrono_literals; | ||
| DeadlineTag deadline_tag{"deadline"}; | ||
| TimeRange range{50ms, 150ms}; | ||
| auto deadline_monitor_builder{DeadlineMonitorBuilder{}.add_deadline(deadline_tag, range)}; | ||
|
NicolasFussberger marked this conversation as resolved.
|
||
| } | ||
|
|
||
| class DeadlineMonitorFixture : public ::testing::Test | ||
| { | ||
| protected: | ||
| std::optional<DeadlineMonitor> deadline_monitor_; | ||
|
|
||
| void SetUp() override | ||
| { | ||
| // Monitor must be obtained from HMON. | ||
| // Initialize deadline monitor builder. | ||
| using namespace std::chrono_literals; | ||
| MonitorTag deadline_monitor_tag{"deadline_monitor"}; | ||
| DeadlineTag deadline_tag{"deadline"}; | ||
| TimeRange range{50ms, 150ms}; | ||
| auto deadline_monitor_builder{DeadlineMonitorBuilder{}.add_deadline(deadline_tag, range)}; | ||
|
|
||
| // Build HMON, including deadline monitor. | ||
| auto hmon_build_result{HealthMonitorBuilder{} | ||
| .add_deadline_monitor(deadline_monitor_tag, std::move(deadline_monitor_builder)) | ||
| .build()}; | ||
| ASSERT_TRUE(hmon_build_result.has_value()); | ||
| auto hmon{std::move(hmon_build_result.value())}; | ||
|
|
||
| // Get deadline monitor. | ||
| auto get_deadline_monitor_result{hmon.get_deadline_monitor(deadline_monitor_tag)}; | ||
| ASSERT_TRUE(get_deadline_monitor_result.has_value()); | ||
| deadline_monitor_ = std::move(get_deadline_monitor_result.value()); | ||
| } | ||
| }; | ||
|
|
||
| TEST_F(DeadlineMonitorFixture, GetDeadline_Succeeds) | ||
| { | ||
| // Get deadline. | ||
| auto get_deadline_result{deadline_monitor_->get_deadline(DeadlineTag{"deadline"})}; | ||
| ASSERT_TRUE(get_deadline_result.has_value()); | ||
| } | ||
|
|
||
| TEST_F(DeadlineMonitorFixture, GetDeadline_Unknown) | ||
| { | ||
| // Get deadline. | ||
| auto get_deadline_result{deadline_monitor_->get_deadline(DeadlineTag{"unknown"})}; | ||
| ASSERT_FALSE(get_deadline_result.has_value()); | ||
| ASSERT_EQ(get_deadline_result.error(), Error::NotFound); | ||
| } | ||
|
|
||
| class DeadlineFixture : public DeadlineMonitorFixture | ||
| { | ||
| }; | ||
|
|
||
| TEST_F(DeadlineFixture, Start_Succeeds) | ||
| { | ||
| // Get deadline. | ||
| DeadlineTag deadline_tag{"deadline"}; | ||
| auto get_deadline_result{deadline_monitor_->get_deadline(deadline_tag)}; | ||
| ASSERT_TRUE(get_deadline_result.has_value()); | ||
| auto deadline{std::move(get_deadline_result.value())}; | ||
|
|
||
| // Try to start and stop deadline. | ||
| auto deadline_start_result{deadline.start()}; | ||
| ASSERT_TRUE(deadline_start_result.has_value()); | ||
| auto deadline_handle{std::move(deadline_start_result.value())}; | ||
|
|
||
| deadline_handle.stop(); | ||
| } | ||
|
|
||
| TEST_F(DeadlineFixture, Start_AlreadyRunning) | ||
| { | ||
| // Get deadline. | ||
| DeadlineTag deadline_tag{"deadline"}; | ||
| auto get_deadline_result{deadline_monitor_->get_deadline(deadline_tag)}; | ||
| ASSERT_TRUE(get_deadline_result.has_value()); | ||
| auto deadline{std::move(get_deadline_result.value())}; | ||
|
|
||
| // Try to start the deadline twice. | ||
| deadline.start(); | ||
| auto deadline_start_result{deadline.start()}; | ||
| ASSERT_FALSE(deadline_start_result.has_value()); | ||
| ASSERT_EQ(deadline_start_result.error(), Error::Failed); | ||
| } | ||
232 changes: 232 additions & 0 deletions
232
score/health_monitor/src/cpp/tests/health_monitor_test.cpp
This file contains hidden or 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,232 @@ | ||
| /******************************************************************************** | ||
| * Copyright (c) 2026 Contributors to the Eclipse Foundation | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information regarding copyright ownership. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Apache License Version 2.0 which is available at | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| ********************************************************************************/ | ||
|
|
||
| #include "score/mw/health/health_monitor.h" | ||
| #include "score/mw/health/deadline_monitor.h" | ||
| #include "score/mw/health/heartbeat_monitor.h" | ||
| #include "score/mw/health/logic_monitor.h" | ||
| #include <gtest/gtest.h> | ||
|
|
||
| using namespace score::mw::health; | ||
| using namespace score::mw::health::deadline; | ||
| using namespace score::mw::health::heartbeat; | ||
| using namespace score::mw::health::logic; | ||
|
|
||
| HeartbeatMonitorBuilder def_heartbeat_monitor_builder() | ||
| { | ||
| using namespace std::chrono_literals; | ||
| TimeRange range{100ms, 200ms}; | ||
| return HeartbeatMonitorBuilder{range}; | ||
| } | ||
|
|
||
| LogicMonitorBuilder def_logic_monitor_builder() | ||
| { | ||
| StateTag state1{"state1"}; | ||
| StateTag state2{"state2"}; | ||
| return LogicMonitorBuilder{state1}.add_state(state1, {state2}).add_state(state2, {state1}); | ||
| } | ||
|
|
||
| TEST(HealthMonitorBuilder, New_Succeeds) | ||
| { | ||
| // Check able to construct and destruct only. | ||
| HealthMonitorBuilder health_monitor_builder; | ||
| } | ||
|
|
||
| TEST(HealthMonitorBuilder, Build_Succeeds) | ||
| { | ||
| MonitorTag deadline_monitor_tag{"deadline_monitor"}; | ||
| DeadlineMonitorBuilder deadline_monitor_builder; | ||
| MonitorTag heartbeat_monitor_tag{"heartbeat_monitor"}; | ||
| auto heartbeat_monitor_builder{def_heartbeat_monitor_builder()}; | ||
| MonitorTag logic_monitor_tag{"logic_monitor"}; | ||
| auto logic_monitor_builder{def_logic_monitor_builder()}; | ||
|
|
||
| auto result{HealthMonitorBuilder{} | ||
| .add_deadline_monitor(deadline_monitor_tag, std::move(deadline_monitor_builder)) | ||
| .add_heartbeat_monitor(heartbeat_monitor_tag, std::move(heartbeat_monitor_builder)) | ||
| .add_logic_monitor(logic_monitor_tag, std::move(logic_monitor_builder)) | ||
| .build()}; | ||
| ASSERT_TRUE(result.has_value()); | ||
| } | ||
|
|
||
| TEST(HealthMonitorBuilder, Build_InvalidCycles) | ||
| { | ||
| using namespace std::chrono_literals; | ||
| auto result{HealthMonitorBuilder{}.with_supervisor_api_cycle(123ms).with_internal_processing_cycle(100ms).build()}; | ||
| ASSERT_FALSE(result.has_value()); | ||
| ASSERT_EQ(result.error(), Error::InvalidArgument); | ||
| } | ||
|
|
||
| TEST(HealthMonitorBuilder, Build_NoMonitors) | ||
| { | ||
| auto result{HealthMonitorBuilder{}.build()}; | ||
| ASSERT_FALSE(result.has_value()); | ||
| ASSERT_EQ(result.error(), Error::WrongState); | ||
| } | ||
|
|
||
| TEST(HealthMonitor, GetDeadlineMonitor_Available) | ||
| { | ||
| MonitorTag deadline_monitor_tag{"deadline_monitor"}; | ||
| DeadlineMonitorBuilder deadline_monitor_builder; | ||
| auto health_monitor{HealthMonitorBuilder{} | ||
| .add_deadline_monitor(deadline_monitor_tag, std::move(deadline_monitor_builder)) | ||
| .build() | ||
| .value()}; | ||
|
|
||
| auto result{health_monitor.get_deadline_monitor(deadline_monitor_tag)}; | ||
| ASSERT_TRUE(result.has_value()); | ||
| } | ||
|
|
||
| TEST(HealthMonitor, GetDeadlineMonitor_Taken) | ||
| { | ||
| MonitorTag deadline_monitor_tag{"deadline_monitor"}; | ||
| DeadlineMonitorBuilder deadline_monitor_builder; | ||
| auto health_monitor{HealthMonitorBuilder{} | ||
| .add_deadline_monitor(deadline_monitor_tag, std::move(deadline_monitor_builder)) | ||
| .build() | ||
| .value()}; | ||
|
|
||
| health_monitor.get_deadline_monitor(deadline_monitor_tag); | ||
| auto result{health_monitor.get_deadline_monitor(deadline_monitor_tag)}; | ||
| ASSERT_FALSE(result.has_value()); | ||
| } | ||
|
|
||
| TEST(HealthMonitor, GetDeadlineMonitor_Unknown) | ||
| { | ||
| MonitorTag deadline_monitor_tag{"deadline_monitor"}; | ||
| DeadlineMonitorBuilder deadline_monitor_builder; | ||
| auto health_monitor{HealthMonitorBuilder{} | ||
| .add_deadline_monitor(deadline_monitor_tag, std::move(deadline_monitor_builder)) | ||
| .build() | ||
| .value()}; | ||
|
|
||
| auto result{health_monitor.get_deadline_monitor(MonitorTag{"undefined_monitor"})}; | ||
| ASSERT_FALSE(result.has_value()); | ||
| } | ||
|
|
||
| TEST(HealthMonitor, GetHeartbeatMonitor_Available) | ||
| { | ||
| MonitorTag heartbeat_monitor_tag{"heartbeat_monitor"}; | ||
| auto heartbeat_monitor_builder{def_heartbeat_monitor_builder()}; | ||
| auto health_monitor{HealthMonitorBuilder{} | ||
| .add_heartbeat_monitor(heartbeat_monitor_tag, std::move(heartbeat_monitor_builder)) | ||
| .build() | ||
| .value()}; | ||
|
|
||
| auto result{health_monitor.get_heartbeat_monitor(heartbeat_monitor_tag)}; | ||
| ASSERT_TRUE(result.has_value()); | ||
| } | ||
|
|
||
| TEST(HealthMonitor, GetHeartbeatMonitor_Taken) | ||
| { | ||
| MonitorTag heartbeat_monitor_tag{"heartbeat_monitor"}; | ||
| HeartbeatMonitorBuilder heartbeat_monitor_builder{def_heartbeat_monitor_builder()}; | ||
| auto health_monitor{HealthMonitorBuilder{} | ||
| .add_heartbeat_monitor(heartbeat_monitor_tag, std::move(heartbeat_monitor_builder)) | ||
| .build() | ||
| .value()}; | ||
|
|
||
| health_monitor.get_heartbeat_monitor(heartbeat_monitor_tag); | ||
| auto result{health_monitor.get_heartbeat_monitor(heartbeat_monitor_tag)}; | ||
| ASSERT_FALSE(result.has_value()); | ||
| } | ||
|
|
||
| TEST(HealthMonitor, GetHeartbeatMonitor_Unknown) | ||
| { | ||
| MonitorTag heartbeat_monitor_tag{"heartbeat_monitor"}; | ||
| HeartbeatMonitorBuilder heartbeat_monitor_builder{def_heartbeat_monitor_builder()}; | ||
| auto health_monitor{HealthMonitorBuilder{} | ||
| .add_heartbeat_monitor(heartbeat_monitor_tag, std::move(heartbeat_monitor_builder)) | ||
| .build() | ||
| .value()}; | ||
|
|
||
| auto result{health_monitor.get_heartbeat_monitor(MonitorTag{"undefined_monitor"})}; | ||
| ASSERT_FALSE(result.has_value()); | ||
| } | ||
|
|
||
| TEST(HealthMonitor, GetLogicMonitor_Available) | ||
| { | ||
| MonitorTag logic_monitor_tag{"logic_monitor"}; | ||
| auto logic_monitor_builder{def_logic_monitor_builder()}; | ||
| auto health_monitor{ | ||
| HealthMonitorBuilder{}.add_logic_monitor(logic_monitor_tag, std::move(logic_monitor_builder)).build().value()}; | ||
|
|
||
| auto result{health_monitor.get_logic_monitor(logic_monitor_tag)}; | ||
| ASSERT_TRUE(result.has_value()); | ||
| } | ||
|
|
||
| TEST(HealthMonitor, GetLogicMonitor_Taken) | ||
| { | ||
| MonitorTag logic_monitor_tag{"logic_monitor"}; | ||
| LogicMonitorBuilder logic_monitor_builder{def_logic_monitor_builder()}; | ||
| auto health_monitor{ | ||
| HealthMonitorBuilder{}.add_logic_monitor(logic_monitor_tag, std::move(logic_monitor_builder)).build().value()}; | ||
|
|
||
| health_monitor.get_logic_monitor(logic_monitor_tag); | ||
| auto result{health_monitor.get_logic_monitor(logic_monitor_tag)}; | ||
| ASSERT_FALSE(result.has_value()); | ||
| } | ||
|
|
||
| TEST(HealthMonitor, GetLogicMonitor_Unknown) | ||
| { | ||
| MonitorTag logic_monitor_tag{"logic_monitor"}; | ||
| LogicMonitorBuilder logic_monitor_builder{def_logic_monitor_builder()}; | ||
| auto health_monitor{ | ||
| HealthMonitorBuilder{}.add_logic_monitor(logic_monitor_tag, std::move(logic_monitor_builder)).build().value()}; | ||
|
|
||
| auto result{health_monitor.get_logic_monitor(MonitorTag{"undefined_monitor"})}; | ||
| ASSERT_FALSE(result.has_value()); | ||
| } | ||
|
|
||
| TEST(HealthMonitor, Start_Succeeds) | ||
| { | ||
| MonitorTag deadline_monitor_tag{"deadline_monitor"}; | ||
| DeadlineMonitorBuilder deadline_monitor_builder; | ||
| MonitorTag heartbeat_monitor_tag{"heartbeat_monitor"}; | ||
| auto heartbeat_monitor_builder{def_heartbeat_monitor_builder()}; | ||
| MonitorTag logic_monitor_tag{"logic_monitor"}; | ||
| auto logic_monitor_builder{def_logic_monitor_builder()}; | ||
|
|
||
| auto health_monitor{HealthMonitorBuilder{} | ||
| .add_deadline_monitor(deadline_monitor_tag, std::move(deadline_monitor_builder)) | ||
| .add_heartbeat_monitor(heartbeat_monitor_tag, std::move(heartbeat_monitor_builder)) | ||
| .add_logic_monitor(logic_monitor_tag, std::move(logic_monitor_builder)) | ||
| .build() | ||
| .value()}; | ||
|
|
||
| health_monitor.get_deadline_monitor(deadline_monitor_tag); | ||
| health_monitor.get_heartbeat_monitor(heartbeat_monitor_tag); | ||
| health_monitor.get_logic_monitor(logic_monitor_tag); | ||
|
|
||
| health_monitor.start(); | ||
| } | ||
|
|
||
| TEST(HealthMonitor, Start_MonitorsNotTaken) | ||
| { | ||
| MonitorTag deadline_monitor_tag{"deadline_monitor"}; | ||
| DeadlineMonitorBuilder deadline_monitor_builder; | ||
| MonitorTag heartbeat_monitor_tag{"heartbeat_monitor"}; | ||
| auto heartbeat_monitor_builder{def_heartbeat_monitor_builder()}; | ||
| MonitorTag logic_monitor_tag{"logic_monitor"}; | ||
| auto logic_monitor_builder{def_logic_monitor_builder()}; | ||
|
|
||
| auto health_monitor{HealthMonitorBuilder{} | ||
| .add_deadline_monitor(deadline_monitor_tag, std::move(deadline_monitor_builder)) | ||
| .add_heartbeat_monitor(heartbeat_monitor_tag, std::move(heartbeat_monitor_builder)) | ||
| .add_logic_monitor(logic_monitor_tag, std::move(logic_monitor_builder)) | ||
| .build() | ||
| .value()}; | ||
|
|
||
| // `SIGABRT` is expected. | ||
| ASSERT_DEATH({ health_monitor.start(); }, ""); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the RecordProperty calls defined in Score Process to define TestType, DerivationTechnique, the Description and possible requirements links.