Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions sonic-xcvrd/tests/test_xcvrd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3925,6 +3925,28 @@ def test_sfp_remove_events(self):

assert port_dict == removal

def test_sfp_removal_from_dict(self):
"""
Test that SFP object is properly removed from sfp_obj_dict when SFP status is SFP_STATUS_REMOVED
"""
from xcvrd.xcvrd import SfpStateUpdateTask, PortChangeEvent, DEFAULT_NAMESPACE
from xcvrd.xcvrd_utilities.port_event_helper import PortMapping
import threading
from unittest.mock import MagicMock

# Setup test environment
port_mapping = PortMapping()
stop_event = threading.Event()
mock_sfp_obj_dict = {1: MagicMock()} # Create a mock SFP object for port 1
task = SfpStateUpdateTask(DEFAULT_NAMESPACE, port_mapping, mock_sfp_obj_dict, stop_event)

# Simulate SFP removal event
port_change_event = PortChangeEvent('Ethernet0', 1, 0, PortChangeEvent.PORT_DEL)
task.on_port_update_event(port_change_event)

# Verify that the SFP object is removed from the dictionary
assert 1 not in mock_sfp_obj_dict

@patch('xcvrd.xcvrd.platform_chassis')
@patch('xcvrd.xcvrd.platform_sfputil')
def test_wrapper_get_presence(self, mock_sfputil, mock_chassis):
Expand Down
2 changes: 2 additions & 0 deletions sonic-xcvrd/xcvrd/xcvrd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1735,6 +1735,8 @@ def task_worker(self, stopping_event, sfp_error_event):
media_settings_parser.notify_media_setting(logical_port, transceiver_dict, self.xcvr_table_helper, self.port_mapping)
transceiver_dict.clear()
elif value == sfp_status_helper.SFP_STATUS_REMOVED:
# Remove the SFP API object for this physical port
self.sfp_obj_dict.pop(int(key), None)
helper_logger.log_notice("{}: Got SFP removed event".format(logical_port))
state_port_table = self.xcvr_table_helper.get_state_port_tbl(asic_index)
state_port_table.set(logical_port, [(NPU_SI_SETTINGS_SYNC_STATUS_KEY, NPU_SI_SETTINGS_DEFAULT_VALUE)])
Expand Down
Loading