Skip to content

Commit 5282422

Browse files
mambocabbjmb
authored andcommitted
General cleanup for simulacron tests
1 parent 7f2a463 commit 5282422

5 files changed

Lines changed: 38 additions & 40 deletions

File tree

cassandra/io/asyncorereactor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def _cleanup(loop_weakref):
4747
loop = loop_weakref()
4848
except ReferenceError:
4949
return
50+
5051
loop._cleanup()
5152

5253

tests/integration/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def get_unsupported_upper_protocol():
265265
dseonly = unittest.skipUnless(DSE_VERSION, "Test is only applicalbe to DSE clusters")
266266
pypy = unittest.skipUnless(platform.python_implementation() == "PyPy", "Test is skipped unless it's on PyPy")
267267
notpy3 = unittest.skipIf(sys.version_info >= (3, 0), "Test not applicable for Python 3.x runtime")
268-
ifsimulacron = unittest.skipIf(SIMULACRON_JAR is None, "Simulacron jar hasn't been specified")
268+
requiressimulacron = unittest.skipIf(SIMULACRON_JAR is None, "Simulacron jar hasn't been specified")
269269

270270

271271
def wait_for_node_socket(node, timeout):

tests/integration/simulacron/test_connection.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@
1616
except ImportError:
1717
import unittest # noqa
1818

19-
import time
2019
import logging
20+
import time
2121

22-
from cassandra import OperationTimedOut
23-
from cassandra.cluster import Cluster, _Scheduler, EXEC_PROFILE_DEFAULT, ExecutionProfile
24-
from cassandra.policies import RoundRobinPolicy, HostStateListener
2522
from concurrent.futures import ThreadPoolExecutor
2623

27-
from tests.integration import ifsimulacron, CASSANDRA_VERSION
28-
from tests.integration.simulacron.utils import start_and_prime_cluster_defaults, prime_query, stop_simulacron, \
29-
prime_request, PrimeOptions, NO_THEN
24+
from cassandra import OperationTimedOut
25+
from cassandra.cluster import (EXEC_PROFILE_DEFAULT, Cluster, ExecutionProfile,
26+
_Scheduler)
27+
from cassandra.policies import HostStateListener, RoundRobinPolicy
28+
from tests.integration import CASSANDRA_VERSION, requiressimulacron
29+
from tests.integration.simulacron.utils import (NO_THEN, PrimeOptions,
30+
prime_query, prime_request,
31+
start_and_prime_cluster_defaults,
32+
stop_simulacron)
3033

31-
import time
3234

3335
class TrackDownListener(HostStateListener):
3436
hosts_marked_down = []
@@ -43,7 +45,7 @@ def submit(self, fn, *args, **kwargs):
4345
self.called_functions.append(fn.__name__)
4446
return super(ThreadTracker, self).submit(fn, *args, **kwargs)
4547

46-
@ifsimulacron
48+
@requiressimulacron
4749
class ConnectionTest(unittest.TestCase):
4850

4951
def test_heart_beat_timeout(self):
@@ -65,6 +67,7 @@ def test_heart_beat_timeout(self):
6567
idle_heartbeat_interval = 1
6668

6769
start_and_prime_cluster_defaults(number_of_dcs, nodes_per_dc, CASSANDRA_VERSION)
70+
self.addCleanup(stop_simulacron)
6871

6972
listener = TrackDownListener()
7073
executor = ThreadTracker(max_workers=16)
@@ -76,18 +79,17 @@ def test_heart_beat_timeout(self):
7679
executor_threads=16,
7780
execution_profiles={
7881
EXEC_PROFILE_DEFAULT: ExecutionProfile(load_balancing_policy=RoundRobinPolicy())})
82+
self.addCleanup(cluster.shutdown)
7983

8084
cluster.scheduler.shutdown()
8185
cluster.executor = executor
8286
cluster.scheduler = _Scheduler(executor)
8387

8488
session = cluster.connect(wait_for_all_pools=True)
8589
cluster.register_listener(listener)
90+
8691
log = logging.getLogger()
8792
log.setLevel('CRITICAL')
88-
89-
self.addCleanup(cluster.shutdown)
90-
self.addCleanup(stop_simulacron)
9193
self.addCleanup(log.setLevel, "DEBUG")
9294

9395
prime_query(query_to_prime, then=NO_THEN)
@@ -105,7 +107,7 @@ def test_heart_beat_timeout(self):
105107

106108
# We allow from some extra time for all the hosts to be to on_down
107109
# The callbacks should start happening after idle_heartbeat_timeout + idle_heartbeat_interval
108-
time.sleep((idle_heartbeat_timeout + idle_heartbeat_interval)*2)
110+
time.sleep((idle_heartbeat_timeout + idle_heartbeat_interval) * 2)
109111

110112
for host in cluster.metadata.all_hosts():
111113
self.assertIn(host, listener.hosts_marked_down)

tests/integration/simulacron/test_policies.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from cassandra.query import SimpleStatement
2222
from cassandra.policies import ConstantSpeculativeExecutionPolicy, RoundRobinPolicy
2323

24-
from tests.integration import PROTOCOL_VERSION, greaterthancass21, ifsimulacron, SIMULACRON_JAR
24+
from tests.integration import PROTOCOL_VERSION, greaterthancass21, requiressimulacron, SIMULACRON_JAR
2525
from tests import notwindows
2626
from tests.integration.simulacron.utils import start_and_prime_singledc, prime_query, \
2727
stop_simulacron, NO_THEN, clear_queries
@@ -42,7 +42,7 @@ def make_query_plan(self, working_keyspace=None, query=None):
4242

4343
# This doesn't work well with Windows clock granularity
4444
@notwindows
45-
@ifsimulacron
45+
@requiressimulacron
4646
class SpecExecTest(unittest.TestCase):
4747

4848
@classmethod
@@ -160,4 +160,4 @@ def test_speculative_and_timeout(self):
160160
self.assertIsInstance(response_future._final_exception, OperationTimedOut)
161161

162162
# This is because 2.2 / 0.4 + 1 = 6
163-
self.assertEqual(len(response_future.attempted_hosts), 6)
163+
self.assertEqual(len(response_future.attempted_hosts), 6)

tests/integration/simulacron/utils.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,24 @@ def __init__(self, json_text):
2929
self.json = json_text
3030
self.o = json.loads(json_text)
3131

32-
def get_cluster_id(self):
32+
@property
33+
def cluster_id(self):
3334
return self.o["id"]
3435

35-
def get_cluster_name(self):
36+
@property
37+
def cluster_name(self):
3638
return self.o["name"]
3739

38-
def get_data_centers_ids(self):
40+
@property
41+
def data_center_ids(self):
3942
return [dc["id"] for dc in self.o["data_centers"]]
4043

41-
def get_data_centers_names(self):
44+
@property
45+
def data_centers_names(self):
4246
return [dc["name"] for dc in self.o["data_centers"]]
4347

4448
def get_node_ids(self, datacenter_id):
45-
datacenter = list(filter(lambda x: x["id"] == datacenter_id, self.o["data_centers"])).pop()
49+
datacenter = list(filter(lambda x: x["id"] == datacenter_id, self.o["data_centers"])).pop()
4650
return [node["id"] for node in datacenter["nodes"]]
4751

4852

@@ -216,14 +220,9 @@ def set_node(self, cluster_id, datacenter_id, node_id):
216220
self.datacenter_id = datacenter_id
217221
self.node_id = node_id
218222

219-
if self.cluster_id is not None:
220-
self.path += "/{}".format(self.cluster_id)
221-
222-
if self.cluster_id is not None:
223-
self.path += "/{}".format(self.datacenter_id)
224-
225-
if self.cluster_id is not None:
226-
self.path += "/{}".format(self.node_id)
223+
self.path += '/'.join([component for component in
224+
(self.cluster_id, self.datacenter_id, self.node_id)
225+
if component is not None])
227226

228227
def fetch_url_params(self):
229228
return ""
@@ -249,7 +248,7 @@ def fetch_json(self):
249248

250249
def fetch_url_params(self):
251250
return "?cassandra_version={0}&data_centers={1}&name={2}".\
252-
format(self.cassandra_version, self.data_centers, self.cluster_name)
251+
format(self.cassandra_version, self.data_centers, self.cluster_name)
253252

254253

255254
def prime_driver_defaults():
@@ -297,9 +296,9 @@ def start_and_prime_cluster_defaults(number_of_dc=1, nodes_per_dc=3, version=Non
297296

298297

299298
default_column_types = {
300-
"key": "bigint",
301-
"value": "ascii"
302-
}
299+
"key": "bigint",
300+
"value": "ascii"
301+
}
303302

304303
default_row = {"key": 2, "value": "value"}
305304
default_rows = [default_row]
@@ -309,9 +308,7 @@ def prime_request(request):
309308
"""
310309
:param request: It could be PrimeQuery class or an PrimeOptions class
311310
"""
312-
client_simulacron = SimulacronClient()
313-
response = client_simulacron.submit_request(request)
314-
return response
311+
return SimulacronClient().submit_request(request)
315312

316313

317314
def prime_query(query, rows=default_rows, column_types=default_column_types, when=None, then=None, cluster_name=DEFAULT_CLUSTER):
@@ -328,6 +325,4 @@ def clear_queries():
328325
"""
329326
Clears all the queries that have been primed to simulacron
330327
"""
331-
client_simulacron = SimulacronClient()
332-
client_simulacron.clear_all_queries()
333-
328+
SimulacronClient().clear_all_queries()

0 commit comments

Comments
 (0)