Skip to content

fboss2: add unit tests for CmdUtilsCommon helpers#1295

Open
atulya-singh wants to merge 1 commit into
facebook:mainfrom
atulya-singh:add-cmdutilscommon-tests
Open

fboss2: add unit tests for CmdUtilsCommon helpers#1295
atulya-singh wants to merge 1 commit into
facebook:mainfrom
atulya-singh:add-cmdutilscommon-tests

Conversation

@atulya-singh

Copy link
Copy Markdown

Pre-submission checklist

  • I've ran the linters locally and fixed lint errors related to the files I modified in this PR. You can install the linters by running pip install -r requirements-dev.txt && pre-commit install
  • pre-commit run

Summary

fboss/cli/fboss2/utils/CmdUtilsCommon.cpp had no unit-test coverage. This PR adds CmdUtilsCommonTest.cpp covering the pure/deterministic helpers in that file:

  • formatBandwidth — the N/A boundary (< 1 byte/s), unit scaling across bps/Kbps/Mbps/Gbps, and its deliberate truncate-rather-than-round behavior.
  • splitFractionalSecondsFromTimer — the second/millisecond split.
  • getEpochFromDuration — zero duration returns ~now, and a duration is subtracted in milliseconds.
  • parseTimeToTimeStamp — structural validation of the YYYY-MM-DD HH:MM:SS.fff format (timezone-independent, using a fraction ≥ 100 ms so the assertion is stable).

The new test is wired into both build systems: the BUCK framework_test target (fboss/cli/fboss2/test/BUCK) and the CMake fboss2_framework_test target (cmake/CliFboss2Test.cmake).

Test Plan

  • pre-commit run --files fboss/cli/fboss2/test/CmdUtilsCommonTest.cpp fboss/cli/fboss2/test/BUCK cmake/CliFboss2Test.cmake -> clang-format ... Passed, all hooks pass.
  • New test cases are deterministic and assert behavior that holds on main:
    • formatBandwidth(0.5f) == "N/A", formatBandwidth(125000000.0f) == "1Gbps", etc.
    • splitFractionalSecondsFromTimer(6475) == {6, 475}.
    • getEpochFromDuration(10000) is ~10 s before getEpochFromDuration(0).
    • parseTimeToTimeStamp(1635292806475) matches ^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.475.

Note: assertions intentionally avoid the sub-100 ms millisecond zero-padding case so this PR stays green on main independently; that case is covered by the related zero-padding fix in #1292.

CmdUtilsCommon.cpp had no unit-test coverage. Add CmdUtilsCommonTest.cpp
covering the pure/deterministic helpers:

- formatBandwidth: N/A boundary, unit scaling (bps/Kbps/Mbps/Gbps), and
  its deliberate truncate-don't-round behavior.
- splitFractionalSecondsFromTimer: second/millisecond split.
- getEpochFromDuration: zero duration and millisecond subtraction.
- parseTimeToTimeStamp: structural date-time + fractional format.

Wire the new test into both the BUCK framework_test target and the CMake
fboss2_framework_test target.
@atulya-singh atulya-singh requested review from a team as code owners June 13, 2026 19:39
@meta-cla meta-cla Bot added the CLA Signed label Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant