Split tests into unit and integration tests
This commit is contained in:
@@ -1,58 +1,5 @@
|
||||
import logging
|
||||
import unittest
|
||||
|
||||
from cassandra.cluster import Cluster
|
||||
from cassandra.connection import _loop
|
||||
from cassandra.policies import HostDistance
|
||||
|
||||
log = logging.getLogger()
|
||||
log.setLevel('DEBUG')
|
||||
log.addHandler(logging.StreamHandler())
|
||||
|
||||
existing_keyspaces = None
|
||||
|
||||
def setup_package():
|
||||
try:
|
||||
cluster = Cluster()
|
||||
cluster.set_core_connections_per_host(HostDistance.LOCAL, 1)
|
||||
cluster.set_max_connections_per_host(HostDistance.LOCAL, 1)
|
||||
session = cluster.connect()
|
||||
except Exception, exc:
|
||||
log.exception('Failed to connect to cluster:')
|
||||
raise unittest.SkipTest('Failed to connect to cluster: %r' % exc)
|
||||
|
||||
try:
|
||||
global existing_keyspaces
|
||||
results = session.execute("SELECT keyspace_name FROM system.schema_keyspaces")
|
||||
existing_keyspaces = set([row.values()[0] for row in results])
|
||||
finally:
|
||||
try:
|
||||
cluster.shutdown()
|
||||
except Exception, exc:
|
||||
log.exception('Failed to connect to cluster:')
|
||||
raise unittest.SkipTest('Failed to connect to cluster: %r' % exc)
|
||||
|
||||
|
||||
def teardown_package():
|
||||
try:
|
||||
cluster = Cluster()
|
||||
cluster.set_core_connections_per_host(HostDistance.LOCAL, 1)
|
||||
cluster.set_max_connections_per_host(HostDistance.LOCAL, 1)
|
||||
session = cluster.connect()
|
||||
except Exception:
|
||||
log.exception('Failed to connect to cluster:')
|
||||
return
|
||||
|
||||
try:
|
||||
if existing_keyspaces:
|
||||
results = session.execute("SELECT keyspace_name FROM system.schema_keyspaces")
|
||||
current_keyspaces = set([row.values()[0] for row in results])
|
||||
for keyspace in current_keyspaces - existing_keyspaces:
|
||||
session.execute("DROP KEYSPACE %s" % (keyspace,))
|
||||
|
||||
finally:
|
||||
try:
|
||||
cluster.shutdown()
|
||||
_loop.stop()
|
||||
except:
|
||||
log.exception('Failed to connect to cluster:')
|
||||
|
55
tests/integration/__init__.py
Normal file
55
tests/integration/__init__.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
import unittest
|
||||
|
||||
from cassandra.cluster import Cluster
|
||||
from cassandra.connection import _loop
|
||||
from cassandra.policies import HostDistance
|
||||
|
||||
existing_keyspaces = None
|
||||
|
||||
def setup_package():
|
||||
try:
|
||||
cluster = Cluster()
|
||||
cluster.set_core_connections_per_host(HostDistance.LOCAL, 1)
|
||||
cluster.set_max_connections_per_host(HostDistance.LOCAL, 1)
|
||||
session = cluster.connect()
|
||||
except Exception, exc:
|
||||
log.exception('Failed to connect to cluster:')
|
||||
raise unittest.SkipTest('Failed to connect to cluster: %r' % exc)
|
||||
|
||||
try:
|
||||
global existing_keyspaces
|
||||
results = session.execute("SELECT keyspace_name FROM system.schema_keyspaces")
|
||||
existing_keyspaces = set([row.values()[0] for row in results])
|
||||
finally:
|
||||
try:
|
||||
cluster.shutdown()
|
||||
except Exception, exc:
|
||||
log.exception('Failed to connect to cluster:')
|
||||
raise unittest.SkipTest('Failed to connect to cluster: %r' % exc)
|
||||
|
||||
|
||||
def teardown_package():
|
||||
try:
|
||||
cluster = Cluster()
|
||||
cluster.set_core_connections_per_host(HostDistance.LOCAL, 1)
|
||||
cluster.set_max_connections_per_host(HostDistance.LOCAL, 1)
|
||||
session = cluster.connect()
|
||||
except Exception:
|
||||
log.exception('Failed to connect to cluster:')
|
||||
return
|
||||
|
||||
try:
|
||||
if existing_keyspaces:
|
||||
results = session.execute("SELECT keyspace_name FROM system.schema_keyspaces")
|
||||
current_keyspaces = set([row.values()[0] for row in results])
|
||||
for keyspace in current_keyspaces - existing_keyspaces:
|
||||
session.execute("DROP KEYSPACE %s" % (keyspace,))
|
||||
|
||||
finally:
|
||||
try:
|
||||
cluster.shutdown()
|
||||
_loop.stop()
|
||||
except:
|
||||
log.exception('Failed to connect to cluster:')
|
75
tests/integration/test_cluster.py
Normal file
75
tests/integration/test_cluster.py
Normal file
@@ -0,0 +1,75 @@
|
||||
import unittest
|
||||
|
||||
from cassandra.cluster import Cluster
|
||||
|
||||
class ClusterTests(unittest.TestCase):
|
||||
|
||||
def test_basic(self):
|
||||
cluster = Cluster()
|
||||
session = cluster.connect()
|
||||
result = session.execute(
|
||||
"""
|
||||
CREATE KEYSPACE clustertests
|
||||
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}
|
||||
""")
|
||||
self.assertEquals(None, result)
|
||||
|
||||
result = session.execute(
|
||||
"""
|
||||
CREATE TABLE clustertests.cf0 (
|
||||
a text,
|
||||
b text,
|
||||
c text,
|
||||
PRIMARY KEY (a, b)
|
||||
)
|
||||
""")
|
||||
self.assertEquals(None, result)
|
||||
|
||||
result = session.execute(
|
||||
"""
|
||||
INSERT INTO clustertests.cf0 (a, b, c) VALUES ('a', 'b', 'c')
|
||||
""")
|
||||
self.assertEquals(None, result)
|
||||
|
||||
result = session.execute("SELECT * FROM clustertests.cf0")
|
||||
self.assertEquals([{'a': 'a', 'b': 'b', 'c': 'c'}], result)
|
||||
|
||||
cluster.shutdown()
|
||||
|
||||
def test_submit_schema_refresh(self):
|
||||
cluster = Cluster()
|
||||
cluster.connect()
|
||||
|
||||
other_cluster = Cluster()
|
||||
session = other_cluster.connect()
|
||||
session.execute(
|
||||
"""
|
||||
CREATE KEYSPACE newkeyspace
|
||||
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}
|
||||
""")
|
||||
|
||||
self.assertNotIn("newkeyspace", cluster.metadata.keyspaces)
|
||||
|
||||
future = cluster.submit_schema_refresh()
|
||||
future.result()
|
||||
|
||||
self.assertIn("newkeyspace", cluster.metadata.keyspaces)
|
||||
|
||||
def test_on_down_and_up(self):
|
||||
cluster = Cluster()
|
||||
session = cluster.connect()
|
||||
host = cluster.metadata.all_hosts()[0]
|
||||
host.monitor.signal_connection_failure(None)
|
||||
cluster.on_down(host)
|
||||
self.assertNotEqual(None, cluster.control_connection._reconnection_handler)
|
||||
self.assertNotIn(host, session._pools)
|
||||
host_reconnector = host._reconnection_handler
|
||||
self.assertNotEqual(None, host_reconnector)
|
||||
|
||||
host.monitor.is_up = True
|
||||
|
||||
cluster.on_up(host)
|
||||
|
||||
self.assertEqual(None, host._reconnection_handler)
|
||||
self.assertTrue(host_reconnector._cancelled)
|
||||
self.assertIn(host, session._pools)
|
@@ -2,7 +2,7 @@ import unittest
|
||||
from mock import Mock, ANY
|
||||
|
||||
from cassandra import ConsistencyLevel
|
||||
from cassandra.cluster import Cluster, Session, ResponseFuture, NoHostAvailable
|
||||
from cassandra.cluster import Session, ResponseFuture, NoHostAvailable
|
||||
from cassandra.connection import ConnectionException
|
||||
from cassandra.decoder import (ReadTimeoutErrorMessage, WriteTimeoutErrorMessage,
|
||||
UnavailableErrorMessage, ResultMessage, QueryMessage,
|
||||
@@ -11,79 +11,6 @@ from cassandra.policies import RetryPolicy
|
||||
from cassandra.pool import NoConnectionsAvailable
|
||||
from cassandra.query import SimpleStatement
|
||||
|
||||
class ClusterTests(unittest.TestCase):
|
||||
|
||||
def test_basic(self):
|
||||
cluster = Cluster()
|
||||
session = cluster.connect()
|
||||
result = session.execute(
|
||||
"""
|
||||
CREATE KEYSPACE clustertests
|
||||
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}
|
||||
""")
|
||||
self.assertEquals(None, result)
|
||||
|
||||
result = session.execute(
|
||||
"""
|
||||
CREATE TABLE clustertests.cf0 (
|
||||
a text,
|
||||
b text,
|
||||
c text,
|
||||
PRIMARY KEY (a, b)
|
||||
)
|
||||
""")
|
||||
self.assertEquals(None, result)
|
||||
|
||||
result = session.execute(
|
||||
"""
|
||||
INSERT INTO clustertests.cf0 (a, b, c) VALUES ('a', 'b', 'c')
|
||||
""")
|
||||
self.assertEquals(None, result)
|
||||
|
||||
result = session.execute("SELECT * FROM clustertests.cf0")
|
||||
self.assertEquals([{'a': 'a', 'b': 'b', 'c': 'c'}], result)
|
||||
|
||||
cluster.shutdown()
|
||||
|
||||
def test_submit_schema_refresh(self):
|
||||
cluster = Cluster()
|
||||
cluster.connect()
|
||||
|
||||
other_cluster = Cluster()
|
||||
session = other_cluster.connect()
|
||||
session.execute(
|
||||
"""
|
||||
CREATE KEYSPACE newkeyspace
|
||||
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}
|
||||
""")
|
||||
|
||||
self.assertNotIn("newkeyspace", cluster.metadata.keyspaces)
|
||||
|
||||
future = cluster.submit_schema_refresh()
|
||||
future.result()
|
||||
|
||||
self.assertIn("newkeyspace", cluster.metadata.keyspaces)
|
||||
|
||||
def test_on_down_and_up(self):
|
||||
cluster = Cluster()
|
||||
session = cluster.connect()
|
||||
host = cluster.metadata.all_hosts()[0]
|
||||
host.monitor.signal_connection_failure(None)
|
||||
cluster.on_down(host)
|
||||
self.assertNotEqual(None, cluster.control_connection._reconnection_handler)
|
||||
self.assertNotIn(host, session._pools)
|
||||
host_reconnector = host._reconnection_handler
|
||||
self.assertNotEqual(None, host_reconnector)
|
||||
|
||||
host.monitor.is_up = True
|
||||
|
||||
cluster.on_up(host)
|
||||
|
||||
self.assertEqual(None, host._reconnection_handler)
|
||||
self.assertTrue(host_reconnector._cancelled)
|
||||
self.assertIn(host, session._pools)
|
||||
|
||||
|
||||
class ResponseFutureTests(unittest.TestCase):
|
||||
|
||||
def make_session(self):
|
0
tests/unit/__init__.py
Normal file
0
tests/unit/__init__.py
Normal file
@@ -20,10 +20,10 @@ class HostConnectionPoolTests(unittest.TestCase):
|
||||
host = Mock(spec=Host, address='ip1')
|
||||
session = self.make_session()
|
||||
conn = NonCallableMagicMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False)
|
||||
session.cluster.connection_factory.return_value = conn
|
||||
session.cluster._connection_factory.return_value = conn
|
||||
|
||||
pool = HostConnectionPool(host, HostDistance.LOCAL, session)
|
||||
session.cluster.connection_factory.assert_called_once_with(host.address)
|
||||
session.cluster._connection_factory.assert_called_once_with(host.address)
|
||||
|
||||
c = pool.borrow_connection(timeout=0.01)
|
||||
self.assertIs(c, conn)
|
||||
@@ -38,10 +38,10 @@ class HostConnectionPoolTests(unittest.TestCase):
|
||||
host = Mock(spec=Host, address='ip1')
|
||||
session = self.make_session()
|
||||
conn = NonCallableMagicMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False)
|
||||
session.cluster.connection_factory.return_value = conn
|
||||
session.cluster._connection_factory.return_value = conn
|
||||
|
||||
pool = HostConnectionPool(host, HostDistance.LOCAL, session)
|
||||
session.cluster.connection_factory.assert_called_once_with(host.address)
|
||||
session.cluster._connection_factory.assert_called_once_with(host.address)
|
||||
|
||||
pool.borrow_connection(timeout=0.01)
|
||||
self.assertEqual(1, conn.in_flight)
|
||||
@@ -56,10 +56,10 @@ class HostConnectionPoolTests(unittest.TestCase):
|
||||
host = Mock(spec=Host, address='ip1')
|
||||
session = self.make_session()
|
||||
conn = NonCallableMagicMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False)
|
||||
session.cluster.connection_factory.return_value = conn
|
||||
session.cluster._connection_factory.return_value = conn
|
||||
|
||||
pool = HostConnectionPool(host, HostDistance.LOCAL, session)
|
||||
session.cluster.connection_factory.assert_called_once_with(host.address)
|
||||
session.cluster._connection_factory.assert_called_once_with(host.address)
|
||||
|
||||
pool.borrow_connection(timeout=0.01)
|
||||
self.assertEqual(1, conn.in_flight)
|
||||
@@ -80,7 +80,7 @@ class HostConnectionPoolTests(unittest.TestCase):
|
||||
host = Mock(spec=Host, address='ip1')
|
||||
session = self.make_session()
|
||||
conn = NonCallableMagicMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False)
|
||||
session.cluster.connection_factory.return_value = conn
|
||||
session.cluster._connection_factory.return_value = conn
|
||||
session.cluster.get_core_connections_per_host.return_value = 1
|
||||
|
||||
# manipulate the core connection setting so that we can
|
||||
@@ -121,13 +121,13 @@ class HostConnectionPoolTests(unittest.TestCase):
|
||||
host = Mock(spec=Host, address='ip1')
|
||||
session = self.make_session()
|
||||
conn = NonCallableMagicMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False)
|
||||
session.cluster.connection_factory.return_value = conn
|
||||
session.cluster._connection_factory.return_value = conn
|
||||
|
||||
# core conns = 1, max conns = 2
|
||||
session.cluster.get_max_connections_per_host.return_value = 2
|
||||
|
||||
pool = HostConnectionPool(host, HostDistance.LOCAL, session)
|
||||
session.cluster.connection_factory.assert_called_once_with(host.address)
|
||||
session.cluster._connection_factory.assert_called_once_with(host.address)
|
||||
|
||||
pool.borrow_connection(timeout=0.01)
|
||||
self.assertEqual(1, conn.in_flight)
|
||||
@@ -145,10 +145,10 @@ class HostConnectionPoolTests(unittest.TestCase):
|
||||
host = Mock(spec=Host, address='ip1')
|
||||
session = self.make_session()
|
||||
conn = NonCallableMagicMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False)
|
||||
session.cluster.connection_factory.return_value = conn
|
||||
session.cluster._connection_factory.return_value = conn
|
||||
|
||||
pool = HostConnectionPool(host, HostDistance.LOCAL, session)
|
||||
session.cluster.connection_factory.assert_called_once_with(host.address)
|
||||
session.cluster._connection_factory.assert_called_once_with(host.address)
|
||||
|
||||
pool.borrow_connection(timeout=0.01)
|
||||
conn.is_defunct = True
|
||||
@@ -165,10 +165,10 @@ class HostConnectionPoolTests(unittest.TestCase):
|
||||
host.monitor = Mock(spec=HealthMonitor)
|
||||
session = self.make_session()
|
||||
conn = NonCallableMagicMock(spec=Connection, in_flight=0, is_defunct=False, is_closed=False)
|
||||
session.cluster.connection_factory.return_value = conn
|
||||
session.cluster._connection_factory.return_value = conn
|
||||
|
||||
pool = HostConnectionPool(host, HostDistance.LOCAL, session)
|
||||
session.cluster.connection_factory.assert_called_once_with(host.address)
|
||||
session.cluster._connection_factory.assert_called_once_with(host.address)
|
||||
|
||||
pool.borrow_connection(timeout=0.01)
|
||||
conn.is_defunct = True
|
Reference in New Issue
Block a user