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_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)
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)
def test_does_nothing_if_only_exists_in_ctx_manager(self): """Test that no timeout tracer is installed if not previously set.""" new_timeout = 234 with db_timeout(max_time=new_timeout): self.assertRaises(errors.NoTimeoutTracer, current_timeout_tracer) self.assertRaises(errors.NoTimeoutTracer, current_timeout_tracer)
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)
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)