From 5e69140c990f38371ae78da6147deb65e17e2bef Mon Sep 17 00:00:00 2001 From: GregBestland Date: Thu, 23 Jul 2015 19:18:02 -0500 Subject: [PATCH] Adding integration and unit tests for PYTHON-358 --- .../standard/test_control_connection.py | 74 +++++++++++++++++++ tests/unit/test_control_connection.py | 38 ++++++++++ 2 files changed, 112 insertions(+) create mode 100644 tests/integration/standard/test_control_connection.py diff --git a/tests/integration/standard/test_control_connection.py b/tests/integration/standard/test_control_connection.py new file mode 100644 index 00000000..af8ad6e0 --- /dev/null +++ b/tests/integration/standard/test_control_connection.py @@ -0,0 +1,74 @@ +# Copyright 2013-2015 DataStax, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License +# +# +# + +try: + import unittest2 as unittest +except ImportError: + import unittest # noqa + + +from cassandra.cluster import Cluster +from cassandra.protocol import ConfigurationException +from tests.integration import use_singledc, PROTOCOL_VERSION +from tests.integration.datatype_utils import update_datatypes + + +def setup_module(): + use_singledc() + update_datatypes() + + +class ControlConnectionTests(unittest.TestCase): + def setUp(self): + self.cluster = Cluster(protocol_version=PROTOCOL_VERSION) + self.session = self.cluster.connect() + + def tearDown(self): + try: + self.session.execute("DROP KEYSPACE keyspacetodrop ") + except (ConfigurationException): + # we already removed the keyspace. + pass + self.cluster.shutdown() + + def test_drop_keyspace(self): + """ + Test to validate that dropping a keyspace with user defined types doesn't kill the control connection. + + + Creates a keyspace, and populates with a user defined type. It then records the control_connection's id. It + will then drop the keyspace and get the id of the control_connection again. They should be the same. If they are + not dropping the keyspace likely caused the control connection to be rebuilt. + + @since 2.7.0 + @jira_ticket PYTHON-358 + @expected_result the control connection is not killed + + @test_category connection + """ + + self.session.execute(""" + CREATE KEYSPACE keyspacetodrop + WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1' } + """) + self.session.set_keyspace("keyspacetodrop") + self.session.execute("CREATE TYPE user (age int, name text)") + self.session.execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen)") + cc_id_pre_drop = id(self.cluster.control_connection._connection) + self.session.execute("DROP KEYSPACE keyspacetodrop") + cc_id_post_drop = id(self.cluster.control_connection._connection) + self.assertEqual(cc_id_post_drop, cc_id_pre_drop) \ No newline at end of file diff --git a/tests/unit/test_control_connection.py b/tests/unit/test_control_connection.py index 1429531c..f1fa9cb6 100644 --- a/tests/unit/test_control_connection.py +++ b/tests/unit/test_control_connection.py @@ -464,3 +464,41 @@ class ControlConnectionTest(unittest.TestCase): cluster.scheduler.schedule_unique.assert_has_calls([call(ANY, cc_no_topo_refresh.refresh_node_list_and_token_map), call(0.0, cc_no_topo_refresh.refresh_schema, schema_event['keyspace'], schema_event['table'], None, None, None)]) + + +class EventTimingTest(unittest.TestCase): + """ + A simple test to validate that event scheduling happens in order + Added for PYTHON-358 + """ + def setUp(self): + self.cluster = MockCluster() + self.connection = MockConnection() + self.time = FakeTime() + + # Use 2 for the schema_event_refresh_window which is what we would normally default to. + self.control_connection = ControlConnection(self.cluster, 1, 2, 0) + self.control_connection._connection = self.connection + self.control_connection._time = self.time + + def test_event_delay_timing(self): + """ + Submits a wide array of events make sure that each is scheduled to occur in the order they were received + """ + prior_delay = 0 + for _ in range(100): + for change_type in ('CREATED', 'DROPPED', 'UPDATED'): + event = { + 'change_type': change_type, + 'keyspace': '1', + 'table': 'table1' + } + # This is to increment the fake time, we don't actually sleep here. + self.time.sleep(.001) + self.cluster.scheduler.reset_mock() + self.control_connection._handle_schema_change(event) + self.cluster.scheduler.mock_calls + # Grabs the delay parameter from the scheduler invocation + current_delay = self.cluster.scheduler.mock_calls[0][1][0] + self.assertLess(prior_delay, current_delay) + prior_delay = current_delay