Skip to content

Speed up execution of thermalctld unit tests#789

Merged
rlhui merged 2 commits intosonic-net:masterfrom
nexthop-ai:aditya.thermalctldunittests
Apr 22, 2026
Merged

Speed up execution of thermalctld unit tests#789
rlhui merged 2 commits intosonic-net:masterfrom
nexthop-ai:aditya.thermalctldunittests

Conversation

@aditya-nexthop
Copy link
Copy Markdown
Contributor

@aditya-nexthop aditya-nexthop commented Apr 7, 2026

Description

Replace the real swsssdk imports in mocked_libs/swsscommon/swsscommon.py with mock stubs for ConfigDBConnector and SonicV2Connector, eliminating all swsssdk dependencies from the test environment, and add the missing getKeys() method to mock_swsscommon.Table.

Motivation and Context

The thermalctld test suite was taking ~36 minutes to run due to Redis connection timeouts incurred on every TemperatureUpdater construction.

TemperatureUpdater.__init__ calls _init_sfp_util_helper(), which invokes SfpUtilHelper().read_porttab_mappings(). This triggers up to 7 calls to device_info.get_localhost_info() per construction through three call chains: get_platform_and_hwsku(), get_port_config(), and get_path_to_port_config_file(). Each call creates a ConfigDBConnector (from swsscommon.swsscommon) that retries connecting to Redis ~3 times with ~7s sleep per retry.

The root cause is that mocked_libs/swsscommon/swsscommon.py was re-exporting the real ConfigDBConnector and SonicV2Connector from swsssdk instead of providing mock stubs. Since device_info.py imports these via from swsscommon.swsscommon import ConfigDBConnector, SonicV2Connector, and the mocked package is first in sys.path during tests, replacing them with no-op stubs is sufficient to prevent all Redis connection attempts with no other test changes required.

mock_swsscommon.Table was also missing getKeys(), which TemperatureUpdater.__del__ calls during cleanup, causing an AttributeError warning on every test that constructs a TemperatureUpdater.

How Has This Been Tested?

Profiled test_dpu_chassis_thermals (the worst offender) using cProfile:

28880 function calls in 49.403 seconds

device_info.get_localhost_info   7 calls × 7.057s = 49.4s total
  └─ swsssdk ConfigDBConnector.connect
     └─ redis retry: 21 × time.sleep(2.35s) = 49.4s

After fix:

  • test_dpu_chassis_thermals: ~230s → 0.07s
  • Full suite: ~36 minutes → ~1 minute

Additional Information (Optional)

The mock stubs follow the same pattern used in sonic-chassisd/tests/mock_swsscommon.py.

SonicV2Connector is stubbed alongside ConfigDBConnector because device_info.py imports both in a single statement (from swsscommon.swsscommon import ConfigDBConnector, SonicV2Connector). swsssdk conditionally bypasses its own deprecation ImportError when mock or unittest are already in sys.modules — so during pytest the real SonicV2Connector would be importable. Stubbing it explicitly removes the dependency on this behavior and ensures swsssdk is never imported in the test environment.

fixes #788

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).

@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

… swsssdk and remove the direct mocks in the test file

Signed-off-by: aditya-nexthop <aditya@nexthop.ai>
@aditya-nexthop aditya-nexthop force-pushed the aditya.thermalctldunittests branch from 0d36026 to 15c7f23 Compare April 7, 2026 17:40
@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 April 7, 2026 17:58
@aditya-nexthop
Copy link
Copy Markdown
Contributor Author

@judyjoseph please can you take a look at this PR?

Copy link
Copy Markdown
Contributor

@vvolam vvolam left a comment

Choose a reason for hiding this comment

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

LGTM, Thanks!

@rlhui rlhui merged commit 8fc1627 into sonic-net:master Apr 22, 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.

thermalctld unit tests take 35 minutes to execute

5 participants