Skip to content

Commit 1f9bb01

Browse files
author
bjmb
committed
Replaced WhiteHost policy for FilterHost policy in tests
1 parent 852d39e commit 1f9bb01

5 files changed

Lines changed: 62 additions & 32 deletions

File tree

cassandra/policies.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -504,20 +504,16 @@ def __init__(self, child_policy, predicate):
504504
self._predicate = predicate
505505

506506
def on_up(self, host, *args, **kwargs):
507-
if self.predicate(host):
508-
return self._child_policy.on_up(host, *args, **kwargs)
507+
return self._child_policy.on_up(host, *args, **kwargs)
509508

510509
def on_down(self, host, *args, **kwargs):
511-
if self.predicate(host):
512-
return self._child_policy.on_down(host, *args, **kwargs)
510+
return self._child_policy.on_down(host, *args, **kwargs)
513511

514512
def on_add(self, host, *args, **kwargs):
515-
if self.predicate(host):
516-
return self._child_policy.on_add(host, *args, **kwargs)
513+
return self._child_policy.on_add(host, *args, **kwargs)
517514

518515
def on_remove(self, host, *args, **kwargs):
519-
if self.predicate(host):
520-
return self._child_policy.on_remove(host, *args, **kwargs)
516+
return self._child_policy.on_remove(host, *args, **kwargs)
521517

522518
@property
523519
def predicate(self):
@@ -545,10 +541,7 @@ def distance(self, host):
545541
return HostDistance.IGNORED
546542

547543
def populate(self, cluster, hosts):
548-
self._child_policy.populate(
549-
cluster=cluster,
550-
hosts=[h for h in hosts if self.predicate(h)]
551-
)
544+
self._child_policy.populate(cluster=cluster, hosts=hosts)
552545

553546
def make_query_plan(self, working_keyspace=None, query=None):
554547
"""

tests/integration/long/test_failure_types.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from cassandra import (ConsistencyLevel, OperationTimedOut, ReadTimeout, WriteTimeout, ReadFailure, WriteFailure,
1818
FunctionFailure, ProtocolVersion)
1919
from cassandra.cluster import Cluster, NoHostAvailable, ExecutionProfile, EXEC_PROFILE_DEFAULT
20-
from cassandra.policies import WhiteListRoundRobinPolicy
20+
from cassandra.policies import HostFilterPolicy, RoundRobinPolicy
2121
from cassandra.concurrent import execute_concurrent_with_args
2222
from cassandra.query import SimpleStatement
2323
from tests.integration import use_singledc, PROTOCOL_VERSION, get_cluster, setup_keyspace, remove_cluster, get_node
@@ -327,7 +327,9 @@ def setUp(self):
327327
# self.node1, self.node2, self.node3 = get_cluster().nodes.values()
328328

329329
node1 = ExecutionProfile(
330-
load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1'])
330+
load_balancing_policy=HostFilterPolicy(
331+
RoundRobinPolicy(), lambda host: host.address == "127.0.0.1"
332+
)
331333
)
332334
self.cluster = Cluster(protocol_version=PROTOCOL_VERSION, execution_profiles={EXEC_PROFILE_DEFAULT: node1})
333335
self.session = self.cluster.connect(wait_for_all_pools=True)

tests/integration/standard/test_cluster.py

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from cassandra.concurrent import execute_concurrent
3030
from cassandra.policies import (RoundRobinPolicy, ExponentialReconnectionPolicy,
3131
RetryPolicy, SimpleConvictionPolicy, HostDistance,
32-
WhiteListRoundRobinPolicy, AddressTranslator)
32+
AddressTranslator, HostFilterPolicy)
3333
from cassandra.pool import Host
3434
from cassandra.query import SimpleStatement, TraceUnavailable, tuple_factory
3535

@@ -477,7 +477,10 @@ def test_refresh_schema_type(self):
477477
def test_refresh_schema_no_wait(self):
478478
contact_points = [CASSANDRA_IP]
479479
cluster = Cluster(protocol_version=PROTOCOL_VERSION, max_schema_agreement_wait=10,
480-
contact_points=contact_points, load_balancing_policy=WhiteListRoundRobinPolicy(contact_points))
480+
contact_points=contact_points,
481+
load_balancing_policy=HostFilterPolicy(
482+
RoundRobinPolicy(), lambda host: host.address == CASSANDRA_IP
483+
))
481484
session = cluster.connect()
482485

483486
schema_ver = session.execute("SELECT schema_version FROM system.local WHERE key='local'")[0][0]
@@ -618,7 +621,7 @@ def test_trace_unavailable(self):
618621
try:
619622
result = future.get_query_trace(-1.0)
620623
# In case the result has time to come back before this timeout due to a race condition
621-
check_trace(result)
624+
self.check_trace(result)
622625
except TraceUnavailable:
623626
break
624627
else:
@@ -630,7 +633,7 @@ def test_trace_unavailable(self):
630633
try:
631634
result = future.get_query_trace(max_wait=120)
632635
# In case the result has been set check the trace
633-
check_trace(result)
636+
self.check_trace(result)
634637
except TraceUnavailable:
635638
break
636639
else:
@@ -774,7 +777,11 @@ def test_profile_load_balancing(self):
774777
@test_category config_profiles
775778
"""
776779
query = "select release_version from system.local"
777-
node1 = ExecutionProfile(load_balancing_policy=WhiteListRoundRobinPolicy([CASSANDRA_IP]))
780+
node1 = ExecutionProfile(
781+
load_balancing_policy=HostFilterPolicy(
782+
RoundRobinPolicy(), lambda host: host.address == CASSANDRA_IP
783+
)
784+
)
778785
with Cluster(execution_profiles={'node1': node1}) as cluster:
779786
session = cluster.connect(wait_for_all_pools=True)
780787

