Skip to content

Speed up sonic_xcvr unit tests by patching time.sleep via autouse fixture#669

Merged
prgeor merged 2 commits into
sonic-net:masterfrom
nexthop-ai:aditya.speedup-xcvr-tests
May 21, 2026
Merged

Speed up sonic_xcvr unit tests by patching time.sleep via autouse fixture#669
prgeor merged 2 commits into
sonic-net:masterfrom
nexthop-ai:aditya.speedup-xcvr-tests

Conversation

@aditya-nexthop
Copy link
Copy Markdown
Contributor

Description

Add an autouse pytest fixture in tests/sonic_xcvr/conftest.py that
patches time.sleep for every test in the tests/sonic_xcvr package.

@pytest.fixture(autouse=True)
def _no_sleep():
    with patch("time.sleep"):
        yield

Motivation and Context

Several tests/sonic_xcvr tests exercise production code paths that
call time.sleep directly, without mocking it. The suite therefore
spends almost all of its wall time blocked in real sleeps:

Test Count Per-run Total Source of sleep
test_cmisCDB.py::test_start_fw_download 3 5.00s 15.0s cmisCDB.py:288time.sleep(MAX_CDB_CMD_FOREGROUND_PROCESSING_TIME_tCDBF) (5s)
test_cmisCDB.py::test_validate_fw_image 3 5.00s 15.0s cmisCDB.py:419 — same 5s constant
test_cmis.py::test_reset 1 2.00s 2.0s cmis.py:1369time.sleep(2) after reset_module()
test_cmisCDB.py::test_run_fw_image 3 0.56s 1.7s cmisCDB.py:459time.sleep(delay/1000) (delay ≈ 562 ms)
test_ccmis.py::test_set_tx_power 2 1.00s 2.0s c_cmis.py:187time.sleep(1)

These 12 test invocations account for ~35.7s of a ~36.7s suite — about
97% of the runtime.

How Has This Been Tested?

Ran the full tests/sonic_xcvr suite before and after the change.

# Before
============================= 824 passed in 36.71s =============================

# After
============================= 824 passed in 1.07s ==============================

All 824 tests still pass; no behavior change. Suite is ~34× faster.

Additional Information (Optional)

Signed-off-by: aditya-nexthop <aditya@nexthop.ai>
@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@aditya-nexthop aditya-nexthop marked this pull request as ready for review May 20, 2026 16:50
@prgeor prgeor requested a review from Copilot May 20, 2026 23:13
import pytest


@pytest.fixture(autouse=True)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@aditya-nexthop you mean test case running sleep is waste of time?

Copy link
Copy Markdown
Contributor Author

@aditya-nexthop aditya-nexthop May 20, 2026

Choose a reason for hiding this comment

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

yes, if it does nothing useful, especially in a unit test with no real hardware behind it?

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR speeds up the tests/sonic_xcvr pytest suite by introducing an autouse fixture that patches time.sleep during each test run, eliminating real sleep delays from exercised production paths.

Changes:

  • Add tests/sonic_xcvr/conftest.py with an autouse fixture that patches time.sleep for all tests under tests/sonic_xcvr.

Comment thread tests/sonic_xcvr/conftest.py Outdated
Signed-off-by: aditya-nexthop <aditya@nexthop.ai>
@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@prgeor prgeor merged commit deffc3f into sonic-net:master May 21, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants