|
20 | 20 | from cassandra.shard_info import _ShardingInfo |
21 | 21 |
|
22 | 22 | import unittest |
23 | | -from threading import Thread, Event, Lock |
| 23 | +from threading import Thread, Event, Lock, Condition |
24 | 24 | from unittest.mock import Mock, NonCallableMagicMock, MagicMock |
25 | 25 |
|
26 | 26 | from cassandra.cluster import Cluster, Session, ShardAwareOptions |
@@ -454,3 +454,89 @@ def test_replace_retries_when_replacement_keyspace_set_fails(self): |
454 | 454 | submitted_fn, submitted_connection = session.submit.call_args.args |
455 | 455 | assert submitted_fn == pool._replace |
456 | 456 | assert submitted_connection is initial_connection |
| 457 | + |
| 458 | + def test_replace_discards_replacement_when_endpoint_changes_during_keyspace_set(self): |
| 459 | + old_endpoint = DefaultEndPoint('127.0.0.1') |
| 460 | + new_endpoint = DefaultEndPoint('127.0.0.2') |
| 461 | + host = Host(old_endpoint, SimpleConvictionPolicy, host_id=uuid.uuid4()) |
| 462 | + session = NonCallableMagicMock(spec=Session, keyspace='ks') |
| 463 | + session.cluster = MagicMock() |
| 464 | + session.cluster.shard_aware_options = ShardAwareOptions() |
| 465 | + session.cluster._endpoints_match.side_effect = Cluster._endpoints_match |
| 466 | + session.remove_pool.return_value = None |
| 467 | + initial_connection = HashableMock( |
| 468 | + spec=Connection, in_flight=0, is_defunct=False, is_closed=False, |
| 469 | + max_request_id=100, signaled_error=False, |
| 470 | + orphaned_threshold_reached=False, |
| 471 | + features=ProtocolFeatures(shard_id=0)) |
| 472 | + replacement_connection = HashableMock( |
| 473 | + spec=Connection, in_flight=0, is_defunct=False, is_closed=False, |
| 474 | + max_request_id=100, signaled_error=False, |
| 475 | + orphaned_threshold_reached=False, |
| 476 | + features=ProtocolFeatures(shard_id=0)) |
| 477 | + replacement_connection.set_keyspace_blocking.side_effect = ( |
| 478 | + lambda keyspace: setattr(host, 'endpoint', new_endpoint)) |
| 479 | + session.cluster.connection_factory.side_effect = [ |
| 480 | + initial_connection, replacement_connection] |
| 481 | + |
| 482 | + pool = HostConnection(host, HostDistance.LOCAL, session) |
| 483 | + pool._is_replacing = True |
| 484 | + |
| 485 | + pool._replace(initial_connection) |
| 486 | + |
| 487 | + replacement_connection.close.assert_called_once_with() |
| 488 | + session.remove_pool.assert_called_once_with( |
| 489 | + host, expected_host=host, expected_endpoint=old_endpoint, |
| 490 | + expected_pool=pool) |
| 491 | + assert pool._connections == {} |
| 492 | + assert not pool._is_replacing |
| 493 | + |
| 494 | + def test_missing_shard_discards_connection_when_endpoint_changes_during_keyspace_set(self): |
| 495 | + old_endpoint = DefaultEndPoint('127.0.0.1') |
| 496 | + new_endpoint = DefaultEndPoint('127.0.0.2') |
| 497 | + host = Host(old_endpoint, SimpleConvictionPolicy, host_id=uuid.uuid4()) |
| 498 | + host.sharding_info = _ShardingInfo( |
| 499 | + shard_id=0, shards_count=1, partitioner='', |
| 500 | + sharding_algorithm='', sharding_ignore_msb=0, |
| 501 | + shard_aware_port='', shard_aware_port_ssl='') |
| 502 | + session = NonCallableMagicMock(spec=Session, keyspace='ks') |
| 503 | + session.cluster = MagicMock() |
| 504 | + session.cluster.shard_aware_options = ShardAwareOptions() |
| 505 | + session.cluster.ssl_options = None |
| 506 | + session.cluster._endpoints_match.side_effect = Cluster._endpoints_match |
| 507 | + session.remove_pool.return_value = None |
| 508 | + connection = HashableMock( |
| 509 | + spec=Connection, in_flight=0, is_defunct=False, is_closed=False, |
| 510 | + max_request_id=100, signaled_error=False, |
| 511 | + orphaned_threshold_reached=False, |
| 512 | + features=ProtocolFeatures(shard_id=0)) |
| 513 | + connection.set_keyspace_blocking.side_effect = ( |
| 514 | + lambda keyspace: setattr(host, 'endpoint', new_endpoint)) |
| 515 | + session.cluster.connection_factory.return_value = connection |
| 516 | + |
| 517 | + pool = HostConnection.__new__(HostConnection) |
| 518 | + pool.host = host |
| 519 | + pool.endpoint = old_endpoint |
| 520 | + pool.host_distance = HostDistance.LOCAL |
| 521 | + pool.is_shutdown = False |
| 522 | + pool._session = session |
| 523 | + pool._lock = Lock() |
| 524 | + pool._stream_available_condition = Condition(Lock()) |
| 525 | + pool._connections = {} |
| 526 | + pool._pending_connections = [] |
| 527 | + pool._connecting = {0} |
| 528 | + pool._excess_connections = set() |
| 529 | + pool._trash = set() |
| 530 | + pool._shard_connections_futures = [] |
| 531 | + pool._keyspace = 'ks' |
| 532 | + pool.advanced_shardaware_block_until = 0 |
| 533 | + pool.tablets_routing_v1 = False |
| 534 | + |
| 535 | + pool._open_connection_to_missing_shard(0) |
| 536 | + |
| 537 | + connection.close.assert_called_once_with() |
| 538 | + session.remove_pool.assert_called_once_with( |
| 539 | + host, expected_host=host, expected_endpoint=old_endpoint, |
| 540 | + expected_pool=pool) |
| 541 | + assert pool._connections == {} |
| 542 | + assert pool._connecting == set() |
0 commit comments