|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2026 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Apache License Version 2.0 which is available at |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: Apache-2.0 |
| 12 | + ********************************************************************************/ |
| 13 | + |
| 14 | +#include "score/hm/deadline/deadline_monitor.h" |
| 15 | +#include "score/hm/health_monitor.h" |
| 16 | +#include <gtest/gtest.h> |
| 17 | + |
| 18 | +using namespace score::hm; |
| 19 | +using namespace score::hm::deadline; |
| 20 | + |
| 21 | +TEST(DeadlineMonitorBuilder, New_Succeeds) |
| 22 | +{ |
| 23 | + DeadlineMonitorBuilder deadline_monitor_builder; |
| 24 | +} |
| 25 | + |
| 26 | +TEST(DeadlineMonitorBuilder, AddDeadline_Succeeds) |
| 27 | +{ |
| 28 | + using namespace std::chrono_literals; |
| 29 | + DeadlineTag deadline_tag{"deadline"}; |
| 30 | + TimeRange range{50ms, 150ms}; |
| 31 | + auto deadline_monitor_builder{DeadlineMonitorBuilder{}.add_deadline(deadline_tag, range)}; |
| 32 | +} |
| 33 | + |
| 34 | +class DeadlineMonitorFixture : public ::testing::Test |
| 35 | +{ |
| 36 | + protected: |
| 37 | + std::optional<DeadlineMonitor> deadline_monitor_; |
| 38 | + |
| 39 | + void SetUp() override |
| 40 | + { |
| 41 | + // Monitor must be obtained from HMON. |
| 42 | + // Initialize deadline monitor builder. |
| 43 | + using namespace std::chrono_literals; |
| 44 | + MonitorTag deadline_monitor_tag{"deadline_monitor"}; |
| 45 | + DeadlineTag deadline_tag{"deadline"}; |
| 46 | + TimeRange range{50ms, 150ms}; |
| 47 | + auto deadline_monitor_builder{DeadlineMonitorBuilder{}.add_deadline(deadline_tag, range)}; |
| 48 | + |
| 49 | + // Build HMON, including deadline monitor. |
| 50 | + auto hmon_build_result{HealthMonitorBuilder{} |
| 51 | + .add_deadline_monitor(deadline_monitor_tag, std::move(deadline_monitor_builder)) |
| 52 | + .build()}; |
| 53 | + ASSERT_TRUE(hmon_build_result.has_value()); |
| 54 | + auto hmon{std::move(hmon_build_result.value())}; |
| 55 | + |
| 56 | + // Get deadline monitor. |
| 57 | + auto get_deadline_monitor_result{hmon.get_deadline_monitor(deadline_monitor_tag)}; |
| 58 | + ASSERT_TRUE(get_deadline_monitor_result.has_value()); |
| 59 | + deadline_monitor_ = std::move(get_deadline_monitor_result.value()); |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +TEST_F(DeadlineMonitorFixture, GetDeadline_Succeeds) |
| 64 | +{ |
| 65 | + // Get deadline. |
| 66 | + auto get_deadline_result{deadline_monitor_->get_deadline(DeadlineTag{"deadline"})}; |
| 67 | + ASSERT_TRUE(get_deadline_result.has_value()); |
| 68 | +} |
| 69 | + |
| 70 | +TEST_F(DeadlineMonitorFixture, GetDeadline_Unknown) |
| 71 | +{ |
| 72 | + // Get deadline. |
| 73 | + auto get_deadline_result{deadline_monitor_->get_deadline(DeadlineTag{"unknown"})}; |
| 74 | + ASSERT_FALSE(get_deadline_result.has_value()); |
| 75 | + ASSERT_EQ(get_deadline_result.error(), Error::NotFound); |
| 76 | +} |
| 77 | + |
| 78 | +class DeadlineFixture : public DeadlineMonitorFixture |
| 79 | +{ |
| 80 | +}; |
| 81 | + |
| 82 | +TEST_F(DeadlineFixture, Start_Succeeds) |
| 83 | +{ |
| 84 | + // Get deadline. |
| 85 | + DeadlineTag deadline_tag{"deadline"}; |
| 86 | + auto get_deadline_result{deadline_monitor_->get_deadline(deadline_tag)}; |
| 87 | + ASSERT_TRUE(get_deadline_result.has_value()); |
| 88 | + auto deadline{std::move(get_deadline_result.value())}; |
| 89 | + |
| 90 | + // Try to start and stop deadline. |
| 91 | + auto deadline_start_result{deadline.start()}; |
| 92 | + ASSERT_TRUE(deadline_start_result.has_value()); |
| 93 | + auto deadline_handle{std::move(deadline_start_result.value())}; |
| 94 | + |
| 95 | + deadline_handle.stop(); |
| 96 | +} |
| 97 | + |
| 98 | +TEST_F(DeadlineFixture, Start_AlreadyRunning) |
| 99 | +{ |
| 100 | + // Get deadline. |
| 101 | + DeadlineTag deadline_tag{"deadline"}; |
| 102 | + auto get_deadline_result{deadline_monitor_->get_deadline(deadline_tag)}; |
| 103 | + ASSERT_TRUE(get_deadline_result.has_value()); |
| 104 | + auto deadline{std::move(get_deadline_result.value())}; |
| 105 | + |
| 106 | + // Try to start the deadline twice. |
| 107 | + deadline.start(); |
| 108 | + auto deadline_start_result{deadline.start()}; |
| 109 | + ASSERT_FALSE(deadline_start_result.has_value()); |
| 110 | + ASSERT_EQ(deadline_start_result.error(), Error::Failed); |
| 111 | +} |
0 commit comments