Skip to content

Deduplicate AMS2 test helpers#537

Open
EffortlessSteven wants to merge 1 commit into
mainfrom
codex/refactor-code-to-be-more-dry-gryukp
Open

Deduplicate AMS2 test helpers#537
EffortlessSteven wants to merge 1 commit into
mainfrom
codex/refactor-code-to-be-more-dry-gryukp

Conversation

@EffortlessSteven

Copy link
Copy Markdown
Member

Motivation

  • Reduce duplicated test helper code across the AMS2 test suite and make tests more DRY by centralizing common helpers used for adapter construction, shared-memory serialization, defaults, and fixed-width string writes.
  • Make future changes to AMS2 test helpers simpler and less error-prone by providing a single source of truth for shared utilities.

Description

  • Added a new shared test helper module at crates/telemetry-ams2/tests/common/mod.rs providing adapter(), shared_memory_to_bytes(), default_shared_memory(), and write_fixed_str() helper functions.
  • Updated crates/telemetry-ams2/tests/adapter_tests.rs, deep_tests.rs, ams2_deep_tests.rs, and snapshot_tests.rs to mod common; and import the shared helpers instead of defining local copies.
  • Replaced ad-hoc string field writes and duplicate serialization/default helpers in tests with write_fixed_str() and shared_memory_to_bytes() aliases where appropriate.
  • Ran workspace formatting to keep code style consistent via python scripts/cargo_fmt_workspace.py.

Testing

  • Ran python scripts/cargo_fmt_workspace.py which completed successfully across the workspace.
  • Ran git diff --check which reported no spacing/whitespace errors.
  • Attempted cargo test -p racing-wheel-telemetry-ams2 --tests, which reached compilation but failed during a native dependency build because the environment is missing the system libudev required by hidapi (build blocked by system dependency), so test execution could not fully complete.

Codex Task

@gemini-code-assist

Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@coderabbitai

coderabbitai Bot commented May 17, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@EffortlessSteven has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 57 minutes and 28 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 13d989ff-98f5-4bdb-b1a0-147663f8eac0

📥 Commits

Reviewing files that changed from the base of the PR and between f5a48ad and 23368b4.

📒 Files selected for processing (5)
  • crates/telemetry-ams2/tests/adapter_tests.rs
  • crates/telemetry-ams2/tests/ams2_deep_tests.rs
  • crates/telemetry-ams2/tests/common/mod.rs
  • crates/telemetry-ams2/tests/deep_tests.rs
  • crates/telemetry-ams2/tests/snapshot_tests.rs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/refactor-code-to-be-more-dry-gryukp

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@factory-droid

factory-droid Bot commented May 17, 2026

Copy link
Copy Markdown

Droid finished @EffortlessSteven's task —— View job


Refactor looks straightforward and keeps behavior equivalent while reducing duplication. The main concern is the test helper’s unsafe struct-to-bytes conversion potentially reading uninitialized padding; tightening the helper to guarantee byte-initialization would make the tests more robust.

@github-actions

Copy link
Copy Markdown

Compatibility Layer Usage Report

Current usage count: 193
Baseline usage count: 193
ℹ️ Usage unchanged

📈 Usage Trend (Last 30 Days)

  • Peak usage: 193
  • Current trend: insufficient_data (0%)
  • Projected removal: unknown

Usage Details

  • /home/runner/work/OpenRacing/OpenRacing/crates/engine/src/compat_impl.rs:69 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/engine/src/hid/linux.rs:1253 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/engine/src/hid/linux.rs:1282 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/engine/src/hid/mod.rs:230 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/engine/src/protocol.rs:290 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/engine/src/protocol.rs:742 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/engine/src/protocol.rs:749 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/schemas/src/service_example.rs:189 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/schemas/src/service_example.rs:218 - temp_c
  • /home/runner/work/OpenRacing/OpenRacing/crates/schemas/src/integration_test.rs:79 - temp_c

... and 183 more occurrences

Migration Guide

To migrate these usages, replace:

  • .temp_c().temperature_c
  • .faults().fault_flags
  • .wheel_angle_mdeg().wheel_angle_deg
  • .wheel_speed_mrad_s().wheel_speed_rad_s

See Migration Patterns for detailed examples.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 23368b470e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

//! AMS2 uses Windows shared memory, so most behavioral tests are adapter-level
//! (game_id, update_rate, etc.). Shared memory tests require a running AMS2 instance.

mod common;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid dead_code failures from shared test helpers

When this integration test declares mod common, Cargo compiles every item in tests/common/mod.rs into the adapter_tests test crate, but this file only uses default_shared_memory and shared_memory_to_bytes; adapter() and write_fixed_str() are therefore dead code in this crate. The repository’s required cargo clippy --all-targets --all-features -- -D warnings promotes those dead_code warnings to errors, so the all-targets lint job fails before tests run. Consider splitting the helpers by use site or allowing dead code on the shared test-helper module.

Useful? React with 👍 / 👎.

Comment on lines +8 to +19
/// Serialize an `AMS2SharedMemory` to its raw byte representation for the normalize API.
pub fn shared_memory_to_bytes(data: &AMS2SharedMemory) -> Vec<u8> {
let size = std::mem::size_of::<AMS2SharedMemory>();
let ptr = data as *const AMS2SharedMemory as *const u8;
// SAFETY: AMS2SharedMemory is repr(C) and fully initialized via Default in tests.
unsafe { std::slice::from_raw_parts(ptr, size) }.to_vec()
}

/// Create a default `AMS2SharedMemory` without repeating private-field workarounds.
pub fn default_shared_memory() -> AMS2SharedMemory {
AMS2SharedMemory::default()
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Avoid UB when converting AMS2SharedMemory to bytes

shared_memory_to_bytes reinterprets the entire #[repr(C)] struct as &[u8] and copies it; this can read uninitialized padding bytes even when the fields were set via Default/struct update syntax, which is UB and can cause flakiness under Miri/sanitizers. In tests, prefer constructing shared-memory instances from an all-zero base (so padding is initialized) and update the safety comment to reflect that requirement.

Suggested change
/// Serialize an `AMS2SharedMemory` to its raw byte representation for the normalize API.
pub fn shared_memory_to_bytes(data: &AMS2SharedMemory) -> Vec<u8> {
let size = std::mem::size_of::<AMS2SharedMemory>();
let ptr = data as *const AMS2SharedMemory as *const u8;
// SAFETY: AMS2SharedMemory is repr(C) and fully initialized via Default in tests.
unsafe { std::slice::from_raw_parts(ptr, size) }.to_vec()
}
/// Create a default `AMS2SharedMemory` without repeating private-field workarounds.
pub fn default_shared_memory() -> AMS2SharedMemory {
AMS2SharedMemory::default()
}
/// Serialize an `AMS2SharedMemory` to its raw byte representation for the normalize API.
pub fn shared_memory_to_bytes(data: &AMS2SharedMemory) -> Vec<u8> {
let size = std::mem::size_of::<AMS2SharedMemory>();
let ptr = data as *const AMS2SharedMemory as *const u8;
// SAFETY: `data` must be fully byte-initialized (including any internal padding).
unsafe { std::slice::from_raw_parts(ptr, size) }.to_vec()
}
/// Create a default `AMS2SharedMemory` without repeating private-field workarounds.
pub fn default_shared_memory() -> AMS2SharedMemory {
// SAFETY: AMS2SharedMemory is a plain-old-data struct (numeric scalars + fixed-size arrays),
// so the all-zero bit pattern is valid and also ensures any internal padding bytes are initialized.
unsafe { std::mem::zeroed() }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant