Esempio n. 1
0
    def test_restore_tracer_on_exception(self, mock_gettt):
        """Test that exception in context manager reverts to the previously set
        timeout.
        """

        old_timeout = 123
        old_remaining_time = 113
        inner_timeout = 50
        inner_remaining_time = 16
        new_remaining_time = (old_remaining_time -
                              (inner_timeout - inner_remaining_time))
        transaction_time = old_timeout - old_remaining_time
        enable_timeout_tracer(max_time=old_timeout)
        mock_gettt.return_value = transaction_time

        class ExpectedError(Exception):
            pass

        self.assertEqual(old_remaining_time,
                         current_timeout_tracer().get_remaining_time())

        try:
            with db_timeout(max_time=inner_timeout):
                self.assertEqual(current_timeout_tracer().get_remaining_time(),
                                 inner_timeout)
                mock_gettt.return_value += (inner_timeout -
                                            inner_remaining_time)
                raise ExpectedError()
        except ExpectedError:
            pass

        self.assertEqual(current_timeout_tracer().get_remaining_time(),
                         new_remaining_time)
Esempio n. 2
0
    def test_uses_old_remaining_time_if_lower_than_new_max_time(
            self, mock_gettt):
        """Uses the remaining time from the older tracer if it's lower than the
        new max_time."""

        old_timeout = 123
        old_remaining_time = 20
        inner_timeout = 50
        inner_remaining_time = 5
        new_remaining_time = (old_remaining_time -
                              (old_remaining_time - inner_remaining_time))
        transaction_time = old_timeout - old_remaining_time
        enable_timeout_tracer(max_time=old_timeout)
        mock_gettt.return_value = transaction_time
        self.assertEqual(old_remaining_time,
                         current_timeout_tracer().get_remaining_time())

        with db_timeout(max_time=inner_timeout):
            self.assertEqual(current_timeout_tracer().get_remaining_time(),
                             old_remaining_time)
            mock_gettt.return_value += (old_remaining_time -
                                        inner_remaining_time)

        self.assertEqual(current_timeout_tracer().get_remaining_time(),
                         new_remaining_time)
    def test_uses_old_remaining_time_if_lower_than_new_max_time(
            self, mock_gettt):
        """Uses the remaining time from the older tracer if it's lower than the
        new max_time."""

        old_timeout = 123
        old_remaining_time = 20
        inner_timeout = 50
        inner_remaining_time = 5
        new_remaining_time = (old_remaining_time -
                              (old_remaining_time - inner_remaining_time))
        transaction_time = old_timeout - old_remaining_time
        enable_timeout_tracer(max_time=old_timeout)
        mock_gettt.return_value = transaction_time
        self.assertEqual(old_remaining_time,
                         current_timeout_tracer().get_remaining_time())

        with db_timeout(max_time=inner_timeout):
            self.assertEqual(current_timeout_tracer().get_remaining_time(),
                             old_remaining_time)
            mock_gettt.return_value += (
                old_remaining_time - inner_remaining_time)

        self.assertEqual(current_timeout_tracer().get_remaining_time(),
                         new_remaining_time)
    def test_restore_tracer_on_exception(self, mock_gettt):
        """Test that exception in context manager reverts to the previously set
        timeout.
        """

        old_timeout = 123
        old_remaining_time = 113
        inner_timeout = 50
        inner_remaining_time = 16
        new_remaining_time = (old_remaining_time -
                              (inner_timeout - inner_remaining_time))
        transaction_time = old_timeout - old_remaining_time
        enable_timeout_tracer(max_time=old_timeout)
        mock_gettt.return_value = transaction_time

        class ExpectedError(Exception):
            pass

        self.assertEqual(old_remaining_time,
                         current_timeout_tracer().get_remaining_time())

        try:
            with db_timeout(max_time=inner_timeout):
                self.assertEqual(current_timeout_tracer().get_remaining_time(),
                                 inner_timeout)
                mock_gettt.return_value += (
                    inner_timeout - inner_remaining_time)
                raise ExpectedError()
        except ExpectedError:
            pass

        self.assertEqual(current_timeout_tracer().get_remaining_time(),
                         new_remaining_time)
Esempio n. 5
0
    def test_reset_timeout(self, mock_gettt):
        """Reset local timeout on _reset_timeout_tracer_remaining_time."""

        old_timeout = 123
        inner_timeout = 50
        transaction_time = 42
        mock_gettt.return_value = transaction_time
        enable_timeout_tracer(max_time=old_timeout)
        with db_timeout(max_time=inner_timeout):
            self.assertEqual(current_timeout_tracer().get_remaining_time(),
                             inner_timeout)
            self.assertEqual(inner_timeout + transaction_time,
                             current_timeout_tracer().local.remaining_time)

        self.assertEqual(old_timeout,
                         current_timeout_tracer().local.remaining_time)

        class Connection(object):
            _timeout_tracer_remaining_time = 0

        connection = Connection()
        current_timeout_tracer().connection_commit(connection)

        self.assertIsNone(current_timeout_tracer().local.remaining_time)
        self.assertEqual(current_timeout_tracer().get_remaining_time(),
                         old_timeout - transaction_time)
    def test_reset_timeout(self, mock_gettt):
        """Reset local timeout on _reset_timeout_tracer_remaining_time."""

        old_timeout = 123
        inner_timeout = 50
        transaction_time = 42
        mock_gettt.return_value = transaction_time
        enable_timeout_tracer(max_time=old_timeout)
        with db_timeout(max_time=inner_timeout):
            self.assertEqual(current_timeout_tracer().get_remaining_time(),
                             inner_timeout)
            self.assertEqual(inner_timeout + transaction_time,
                             current_timeout_tracer().local.remaining_time)

        self.assertEqual(old_timeout,
                         current_timeout_tracer().local.remaining_time)

        class Connection(object):
            _timeout_tracer_remaining_time = 0

        connection = Connection()
        current_timeout_tracer().connection_commit(connection)

        self.assertIsNone(current_timeout_tracer().local.remaining_time)
        self.assertEqual(current_timeout_tracer().get_remaining_time(),
                         old_timeout - transaction_time)
    def test_gets_current_timeout_tracer(self):
        """Test that we're able to get the current timeout tracer."""
        enable_timeout_tracer()
        timeout_tracers = [t for t in tracer.get_tracers()
                           if isinstance(t, StorageTimeoutTracer)]
        expected_tracer = timeout_tracers[0]
        actual_tracer = current_timeout_tracer()

        self.assertEqual(actual_tracer, expected_tracer)
