Skip to content

Commit 2c85594

Browse files
committed
Moved some utils module to fix import errors in unit tests
1 parent 5d7637d commit 2c85594

File tree

9 files changed

+84
-66
lines changed

9 files changed

+84
-66
lines changed

tests/integration/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ def _get_dse_version_from_cass(cass_version):
166166
cv_string = os.getenv('CASSANDRA_VERSION', None)
167167
mcv_string = os.getenv('MAPPED_CASSANDRA_VERSION', None)
168168
try:
169+
print(cv_string)
170+
sasa
169171
cassandra_version = Version(cv_string) # env var is set to test-dse for DDAC
170172
except:
171173
# fallback to MAPPED_CASSANDRA_VERSION

tests/integration/cloud/test_cloud.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from mock import patch
3232

3333
from tests.integration import requirescloudproxy
34-
from tests.integration.util import wait_until_not_raised
34+
from tests.util import wait_until_not_raised
3535
from tests.integration.cloud import CloudProxyCluster, CLOUD_PROXY_SERVER
3636

3737
DISALLOWED_CONSISTENCIES = [

tests/integration/long/test_topology_change.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from cassandra.policies import HostStateListener
55
from tests.integration import PROTOCOL_VERSION, get_node, use_cluster, local
66
from tests.integration.long.utils import decommission
7-
from tests.integration.util import wait_until
7+
from tests.util import wait_until
88

99

1010
class StateListener(HostStateListener):

tests/integration/simulacron/test_connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
from cassandra.policies import HostStateListener, RoundRobinPolicy
2828

2929
from tests import connection_class, thread_pool_executor_class
30+
from tests.util late
3031
from tests.integration import requiressimulacron, libevtest
31-
from tests.integration.util import assert_quiescent_pool_state, late
32+
from tests.integration.util import assert_quiescent_pool_state
3233
# important to import the patch PROTOCOL_VERSION from the simulacron module
3334
from tests.integration.simulacron import SimulacronBase, PROTOCOL_VERSION
3435
from cassandra.connection import DEFAULT_CQL_VERSION

tests/integration/standard/test_custom_cluster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from cassandra.cluster import Cluster, NoHostAvailable
1616
from tests.integration import use_singledc, get_cluster, remove_cluster, local
17-
from tests.integration.util import wait_until, wait_until_not_raised
17+
from tests.util import wait_until, wait_until_not_raised
1818

1919
try:
2020
import unittest2 as unittest

tests/integration/util.py

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
from tests.integration import PROTOCOL_VERSION
16-
from functools import wraps
1716
import time
1817

1918

@@ -50,60 +49,3 @@ def assert_quiescent_pool_state(test_case, cluster, wait=None):
5049
test_case.assertEqual(connection.highest_request_id, max(req_ids))
5150
if PROTOCOL_VERSION < 3:
5251
test_case.assertEqual(connection.highest_request_id, connection.max_request_id)
53-
54-
55-
def wait_until(condition, delay, max_attempts):
56-
"""
57-
Executes a function at regular intervals while the condition
58-
is false and the amount of attempts < maxAttempts.
59-
:param condition: a function
60-
:param delay: the delay in second
61-
:param max_attempts: the maximum number of attempts. So the timeout
62-
of this function is delay*max_attempts
63-
"""
64-
attempt = 0
65-
while not condition() and attempt < max_attempts:
66-
attempt += 1
67-
time.sleep(delay)
68-
69-
if attempt >= max_attempts:
70-
raise Exception("Condition is still False after {} attempts.".format(max_attempts))
71-
72-
73-
def wait_until_not_raised(condition, delay, max_attempts):
74-
"""
75-
Executes a function at regular intervals while the condition
76-
doesn't raise an exception and the amount of attempts < maxAttempts.
77-
:param condition: a function
78-
:param delay: the delay in second
79-
:param max_attempts: the maximum number of attemps. So the timeout
80-
of this function will be delay*max_attempts
81-
"""
82-
def wrapped_condition():
83-
try:
84-
condition()
85-
except:
86-
return False
87-
88-
return True
89-
90-
attempt = 0
91-
while attempt < (max_attempts-1):
92-
attempt += 1
93-
if wrapped_condition():
94-
return
95-
96-
time.sleep(delay)
97-
98-
# last attempt, let the exception raise
99-
condition()
100-
101-
102-
def late(seconds=1):
103-
def decorator(func):
104-
@wraps(func)
105-
def wrapper(*args, **kwargs):
106-
time.sleep(seconds)
107-
func(*args, **kwargs)
108-
return wrapper
109-
return decorator

tests/unit/advanced/test_auth.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
1516
from puresasl import QOP
1617

1718
try:
@@ -21,10 +22,10 @@
2122

2223
from cassandra.auth import DSEGSSAPIAuthProvider
2324

24-
from tests.integration import requiredse
25-
25+
# Cannot import requiredse from tests.integration
2626
# This auth provider requires kerberos and puresals
27-
@requiredse
27+
DSE_VERSION = os.getenv('DSE_VERSION', None)
28+
@unittest.skipUnless(DSE_VERSION, "DSE required")
2829
class TestGSSAPI(unittest.TestCase):
2930

3031
def test_host_resolution(self):

tests/unit/test_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from cassandra.protocol import (write_stringmultimap, write_int, write_string,
3232
SupportedMessage, ProtocolHandler)
3333

34-
from tests.integration.util import wait_until
34+
from tests.util import wait_until
3535

3636

3737
class ConnectionTest(unittest.TestCase):

tests/util.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright DataStax, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import time
16+
from functools import wraps
17+
18+
def wait_until(condition, delay, max_attempts):
19+
"""
20+
Executes a function at regular intervals while the condition
21+
is false and the amount of attempts < maxAttempts.
22+
:param condition: a function
23+
:param delay: the delay in second
24+
:param max_attempts: the maximum number of attempts. So the timeout
25+
of this function is delay*max_attempts
26+
"""
27+
attempt = 0
28+
while not condition() and attempt < max_attempts:
29+
attempt += 1
30+
time.sleep(delay)
31+
32+
if attempt >= max_attempts:
33+
raise Exception("Condition is still False after {} attempts.".format(max_attempts))
34+
35+
36+
def wait_until_not_raised(condition, delay, max_attempts):
37+
"""
38+
Executes a function at regular intervals while the condition
39+
doesn't raise an exception and the amount of attempts < maxAttempts.
40+
:param condition: a function
41+
:param delay: the delay in second
42+
:param max_attempts: the maximum number of attemps. So the timeout
43+
of this function will be delay*max_attempts
44+
"""
45+
def wrapped_condition():
46+
try:
47+
condition()
48+
except:
49+
return False
50+
51+
return True
52+
53+
attempt = 0
54+
while attempt < (max_attempts-1):
55+
attempt += 1
56+
if wrapped_condition():
57+
return
58+
59+
time.sleep(delay)
60+
61+
# last attempt, let the exception raise
62+
condition()
63+
64+
65+
def late(seconds=1):
66+
def decorator(func):
67+
@wraps(func)
68+
def wrapper(*args, **kwargs):
69+
time.sleep(seconds)
70+
func(*args, **kwargs)
71+
return wrapper
72+
return decorator

0 commit comments

Comments
 (0)