-
Notifications
You must be signed in to change notification settings - Fork 35
test(fit): add score_time clock integration test #300
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
clanghans
wants to merge
1
commit into
eclipse-score:main
Choose a base branch
from
etas-contrib:feature/score-time-integration-test
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
100 changes: 100 additions & 0 deletions
100
feature_integration_tests/test_cases/tests/time/test_clock_now.py
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,100 @@ | ||
| # ******************************************************************************* | ||
| # 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 | ||
| # ******************************************************************************* | ||
|
|
||
| from typing import Any | ||
|
|
||
| import pytest | ||
| from fit_scenario import FitScenario, ResultCode | ||
| from testing_utils import BazelTools, BuildTools, LogContainer, ScenarioResult | ||
|
|
||
| # score_time is C++ only; there is no Rust variant of the clock library. | ||
| pytestmark = pytest.mark.parametrize("version", ["cpp"], scope="class") | ||
|
|
||
|
|
||
| class ClockScenario(FitScenario): | ||
| """Common base for score_time clock scenarios (no scenario input required).""" | ||
|
|
||
| @pytest.fixture(scope="class") | ||
| def build_tools(self, version: str) -> BuildTools: | ||
| # Consume the parametrized `version` and select the C++ scenario binary. | ||
| return BazelTools(option_prefix=version) | ||
|
|
||
| @pytest.fixture(scope="class") | ||
| def test_config(self) -> dict[str, Any]: | ||
| return {} | ||
|
|
||
|
|
||
| class TestSystemClockNow(ClockScenario): | ||
| """ | ||
| Verify SystemClock::Now() returns a live reading through the public Clock API. | ||
|
|
||
| The C++ scenario compares the reading against the host system clock within | ||
| tolerance and fails the process otherwise. Python confirms the process | ||
| succeeded and that the reading was emitted. | ||
| """ | ||
|
|
||
| @pytest.fixture(scope="class") | ||
| def scenario_name(self) -> str: | ||
| return "time.system_clock_now" | ||
|
|
||
| def test_returns_success(self, results: ScenarioResult) -> None: | ||
| assert results.return_code == ResultCode.SUCCESS | ||
|
|
||
| def test_reading_logged(self, logs_info_level: LogContainer) -> None: | ||
| log = logs_info_level.find_log("clock", value="system") | ||
| assert log is not None | ||
| assert log.value_ns > 0 | ||
|
|
||
|
|
||
| class TestSteadyClockNow(ClockScenario): | ||
| """ | ||
| Verify SteadyClock::Now() is monotonic across two consecutive readings. | ||
|
|
||
| The C++ scenario fails the process if the second reading precedes the first. | ||
| Python confirms success and that the logged readings are non-decreasing. | ||
| """ | ||
|
|
||
| @pytest.fixture(scope="class") | ||
| def scenario_name(self) -> str: | ||
| return "time.steady_clock_now" | ||
|
|
||
| def test_returns_success(self, results: ScenarioResult) -> None: | ||
| assert results.return_code == ResultCode.SUCCESS | ||
|
|
||
| def test_readings_monotonic(self, logs_info_level: LogContainer) -> None: | ||
| log = logs_info_level.find_log("clock", value="steady") | ||
| assert log is not None | ||
| assert log.second_ns >= log.first_ns | ||
|
|
||
|
|
||
| class TestHighResSteadyClockNow(ClockScenario): | ||
| """ | ||
| Verify HighResSteadyClock::Now() is live and monotonic. | ||
|
|
||
| The C++ scenario fails the process on a zero or non-monotonic reading. | ||
| Python confirms success and that the logged readings are non-zero and | ||
| non-decreasing. | ||
| """ | ||
|
|
||
| @pytest.fixture(scope="class") | ||
| def scenario_name(self) -> str: | ||
| return "time.high_res_steady_clock_now" | ||
|
|
||
| def test_returns_success(self, results: ScenarioResult) -> None: | ||
| assert results.return_code == ResultCode.SUCCESS | ||
|
|
||
| def test_readings_monotonic(self, logs_info_level: LogContainer) -> None: | ||
| log = logs_info_level.find_log("clock", value="high_res_steady") | ||
| assert log is not None | ||
| assert log.first_ns > 0 | ||
| assert log.second_ns >= log.first_ns | ||
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
63 changes: 63 additions & 0 deletions
63
feature_integration_tests/test_scenarios/cpp/src/internals/time/clock_log.h
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,63 @@ | ||
| /******************************************************************************** | ||
| * 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 | ||
| ********************************************************************************/ | ||
|
|
||
| #ifndef INTERNALS_TIME_CLOCK_LOG_H_ | ||
| #define INTERNALS_TIME_CLOCK_LOG_H_ | ||
|
|
||
| #include <chrono> | ||
| #include <cstdint> | ||
| #include <iostream> | ||
| #include <string> | ||
|
|
||
| namespace time_log { | ||
|
|
||
| /** | ||
| * @brief Return the current UNIX timestamp in microseconds. | ||
| * | ||
| * The FIT LogContainer parses the "timestamp" field as microseconds, so the | ||
| * C++ scenarios emit microseconds to keep log ordering consistent with the | ||
| * Rust tracing JSON shape. | ||
| * | ||
| * @return Microseconds since the UNIX epoch. | ||
| */ | ||
| inline std::int64_t unix_micros() { | ||
| const auto now = std::chrono::system_clock::now(); | ||
| return std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Emit a structured JSON INFO log line to stdout. | ||
| * | ||
| * Matches the Rust tracing JSON format expected by the FIT LogContainer so | ||
| * that Python test assertions can use find_log() uniformly for both Rust and | ||
| * C++ scenarios. | ||
| * | ||
| * Example output: | ||
| * @code | ||
| * {"timestamp":"1700000000000000","level":"INFO","fields":{"clock":"system","value_ns":1700000000000000000}, | ||
| * "target":"cpp_test_scenarios::scenarios::time::system_clock_now","threadId":"ThreadId(1)"} | ||
| * @endcode | ||
| * | ||
| * @param fields JSON fragment for the "fields" object, e.g. @c "\"clock\":\"system\",\"value_ns\":1" | ||
| * @param target Module target string embedded in the log line. | ||
| */ | ||
| inline void log_info(const std::string& fields, const std::string& target) { | ||
| std::cout << "{\"timestamp\":\"" << unix_micros() | ||
| << "\",\"level\":\"INFO\",\"fields\":{" << fields | ||
| << "},\"target\":\"" << target | ||
| << "\",\"threadId\":\"ThreadId(1)\"}\n"; | ||
| } | ||
|
|
||
| } // namespace time_log | ||
|
|
||
| #endif // INTERNALS_TIME_CLOCK_LOG_H_ |
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
79 changes: 79 additions & 0 deletions
79
...ure_integration_tests/test_scenarios/cpp/src/scenarios/time/high_res_steady_clock_now.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,79 @@ | ||
| /******************************************************************************** | ||
| * 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 "../../internals/time/clock_log.h" | ||
|
|
||
| #include "score/time/high_res_steady_time/src/high_res_steady_clock.h" | ||
|
|
||
| #include <scenario.hpp> | ||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <stdexcept> | ||
| #include <string> | ||
|
|
||
| namespace { | ||
|
|
||
| /// Read the HighResSteadyClock twice via the public Clock API and verify it is | ||
| /// live and monotonic. | ||
| /// | ||
| /// The high-resolution steady clock must return a non-zero reading and never | ||
| /// move backwards. This proves the high-resolution backend (system-clock based | ||
| /// on Linux) links and behaves monotonically in the reference integration build. | ||
| class HighResSteadyClockNow final : public Scenario { | ||
| public: | ||
| /** | ||
| * @brief Return the scenario name used to identify this scenario in the runner. | ||
| * @return Scenario name string. | ||
| */ | ||
| std::string name() const final { return "high_res_steady_clock_now"; } | ||
|
|
||
| /** | ||
| * @brief Execute the high-resolution steady-clock scenario. | ||
| * | ||
| * Takes two consecutive snapshots and verifies the first is non-zero and the | ||
| * second is not earlier than the first, then logs both readings for Python | ||
| * inspection. | ||
| * | ||
| * @param input Unused; this scenario takes no configuration. | ||
| * @throws std::runtime_error if the reading is zero or non-monotonic. | ||
| */ | ||
| void run(const std::string& /*input*/) const final { | ||
| const auto clock = score::time::HighResSteadyClock::GetInstance(); | ||
| const std::int64_t first_ns = clock.Now().TimePointNs().count(); | ||
| const std::int64_t second_ns = clock.Now().TimePointNs().count(); | ||
|
|
||
| if (first_ns == 0) { | ||
| throw std::runtime_error("HighResSteadyClock::Now() returned a zero reading"); | ||
| } | ||
| if (second_ns < first_ns) { | ||
| throw std::runtime_error( | ||
| "HighResSteadyClock::Now() is not monotonic: second reading precedes the first"); | ||
| } | ||
|
|
||
| time_log::log_info( | ||
| "\"clock\":\"high_res_steady\",\"first_ns\":" + std::to_string(first_ns) + | ||
| ",\"second_ns\":" + std::to_string(second_ns), | ||
| "cpp_test_scenarios::scenarios::time::high_res_steady_clock_now"); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| /** | ||
| * @brief Factory function for the HighResSteadyClockNow scenario. | ||
| * @return Shared pointer to the constructed scenario. | ||
| */ | ||
| Scenario::Ptr make_high_res_steady_clock_now_scenario() { | ||
| return std::make_shared<HighResSteadyClockNow>(); | ||
| } |
31 changes: 31 additions & 0 deletions
31
feature_integration_tests/test_scenarios/cpp/src/scenarios/time/mod.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,31 @@ | ||
| /******************************************************************************** | ||
| * 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 <scenario.hpp> | ||
|
|
||
| #include <vector> | ||
|
|
||
| Scenario::Ptr make_system_clock_now_scenario(); | ||
| Scenario::Ptr make_steady_clock_now_scenario(); | ||
| Scenario::Ptr make_high_res_steady_clock_now_scenario(); | ||
|
|
||
| ScenarioGroup::Ptr time_scenario_group() { | ||
| return std::make_shared<ScenarioGroupImpl>( | ||
| "time", | ||
| std::vector<Scenario::Ptr>{ | ||
| make_system_clock_now_scenario(), | ||
| make_steady_clock_now_scenario(), | ||
| make_high_res_steady_clock_now_scenario(), | ||
| }, | ||
| std::vector<ScenarioGroup::Ptr>{}); | ||
| } |
75 changes: 75 additions & 0 deletions
75
feature_integration_tests/test_scenarios/cpp/src/scenarios/time/steady_clock_now.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,75 @@ | ||
| /******************************************************************************** | ||
| * 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 "../../internals/time/clock_log.h" | ||
|
|
||
| #include "score/time/steady_time/src/steady_clock.h" | ||
|
|
||
| #include <scenario.hpp> | ||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <stdexcept> | ||
| #include <string> | ||
|
|
||
| namespace { | ||
|
|
||
| /// Read the SteadyClock twice via the public Clock API and verify monotonicity. | ||
| /// | ||
| /// A steady (monotonic) clock must never move backwards. Two consecutive | ||
| /// Clock<steady_clock>::Now() readings must be non-decreasing. This proves the | ||
| /// steady clock backend links and behaves monotonically in the reference | ||
| /// integration build. | ||
| class SteadyClockNow final : public Scenario { | ||
| public: | ||
| /** | ||
| * @brief Return the scenario name used to identify this scenario in the runner. | ||
| * @return Scenario name string. | ||
| */ | ||
| std::string name() const final { return "steady_clock_now"; } | ||
|
|
||
| /** | ||
| * @brief Execute the steady-clock monotonicity scenario. | ||
| * | ||
| * Takes two consecutive snapshots and verifies the second is not earlier | ||
| * than the first, then logs both readings for Python inspection. | ||
| * | ||
| * @param input Unused; this scenario takes no configuration. | ||
| * @throws std::runtime_error if the second reading precedes the first. | ||
| */ | ||
| void run(const std::string& /*input*/) const final { | ||
| const auto clock = score::time::SteadyClock::GetInstance(); | ||
| const std::int64_t first_ns = clock.Now().TimePointNs().count(); | ||
| const std::int64_t second_ns = clock.Now().TimePointNs().count(); | ||
|
|
||
| if (second_ns < first_ns) { | ||
| throw std::runtime_error( | ||
| "SteadyClock::Now() is not monotonic: second reading precedes the first"); | ||
| } | ||
|
|
||
| time_log::log_info( | ||
| "\"clock\":\"steady\",\"first_ns\":" + std::to_string(first_ns) + | ||
| ",\"second_ns\":" + std::to_string(second_ns), | ||
| "cpp_test_scenarios::scenarios::time::steady_clock_now"); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| /** | ||
| * @brief Factory function for the SteadyClockNow scenario. | ||
| * @return Shared pointer to the constructed scenario. | ||
| */ | ||
| Scenario::Ptr make_steady_clock_now_scenario() { | ||
| return std::make_shared<SteadyClockNow>(); | ||
| } |
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.
redundant? should be possible to use FitScenario directly