Skip to content

Commit 6a9b9b6

Browse files
author
bjmb
committed
Added tests for PYTHON-761
1 parent 852d39e commit 6a9b9b6

3 files changed

Lines changed: 117 additions & 8 deletions

File tree

tests/integration/long/test_loadbalancingpolicies.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,15 @@ def test_white_list(self):
668668
cluster.shutdown()
669669

670670
def test_black_list_with_host_filter_policy(self):
671+
"""
672+
Test to validate removing certain hosts from the query plan with
673+
HostFilterPolicy
674+
@since 3.8
675+
@jira_ticket PYTHON-961
676+
@expected_result the excluded hosts are ignored
677+
678+
@test_category policy
679+
"""
671680
use_singledc()
672681
keyspace = 'test_black_list_with_hfp'
673682
ignored_address = (IP_FORMAT % 2)
@@ -692,9 +701,9 @@ def test_black_list_with_host_filter_policy(self):
692701
self._insert(session, keyspace)
693702
self._query(session, keyspace)
694703

695-
self.coordinator_stats.assert_query_count_equals(self, 1, 6)
704+
self.coordinator_stats.assert_query_count_equals(self, 1, 8)
696705
self.coordinator_stats.assert_query_count_equals(self, 2, 0)
697-
self.coordinator_stats.assert_query_count_equals(self, 3, 6)
706+
self.coordinator_stats.assert_query_count_equals(self, 3, 4)
698707

699708
# policy should not allow reconnecting to ignored host
700709
force_stop(2)

tests/integration/standard/test_policies.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import unittest # noqa
2121

2222
from cassandra import OperationTimedOut
23-
from cassandra.cluster import ExecutionProfile
23+
from cassandra.cluster import ExecutionProfile, Cluster
2424
from cassandra.query import SimpleStatement
25-
from cassandra.policies import ConstantSpeculativeExecutionPolicy, RoundRobinPolicy
25+
from cassandra.policies import ConstantSpeculativeExecutionPolicy, HostFilterPolicy, RoundRobinPolicy, \
26+
SimpleConvictionPolicy
2627
from cassandra.connection import Connection
28+
from cassandra.pool import Host
2729

28-
from tests.integration import BasicSharedKeyspaceUnitTestCase, greaterthancass21
30+
from tests.integration import BasicSharedKeyspaceUnitTestCase, greaterthancass21, PROTOCOL_VERSION
2931
from tests import notwindows
3032

3133
from mock import patch
@@ -47,6 +49,49 @@ def make_query_plan(self, working_keyspace=None, query=None):
4749
return hosts
4850

4951

52+
class HostFilterPolicyTests(unittest.TestCase):
53+
54+
def test_predicate_changes(self):
55+
"""
56+
Test to validate hostfilter reacts correctly when the predicate return
57+
a different subset of the hosts
58+
HostFilterPolicy
59+
@since 3.8
60+
@jira_ticket PYTHON-961
61+
@expected_result the excluded hosts are ignored
62+
63+
@test_category policy
64+
"""
65+
external_event = True
66+
contact_point = "127.0.0.1"
67+
68+
single_host = {Host(contact_point, SimpleConvictionPolicy)}
69+
all_hosts = {Host("127.0.0.{}".format(i), SimpleConvictionPolicy) for i in (1, 2, 3)}
70+
71+
predicate = lambda host: host.address == contact_point if external_event else True
72+
cluster = Cluster((contact_point,), load_balancing_policy=HostFilterPolicy(RoundRobinPolicy(),
73+
predicate=predicate),
74+
protocol_version=PROTOCOL_VERSION, topology_event_refresh_window=0,
75+
status_event_refresh_window=0)
76+
session = cluster.connect(wait_for_all_pools=True)
77+
78+
queried_hosts = set()
79+
for _ in range(10):
80+
response = session.execute("SELECT * from system.local")
81+
queried_hosts.update(response.response_future.attempted_hosts)
82+
83+
self.assertEqual(queried_hosts, single_host)
84+
85+
external_event = False
86+
session.update_created_pools()
87+
88+
queried_hosts = set()
89+
for _ in range(10):
90+
response = session.execute("SELECT * from system.local")
91+
queried_hosts.update(response.response_future.attempted_hosts)
92+
self.assertEqual(queried_hosts, all_hosts)
93+
94+
5095
# This doesn't work well with Windows clock granularity
5196
@notwindows
5297
class SpecExecTest(BasicSharedKeyspaceUnitTestCase):

tests/unit/test_policies.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,13 +1402,13 @@ def test_populate_deferred_to_child(self):
14021402
hosts=hosts
14031403
)
14041404

1405-
def test_child_not_populated_with_filtered_hosts(self):
1405+
def test_child_is_populated_with_filtered_hosts(self):
14061406
hfp = HostFilterPolicy(
14071407
child_policy=Mock(name='child_policy'),
1408-
predicate=lambda host: 'acceptme' in host
1408+
predicate=lambda host: False
14091409
)
14101410
mock_cluster, hosts = (Mock(name='cluster'),
1411-
['acceptme0', 'ignoreme0', 'ignoreme1', 'acceptme1'])
1411+
['acceptme0', 'acceptme1'])
14121412
hfp.populate(mock_cluster, hosts)
14131413
hfp._child_policy.populate.assert_called_once()
14141414
self.assertEqual(
@@ -1439,3 +1439,58 @@ def test_query_plan_deferred_to_child(self):
14391439
query=query
14401440
)
14411441
self.assertEqual(qp, hfp._child_policy.make_query_plan.return_value)
1442+
1443+
def test_wrap_token_aware(self):
1444+
cluster = Mock(spec=Cluster)
1445+
hosts = [Host("127.0.0.{}".format(i), SimpleConvictionPolicy) for i in range(1, 6)]
1446+
for host in hosts:
1447+
host.set_up()
1448+
1449+
def get_replicas(keyspace, packed_key):
1450+
return hosts[:2]
1451+
1452+
cluster.metadata.get_replicas.side_effect = get_replicas
1453+
1454+
child_policy = TokenAwarePolicy(RoundRobinPolicy())
1455+
1456+
hfp = HostFilterPolicy(
1457+
child_policy=child_policy,
1458+
predicate=lambda host: host.address != "127.0.0.1" and host.address != "127.0.0.4"
1459+
)
1460+
hfp.populate(cluster, hosts)
1461+
1462+
# We don't allow randomness for ordering the replicas in RoundRobin
1463+
hfp._child_policy._child_policy._position = 0
1464+
1465+
1466+
mocked_query = Mock()
1467+
query_plan = hfp.make_query_plan("keyspace", mocked_query)
1468+
# First the not filtered replica, and then the rest of the allowed hosts ordered
1469+
query_plan = list(query_plan)
1470+
self.assertEqual(query_plan[0], Host("127.0.0.2", SimpleConvictionPolicy))
1471+
self.assertEqual(set(query_plan[1:]),{Host("127.0.0.3", SimpleConvictionPolicy),
1472+
Host("127.0.0.5", SimpleConvictionPolicy)})
1473+
1474+
def test_create_whitelist(self):
1475+
cluster = Mock(spec=Cluster)
1476+
hosts = [Host("127.0.0.{}".format(i), SimpleConvictionPolicy) for i in range(1, 6)]
1477+
for host in hosts:
1478+
host.set_up()
1479+
1480+
child_policy = RoundRobinPolicy()
1481+
1482+
hfp = HostFilterPolicy(
1483+
child_policy=child_policy,
1484+
predicate=lambda host: host.address == "127.0.0.1" or host.address == "127.0.0.4"
1485+
)
1486+
hfp.populate(cluster, hosts)
1487+
1488+
# We don't allow randomness for ordering the replicas in RoundRobin
1489+
hfp._child_policy._position = 0
1490+
1491+
mocked_query = Mock()
1492+
query_plan = hfp.make_query_plan("keyspace", mocked_query)
1493+
# Only the filtered replicas should be allowed
1494+
self.assertEqual(set(query_plan), {Host("127.0.0.1", SimpleConvictionPolicy),
1495+
Host("127.0.0.4", SimpleConvictionPolicy)})
1496+

0 commit comments

Comments
 (0)