diff options
-rw-r--r-- | oslo_utils/tests/test_excutils.py | 281 |
1 files changed, 195 insertions, 86 deletions
diff --git a/oslo_utils/tests/test_excutils.py b/oslo_utils/tests/test_excutils.py index b0d42cd..68c1790 100644 --- a/oslo_utils/tests/test_excutils.py +++ b/oslo_utils/tests/test_excutils.py @@ -183,131 +183,240 @@ class ForeverRetryUncaughtExceptionsTest(test_base.BaseTestCase): exc = self.exception_to_raise() while exc is not None: raise exc - exc = self.exception_to_raise() def exception_to_raise(self): return None - def exc_retrier_sequence(self, exc_id=None, - exc_count=None, before_timestamp_calls=(), - after_timestamp_calls=()): + def test_exc_retrier_1exc_gives_1log(self): self.exception_to_raise().AndReturn( - Exception('unexpected %d' % exc_id)) - # Timestamp calls that happen before the logging is possibly triggered. - for timestamp in before_timestamp_calls: - timeutils.now().AndReturn(timestamp) - if exc_count != 0: - logging.exception(mox.In( - 'Unexpected exception occurred %d time(s)' % exc_count)) + Exception('unexpected %d' % 1)) + logging.exception(mox.In( + 'Unexpected exception occurred %d time(s)' % 1)) # Timestamp calls that happen after the logging is possibly triggered. - for timestamp in after_timestamp_calls: + for timestamp in [0]: timeutils.now().AndReturn(timestamp) - def exc_retrier_common_end(self): self.exception_to_raise().AndReturn(None) self.mox.ReplayAll() self.exception_generator() - def test_exc_retrier_1exc_gives_1log(self): - self.exc_retrier_sequence(exc_id=1, exc_count=1, - after_timestamp_calls=[0]) - self.exc_retrier_common_end() - def test_exc_retrier_same_10exc_1min_gives_1log(self): - self.exc_retrier_sequence(exc_id=1, - after_timestamp_calls=[0], exc_count=1) + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + logging.exception(mox.In( + 'Unexpected exception occurred %d time(s)' % 1)) + # Timestamp calls that happen after the logging is possibly triggered. + for timestamp in [0]: + timeutils.now().AndReturn(timestamp) + # By design, the following exception don't get logged because they # are within the same minute. for i in range(2, 11): - self.exc_retrier_sequence(exc_id=1, - before_timestamp_calls=[i], - exc_count=0) - self.exc_retrier_common_end() + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + # Timestamp calls that happen before the logging is possibly + # triggered. + for timestamp in [i]: + timeutils.now().AndReturn(timestamp) + + self.exception_to_raise().AndReturn(None) + self.mox.ReplayAll() + self.exception_generator() def test_exc_retrier_same_2exc_2min_gives_2logs(self): - self.exc_retrier_sequence(exc_id=1, - after_timestamp_calls=[0], exc_count=1) - self.exc_retrier_sequence(exc_id=1, - before_timestamp_calls=[65], exc_count=1, - after_timestamp_calls=[65, 66]) - self.exc_retrier_common_end() + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + logging.exception(mox.In( + 'Unexpected exception occurred %d time(s)' % 1)) + # Timestamp calls that happen after the logging is possibly triggered. + for timestamp in [0]: + timeutils.now().AndReturn(timestamp) + + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + # Timestamp calls that happen before the logging is possibly triggered. + for timestamp in [65]: + timeutils.now().AndReturn(timestamp) + logging.exception(mox.In( + 'Unexpected exception occurred %d time(s)' % 1)) + # Timestamp calls that happen after the logging is possibly triggered. + for timestamp in [65, 66]: + timeutils.now().AndReturn(timestamp) + + self.exception_to_raise().AndReturn(None) + self.mox.ReplayAll() + self.exception_generator() def test_exc_retrier_same_10exc_2min_gives_2logs(self): - self.exc_retrier_sequence(exc_id=1, - after_timestamp_calls=[0], exc_count=1) + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + logging.exception(mox.In( + 'Unexpected exception occurred %d time(s)' % 1)) + # Timestamp calls that happen after the logging is possibly triggered. + for timestamp in [0]: + timeutils.now().AndReturn(timestamp) + for ts in [12, 23, 34, 45]: - self.exc_retrier_sequence(exc_id=1, - before_timestamp_calls=[ts], - exc_count=0) + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + # Timestamp calls that happen before the logging is possibly + # triggered. + for timestamp in [ts]: + timeutils.now().AndReturn(timestamp) + # The previous 4 exceptions are counted here - self.exc_retrier_sequence(exc_id=1, - before_timestamp_calls=[106], - exc_count=5, - after_timestamp_calls=[106, 107]) + + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + # Timestamp calls that happen before the logging is possibly triggered. + for timestamp in [106]: + timeutils.now().AndReturn(timestamp) + logging.exception(mox.In( + 'Unexpected exception occurred %d time(s)' % 5)) + # Timestamp calls that happen after the logging is possibly triggered. + for timestamp in [106, 107]: + timeutils.now().AndReturn(timestamp) + # Again, the following are not logged due to being within # the same minute + for ts in [117, 128, 139, 150]: - self.exc_retrier_sequence(exc_id=1, - before_timestamp_calls=[ts], - exc_count=0) - self.exc_retrier_common_end() + + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + # Timestamp calls that happen before the logging is possibly + # triggered. + for timestamp in [ts]: + timeutils.now().AndReturn(timestamp) + + self.exception_to_raise().AndReturn(None) + self.mox.ReplayAll() + self.exception_generator() def test_exc_retrier_mixed_4exc_1min_gives_2logs(self): - self.exc_retrier_sequence(exc_id=1, - # The stop watch will be started, - # which will consume one timestamp call. - after_timestamp_calls=[0], exc_count=1) + # The stop watch will be started, which will consume one timestamp + # call. + + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + logging.exception(mox.In( + 'Unexpected exception occurred %d time(s)' % 1)) + # Timestamp calls that happen after the logging is possibly triggered. + for timestamp in [0]: + timeutils.now().AndReturn(timestamp) + # By design, this second 'unexpected 1' exception is not counted. This # is likely a rare thing and is a sacrifice for code simplicity. - self.exc_retrier_sequence(exc_id=1, exc_count=0, - # Since the exception will be the same - # the expiry method will be called, which - # uses up a timestamp call. - before_timestamp_calls=[5]) - self.exc_retrier_sequence(exc_id=2, exc_count=1, - # The watch should get reset, which uses - # up two timestamp calls. - after_timestamp_calls=[10, 20]) - # Again, trailing exceptions within a minute are not counted. - self.exc_retrier_sequence(exc_id=2, exc_count=0, - # Since the exception will be the same - # the expiry method will be called, which - # uses up a timestamp call. - before_timestamp_calls=[25]) - self.exc_retrier_common_end() + + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + # Timestamp calls that happen before the logging is possibly triggered. + # Since the exception will be the same the expiry method will be + # called, which uses up a timestamp call. + for timestamp in [5]: + timeutils.now().AndReturn(timestamp) + + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 2)) + logging.exception(mox.In( + 'Unexpected exception occurred %d time(s)' % 1)) + # Timestamp calls that happen after the logging is possibly triggered. + # The watch should get reset, which uses up two timestamp calls. + for timestamp in [10, 20]: + timeutils.now().AndReturn(timestamp) + + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 2)) + # Timestamp calls that happen before the logging is possibly triggered. + # Since the exception will be the same the expiry method will be + # called, which uses up a timestamp call. + for timestamp in [25]: + timeutils.now().AndReturn(timestamp) + + self.exception_to_raise().AndReturn(None) + self.mox.ReplayAll() + self.exception_generator() def test_exc_retrier_mixed_4exc_2min_gives_2logs(self): - self.exc_retrier_sequence(exc_id=1, - # The stop watch will now be started. - after_timestamp_calls=[0], exc_count=1) + + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + logging.exception(mox.In( + 'Unexpected exception occurred %d time(s)' % 1)) + # Timestamp calls that happen after the logging is possibly triggered. + for timestamp in [0]: + timeutils.now().AndReturn(timestamp) + # Again, this second exception of the same type is not counted # for the sake of code simplicity. - self.exc_retrier_sequence(exc_id=1, - before_timestamp_calls=[10], exc_count=0) + + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + # Timestamp calls that happen before the logging is possibly triggered. + for timestamp in [10]: + timeutils.now().AndReturn(timestamp) + # The difference between this and the previous case is the log # is also triggered by more than a minute expiring. - self.exc_retrier_sequence(exc_id=2, exc_count=1, - # The stop watch will now be restarted. - after_timestamp_calls=[100, 105]) - self.exc_retrier_sequence(exc_id=2, - before_timestamp_calls=[110], exc_count=0) - self.exc_retrier_common_end() + + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 2)) + logging.exception(mox.In( + 'Unexpected exception occurred %d time(s)' % 1)) + # Timestamp calls that happen after the logging is possibly triggered. + for timestamp in [100, 105]: + timeutils.now().AndReturn(timestamp) + + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 2)) + # Timestamp calls that happen before the logging is possibly triggered. + for timestamp in [110]: + timeutils.now().AndReturn(timestamp) + + self.exception_to_raise().AndReturn(None) + self.mox.ReplayAll() + self.exception_generator() def test_exc_retrier_mixed_4exc_2min_gives_3logs(self): - self.exc_retrier_sequence(exc_id=1, - # The stop watch will now be started. - after_timestamp_calls=[0], exc_count=1) + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + logging.exception(mox.In( + 'Unexpected exception occurred %d time(s)' % 1)) + # Timestamp calls that happen after the logging is possibly triggered. + for timestamp in [0]: + timeutils.now().AndReturn(timestamp) + # This time the second 'unexpected 1' exception is counted due # to the same exception occurring same when the minute expires. - self.exc_retrier_sequence(exc_id=1, - before_timestamp_calls=[10], exc_count=0) - self.exc_retrier_sequence(exc_id=1, - before_timestamp_calls=[100], - after_timestamp_calls=[100, 105], - exc_count=2) - self.exc_retrier_sequence(exc_id=2, exc_count=1, - after_timestamp_calls=[110, 111]) - self.exc_retrier_common_end() + + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + # Timestamp calls that happen before the logging is possibly triggered. + for timestamp in [10]: + timeutils.now().AndReturn(timestamp) + + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 1)) + # Timestamp calls that happen before the logging is possibly triggered. + for timestamp in [100]: + timeutils.now().AndReturn(timestamp) + logging.exception(mox.In( + 'Unexpected exception occurred %d time(s)' % 2)) + # Timestamp calls that happen after the logging is possibly triggered. + for timestamp in [100, 105]: + timeutils.now().AndReturn(timestamp) + + self.exception_to_raise().AndReturn( + Exception('unexpected %d' % 2)) + logging.exception(mox.In( + 'Unexpected exception occurred %d time(s)' % 1)) + # Timestamp calls that happen after the logging is possibly triggered. + for timestamp in [110, 111]: + timeutils.now().AndReturn(timestamp) + + self.exception_to_raise().AndReturn(None) + self.mox.ReplayAll() + self.exception_generator() class ExceptionFilterTest(test_base.BaseTestCase): |