@@ -233,6 +233,35 @@ def test_handle_change_merges_when_host_ids_present(self, mock_query):
233233 self .assertIsNotNone (handler ._routes .get_by_host_id (existing_host ))
234234 self .assertIsNotNone (handler ._routes .get_by_host_id (new_host ))
235235
236+ @patch .object (_ClientRoutesHandler , '_query_routes_for_change_event' )
237+ def test_handle_change_preserves_routes_for_unrelated_connection_ids (self , mock_query ):
238+ """Routes for unrelated connection_ids in mixed events should not be removed."""
239+ handler = _ClientRoutesHandler (self .config )
240+ mock_conn = Mock ()
241+
242+ conn_id = str (self .conn_id )
243+ changed_host = uuid .uuid4 ()
244+ unrelated_host = uuid .uuid4 ()
245+
246+ handler ._routes .update ([
247+ _Route (connection_id = conn_id , host_id = changed_host , address = "old.com" , port = 9042 ),
248+ _Route (connection_id = conn_id , host_id = unrelated_host , address = "keep.com" , port = 9042 ),
249+ ])
250+
251+ mock_query .return_value = [
252+ _Route (connection_id = conn_id , host_id = changed_host , address = "new.com" , port = 9042 ),
253+ ]
254+
255+ handler .handle_client_routes_change (
256+ mock_conn , 5.0 ,
257+ ClientRoutesChangeType .UPDATE_NODES ,
258+ connection_ids = [conn_id , "unrelated-conn-id" ],
259+ host_ids = [str (changed_host ), str (unrelated_host )],
260+ )
261+
262+ self .assertEqual (handler ._routes .get_by_host_id (changed_host ).address , "new.com" )
263+ self .assertEqual (handler ._routes .get_by_host_id (unrelated_host ).address , "keep.com" )
264+
236265 @patch .object (_ClientRoutesHandler , '_query_all_routes_for_connections' )
237266 def test_handle_change_updates_when_no_host_ids (self , mock_query ):
238267 """When no host_ids are provided, routes should be fully replaced."""
@@ -388,6 +417,42 @@ def test_resolve_host_missing_port_raises(self):
388417 with self .assertRaises (ValueError ):
389418 self .handler .resolve_host (host_id )
390419
420+ def test_endpoint_identity_includes_original_port (self ):
421+ host_id = uuid .uuid4 ()
422+ first = ClientRoutesEndPoint (
423+ host_id = host_id ,
424+ handler = self .handler ,
425+ original_address = "10.0.0.1" ,
426+ original_port = 9042 ,
427+ )
428+ second = ClientRoutesEndPoint (
429+ host_id = host_id ,
430+ handler = self .handler ,
431+ original_address = "10.0.0.1" ,
432+ original_port = 9142 ,
433+ )
434+
435+ self .assertNotEqual (first , second )
436+ self .assertEqual (len ({first , second }), 2 )
437+
438+ def test_endpoint_ordering_handles_missing_original_port (self ):
439+ host_id = uuid .uuid4 ()
440+ without_port = ClientRoutesEndPoint (
441+ host_id = host_id ,
442+ handler = self .handler ,
443+ original_address = "10.0.0.1" ,
444+ original_port = None ,
445+ )
446+ with_port = ClientRoutesEndPoint (
447+ host_id = host_id ,
448+ handler = self .handler ,
449+ original_address = "10.0.0.1" ,
450+ original_port = 9042 ,
451+ )
452+
453+ self .assertCountEqual (
454+ sorted ([without_port , with_port ]), [without_port , with_port ])
455+
391456
392457class TestClientRoutesEndPointFactory (unittest .TestCase ):
393458
0 commit comments