Esempio n. 8
0
    def test_changes_timeout_within_context_manager(self, mock_gettt):
        """Timing out changes and reverts to the previously set timeout."""
        old_timeout = 123
        old_remaining_time = 113
        inner_timeout = 50
        inner_remaining_time = 16
        transaction_time = old_timeout - old_remaining_time
        new_remaining_time = (old_remaining_time -
                              (inner_timeout - inner_remaining_time))
        enable_timeout_tracer(max_time=old_timeout)
        mock_gettt.return_value = transaction_time

        with db_timeout(max_time=inner_timeout):
            self.assertEqual(current_timeout_tracer().get_remaining_time(),
                             inner_timeout)
            mock_gettt.return_value += inner_timeout - inner_remaining_time

        self.assertEqual(current_timeout_tracer().get_remaining_time(),
                         new_remaining_time)
Esempio n. 9
0
    def test_forces_change_timeout_within_context_manager(self, mock_gettt):
        """Test that forcing a timeout higher than the currently allowed budget
        is possible and that current budget is not changed, but the current
        remaining time is subtracted from the requested max_time.
        """
        old_timeout = 123
        old_remaining_time = 113
        inner_timeout = 300
        inner_remaining_time = 16
        transaction_time = old_timeout - old_remaining_time
        enable_timeout_tracer(max_time=old_timeout)
        mock_gettt.return_value = transaction_time

        with db_timeout(max_time=inner_timeout, force=True):
            self.assertEqual(current_timeout_tracer().get_remaining_time(),
                             inner_timeout - old_remaining_time)
            mock_gettt.return_value += inner_timeout - inner_remaining_time

        self.assertEqual(current_timeout_tracer().get_remaining_time(),
                         old_remaining_time)
Esempio n. 10
0
    def test_gets_current_timeout_tracer(self):
        """Test that we're able to get the current timeout tracer."""
        enable_timeout_tracer()
        timeout_tracers = [
            t for t in tracer.get_tracers()
            if isinstance(t, StorageTimeoutTracer)
        ]
        expected_tracer = timeout_tracers[0]
        actual_tracer = current_timeout_tracer()

        self.assertEqual(actual_tracer, expected_tracer)
    def test_forces_change_timeout_within_context_manager(self, mock_gettt):
        """Test that forcing a timeout higher than the currently allowed budget
        is possible and that current budget is not changed, but the current
        remaining time is subtracted from the requested max_time.
        """
        old_timeout = 123
        old_remaining_time = 113
        inner_timeout = 300
        inner_remaining_time = 16
        transaction_time = old_timeout - old_remaining_time
        enable_timeout_tracer(max_time=old_timeout)
        mock_gettt.return_value = transaction_time

        with db_timeout(max_time=inner_timeout, force=True):
            self.assertEqual(current_timeout_tracer().get_remaining_time(),
                             inner_timeout - old_remaining_time)
            mock_gettt.return_value += inner_timeout - inner_remaining_time

        self.assertEqual(current_timeout_tracer().get_remaining_time(),
                         old_remaining_time)
    def test_changes_timeout_within_context_manager(self, mock_gettt):
        """Timing out changes and reverts to the previously set timeout."""
        old_timeout = 123
        old_remaining_time = 113
        inner_timeout = 50
        inner_remaining_time = 16
        transaction_time = old_timeout - old_remaining_time
        new_remaining_time = (old_remaining_time -
                              (inner_timeout -
                               inner_remaining_time))
        enable_timeout_tracer(max_time=old_timeout)
        mock_gettt.return_value = transaction_time

        with db_timeout(max_time=inner_timeout):
            self.assertEqual(current_timeout_tracer().get_remaining_time(),
                             inner_timeout)
            mock_gettt.return_value += inner_timeout - inner_remaining_time

        self.assertEqual(current_timeout_tracer().get_remaining_time(),
                         new_remaining_time)
Esempio n. 13
0
 def test_enable_timeout_tracer_max_time(self):
     """The max_time argument is passed to the tracer instance."""
     enable_timeout_tracer(max_time=42)
     timeout_tracer = current_timeout_tracer()
     self.assertEqual(timeout_tracer.max_time, 42)
 def test_enable_timeout_tracer_max_time(self):
     """The max_time argument is passed to the tracer instance."""
     enable_timeout_tracer(max_time=42)
     timeout_tracer = current_timeout_tracer()
     self.assertEqual(timeout_tracer.max_time, 42)