@@ -925,8 +932,16 @@ def test_profile_pool_management(self):
925932
@test_category config_profiles
926933
"""
927934

928-
node1 = ExecutionProfile(load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1']))
929-
node2 = ExecutionProfile(load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.2']))
935+
node1 = ExecutionProfile(
936+
load_balancing_policy=HostFilterPolicy(
937+
RoundRobinPolicy(), lambda host: host.address == "127.0.0.1"
938+
)
939+
)
940+
node2 = ExecutionProfile(
941+
load_balancing_policy=HostFilterPolicy(
942+
RoundRobinPolicy(), lambda host: host.address == "127.0.0.2"
943+
)
944+
)
930945
with Cluster(execution_profiles={EXEC_PROFILE_DEFAULT: node1, 'node2': node2}) as cluster:
931946
session = cluster.connect(wait_for_all_pools=True)
932947
pools = session.get_pool_state()
@@ -935,7 +950,11 @@ def test_profile_pool_management(self):
935950
self.assertEqual(set(h.address for h in pools), set(('127.0.0.1', '127.0.0.2')))
936951

937952
# dynamically update pools on add
938-
node3 = ExecutionProfile(load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.3']))
953+
node3 = ExecutionProfile(
954+
load_balancing_policy=HostFilterPolicy(
955+
RoundRobinPolicy(), lambda host: host.address == "127.0.0.3"
956+
)
957+
)
939958
cluster.add_execution_profile('node3', node3)
940959
pools = session.get_pool_state()
941960
self.assertEqual(set(h.address for h in pools), set(('127.0.0.1', '127.0.0.2', '127.0.0.3')))
@@ -953,14 +972,22 @@ def test_add_profile_timeout(self):
953972
"""
954973
max_retry_count = 10
955974
for i in range(max_retry_count):
956-
node1 = ExecutionProfile(load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.1']))
975+
node1 = ExecutionProfile(
976+
load_balancing_policy=HostFilterPolicy(
977+
RoundRobinPolicy(), lambda host: host.address == "127.0.0.1"
978+
)
979+
)
957980
with Cluster(execution_profiles={EXEC_PROFILE_DEFAULT: node1}) as cluster:
958981
session = cluster.connect(wait_for_all_pools=True)
959982
pools = session.get_pool_state()
960983
self.assertGreater(len(cluster.metadata.all_hosts()), 2)
961984
self.assertEqual(set(h.address for h in pools), set(('127.0.0.1',)))
962985

963-
node2 = ExecutionProfile(load_balancing_policy=WhiteListRoundRobinPolicy(['127.0.0.2', '127.0.0.3']))
986+
node2 = ExecutionProfile(
987+
load_balancing_policy=HostFilterPolicy(
988+
RoundRobinPolicy(), lambda host: host.address in ["127.0.0.2", "127.0.0.3"]
989+
)
990+
)
964991

965992
start = time.time()
966993
try:
@@ -1030,7 +1057,9 @@ def test_address_translator_with_mixed_nodes(self):
10301057

10311058
@local
10321059
class ContextManagementTest(unittest.TestCase):
1033-
load_balancing_policy = WhiteListRoundRobinPolicy([CASSANDRA_IP])
1060+
load_balancing_policy = HostFilterPolicy(
1061+
RoundRobinPolicy(), lambda host: host.address == CASSANDRA_IP
1062+
)
10341063
cluster_kwargs = {'execution_profiles': {EXEC_PROFILE_DEFAULT: ExecutionProfile(load_balancing_policy=
10351064
load_balancing_policy)},
10361065
'schema_metadata_enabled': False,
@@ -1150,7 +1179,6 @@ def test_down_event_with_active_connection(self):
11501179

11511180
@local
11521181
class DontPrepareOnIgnoredHostsTest(unittest.TestCase):
1153-
11541182
ignored_addresses = ['127.0.0.3']
11551183
ignore_node_3_policy = IgnoredHostPolicy(ignored_addresses)
11561184

@@ -1189,7 +1217,8 @@ def test_prepare_on_ignored_hosts(self):
11891217
@local
11901218
class DuplicateRpcTest(unittest.TestCase):
11911219

1192-
load_balancing_policy = WhiteListRoundRobinPolicy(['127.0.0.1'])
1220+
load_balancing_policy = HostFilterPolicy(RoundRobinPolicy(),
1221+
lambda host: host.address == "127.0.0.1")
11931222

11941223
def setUp(self):
11951224
self.cluster = Cluster(protocol_version=PROTOCOL_VERSION, load_balancing_policy=self.load_balancing_policy)

tests/integration/standard/test_connection.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from cassandra.io.asyncorereactor import AsyncoreConnection
3030
from cassandra.protocol import QueryMessage
3131
from cassandra.connection import Connection
32-
from cassandra.policies import WhiteListRoundRobinPolicy, HostStateListener
32+
from cassandra.policies import HostFilterPolicy, RoundRobinPolicy, HostStateListener
3333
from cassandra.pool import HostConnectionPool
3434

3535
from tests import is_monkey_patched, notwindows
@@ -50,8 +50,12 @@ class ConnectionTimeoutTest(unittest.TestCase):
5050
def setUp(self):
5151
self.defaultInFlight = Connection.max_in_flight
5252
Connection.max_in_flight = 2
53-
self.cluster = Cluster(protocol_version=PROTOCOL_VERSION, load_balancing_policy=
54-
WhiteListRoundRobinPolicy([CASSANDRA_IP]))
53+
self.cluster = Cluster(
54+
protocol_version=PROTOCOL_VERSION,
55+
load_balancing_policy=HostFilterPolicy(
56+
RoundRobinPolicy(), predicate=lambda host: host.address == CASSANDRA_IP
57+
)
58+
)
5559
self.session = self.cluster.connect()
5660

5761
def tearDown(self):

tests/integration/standard/test_metrics.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import time
1616

17-
from cassandra.policies import WhiteListRoundRobinPolicy, FallthroughRetryPolicy
17+
from cassandra.policies import HostFilterPolicy, RoundRobinPolicy, FallthroughRetryPolicy
1818

1919
try:
2020
import unittest2 as unittest
@@ -39,7 +39,9 @@ class MetricsTests(unittest.TestCase):
3939
def setUp(self):
4040
contact_point = ['127.0.0.2']
4141
self.cluster = Cluster(contact_points=contact_point, metrics_enabled=True, protocol_version=PROTOCOL_VERSION,
42-
load_balancing_policy=WhiteListRoundRobinPolicy(contact_point),
42+
load_balancing_policy=HostFilterPolicy(
43+
RoundRobinPolicy(), lambda host: host.address in contact_point
44+
),
4345
default_retry_policy=FallthroughRetryPolicy())
4446
self.session = self.cluster.connect("test3rf", wait_for_all_pools=True)
4547

0 commit comments

Comments
 (0)