def setUp(self):
     if twistedreactor is None:
         raise unittest.SkipTest("Twisted libraries not available")
     twistedreactor.TwistedConnection.initialize_reactor()
     self.reactor_cft_patcher = patch(
         'twisted.internet.reactor.callFromThread')
     self.reactor_run_patcher = patch('twisted.internet.reactor.run')
     self.mock_reactor_cft = self.reactor_cft_patcher.start()
     self.mock_reactor_run = self.reactor_run_patcher.start()
     self.obj_ut = twistedreactor.TwistedConnection('1.2.3.4',
                                                    cql_version='3.0.1')
 def test_multi_timer_validation(self):
     """
     Verify that the timers are called in the correct order
     """
     twistedreactor.TwistedConnection.initialize_reactor()
     connection = twistedreactor.TwistedConnection('1.2.3.4',
                                                   cql_version='3.0.1')
     # Tests timers submitted in order at various timeouts
     submit_and_wait_for_completion(self, connection, 0, 100, 1, 100)
     # Tests timers submitted in reverse order at various timeouts
     submit_and_wait_for_completion(self, connection, 100, 0, -1, 100)
     # Tests timers submitted in varying order at various timeouts
     submit_and_wait_for_completion(self, connection, 0, 100, 1, 100, True)
    def test_timer_cancellation(self, *args):
        """
        Verify that timer cancellation is honored
        """

        # Various lists for tracking callback stage
        connection = twistedreactor.TwistedConnection('1.2.3.4',
                                                      cql_version='3.0.1')
        timeout = .1
        callback = TimerCallback(timeout)
        timer = connection.create_timer(timeout, callback.invoke)
        timer.cancel()
        # Release context allow for timer thread to run.
        time.sleep(.2)
        timer_manager = connection._loop._timers
        # Assert that the cancellation was honored
        self.assertFalse(timer_manager._queue)
        self.assertFalse(timer_manager._new_timers)
        self.assertFalse(callback.was_invoked())