/usr/lib/python2.7/dist-packages/oslo_service/tests/test_loopingcall.py is in python-oslo.service 1.8.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | # Copyright 2012 Red Hat, 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.
from eventlet.green import threading as greenthreading
import mock
from oslotest import base as test_base
from oslo_service import loopingcall
class LoopingCallTestCase(test_base.BaseTestCase):
def setUp(self):
super(LoopingCallTestCase, self).setUp()
self.num_runs = 0
def test_return_true(self):
def _raise_it():
raise loopingcall.LoopingCallDone(True)
timer = loopingcall.FixedIntervalLoopingCall(_raise_it)
self.assertTrue(timer.start(interval=0.5).wait())
def test_return_false(self):
def _raise_it():
raise loopingcall.LoopingCallDone(False)
timer = loopingcall.FixedIntervalLoopingCall(_raise_it)
self.assertFalse(timer.start(interval=0.5).wait())
def test_terminate_on_exception(self):
def _raise_it():
raise RuntimeError()
timer = loopingcall.FixedIntervalLoopingCall(_raise_it)
self.assertRaises(RuntimeError, timer.start(interval=0.5).wait)
def _raise_and_then_done(self):
if self.num_runs == 0:
raise loopingcall.LoopingCallDone(False)
else:
self.num_runs = self.num_runs - 1
raise RuntimeError()
def test_do_not_stop_on_exception(self):
self.num_runs = 2
timer = loopingcall.FixedIntervalLoopingCall(self._raise_and_then_done)
res = timer.start(interval=0.5, stop_on_exception=False).wait()
self.assertFalse(res)
def _wait_for_zero(self):
"""Called at an interval until num_runs == 0."""
if self.num_runs == 0:
raise loopingcall.LoopingCallDone(False)
else:
self.num_runs = self.num_runs - 1
def test_no_double_start(self):
wait_ev = greenthreading.Event()
def _run_forever_until_set():
if wait_ev.is_set():
raise loopingcall.LoopingCallDone(True)
timer = loopingcall.FixedIntervalLoopingCall(_run_forever_until_set)
timer.start(interval=0.01)
self.assertRaises(RuntimeError, timer.start, interval=0.01)
wait_ev.set()
timer.wait()
def test_repeat(self):
self.num_runs = 2
timer = loopingcall.FixedIntervalLoopingCall(self._wait_for_zero)
self.assertFalse(timer.start(interval=0.5).wait())
def assertAlmostEqual(self, expected, actual, precision=7, message=None):
self.assertEqual(0, round(actual - expected, precision), message)
@mock.patch('eventlet.greenthread.sleep')
@mock.patch('oslo_utils.timeutils.now')
def test_interval_adjustment(self, time_mock, sleep_mock):
"""Ensure the interval is adjusted to account for task duration."""
self.num_runs = 3
now = 1234567890
second = 1
smidgen = 0.01
time_mock.side_effect = [now, # restart
now + second - smidgen, # end
now, # restart
now + second + second, # end
now, # restart
now + second + smidgen, # end
now] # restart
timer = loopingcall.FixedIntervalLoopingCall(self._wait_for_zero)
timer.start(interval=1.01).wait()
expected_calls = [0.02, 0.00, 0.00]
for i, call in enumerate(sleep_mock.call_args_list):
expected = expected_calls[i]
args, kwargs = call
actual = args[0]
message = ('Call #%d, expected: %s, actual: %s' %
(i, expected, actual))
self.assertAlmostEqual(expected, actual, message=message)
class DynamicLoopingCallTestCase(test_base.BaseTestCase):
def setUp(self):
super(DynamicLoopingCallTestCase, self).setUp()
self.num_runs = 0
def test_return_true(self):
def _raise_it():
raise loopingcall.LoopingCallDone(True)
timer = loopingcall.DynamicLoopingCall(_raise_it)
self.assertTrue(timer.start().wait())
def test_no_double_start(self):
wait_ev = greenthreading.Event()
def _run_forever_until_set():
if wait_ev.is_set():
raise loopingcall.LoopingCallDone(True)
else:
return 0.01
timer = loopingcall.DynamicLoopingCall(_run_forever_until_set)
timer.start()
self.assertRaises(RuntimeError, timer.start)
wait_ev.set()
timer.wait()
def test_return_false(self):
def _raise_it():
raise loopingcall.LoopingCallDone(False)
timer = loopingcall.DynamicLoopingCall(_raise_it)
self.assertFalse(timer.start().wait())
def test_terminate_on_exception(self):
def _raise_it():
raise RuntimeError()
timer = loopingcall.DynamicLoopingCall(_raise_it)
self.assertRaises(RuntimeError, timer.start().wait)
def _raise_and_then_done(self):
if self.num_runs == 0:
raise loopingcall.LoopingCallDone(False)
else:
self.num_runs = self.num_runs - 1
raise RuntimeError()
def test_do_not_stop_on_exception(self):
self.num_runs = 2
timer = loopingcall.DynamicLoopingCall(self._raise_and_then_done)
timer.start(stop_on_exception=False).wait()
def _wait_for_zero(self):
"""Called at an interval until num_runs == 0."""
if self.num_runs == 0:
raise loopingcall.LoopingCallDone(False)
else:
self.num_runs = self.num_runs - 1
sleep_for = self.num_runs * 10 + 1 # dynamic duration
return sleep_for
def test_repeat(self):
self.num_runs = 2
timer = loopingcall.DynamicLoopingCall(self._wait_for_zero)
self.assertFalse(timer.start().wait())
def _timeout_task_without_any_return(self):
pass
def test_timeout_task_without_return_and_max_periodic(self):
timer = loopingcall.DynamicLoopingCall(
self._timeout_task_without_any_return
)
self.assertRaises(RuntimeError, timer.start().wait)
def _timeout_task_without_return_but_with_done(self):
if self.num_runs == 0:
raise loopingcall.LoopingCallDone(False)
else:
self.num_runs = self.num_runs - 1
@mock.patch('eventlet.greenthread.sleep')
def test_timeout_task_without_return(self, sleep_mock):
self.num_runs = 1
timer = loopingcall.DynamicLoopingCall(
self._timeout_task_without_return_but_with_done
)
timer.start(periodic_interval_max=5).wait()
sleep_mock.assert_has_calls([mock.call(5)])
@mock.patch('eventlet.greenthread.sleep')
def test_interval_adjustment(self, sleep_mock):
self.num_runs = 2
timer = loopingcall.DynamicLoopingCall(self._wait_for_zero)
timer.start(periodic_interval_max=5).wait()
sleep_mock.assert_has_calls([mock.call(5), mock.call(1)])
@mock.patch('eventlet.greenthread.sleep')
def test_initial_delay(self, sleep_mock):
self.num_runs = 1
timer = loopingcall.DynamicLoopingCall(self._wait_for_zero)
timer.start(initial_delay=3).wait()
sleep_mock.assert_has_calls([mock.call(3), mock.call(1)])
class TestBackOffLoopingCall(test_base.BaseTestCase):
@mock.patch('random.SystemRandom.gauss')
@mock.patch('eventlet.greenthread.sleep')
def test_exponential_backoff(self, sleep_mock, random_mock):
def false():
return False
random_mock.return_value = .8
self.assertRaises(loopingcall.LoopingCallTimeOut,
loopingcall.BackOffLoopingCall(false).start()
.wait)
expected_times = [mock.call(1.6000000000000001),
mock.call(2.5600000000000005),
mock.call(4.096000000000001),
mock.call(6.5536000000000021),
mock.call(10.485760000000004),
mock.call(16.777216000000006),
mock.call(26.843545600000013),
mock.call(42.949672960000022),
mock.call(68.719476736000033),
mock.call(109.95116277760006)]
self.assertEqual(expected_times, sleep_mock.call_args_list)
@mock.patch('random.SystemRandom.gauss')
@mock.patch('eventlet.greenthread.sleep')
def test_no_backoff(self, sleep_mock, random_mock):
random_mock.return_value = 1
func = mock.Mock()
# func.side_effect
func.side_effect = [True, True, True, loopingcall.LoopingCallDone(
retvalue='return value')]
retvalue = loopingcall.BackOffLoopingCall(func).start().wait()
expected_times = [mock.call(1), mock.call(1), mock.call(1)]
self.assertEqual(expected_times, sleep_mock.call_args_list)
self.assertTrue(retvalue, 'return value')
@mock.patch('random.SystemRandom.gauss')
@mock.patch('eventlet.greenthread.sleep')
def test_no_sleep(self, sleep_mock, random_mock):
# Any call that executes properly the first time shouldn't sleep
random_mock.return_value = 1
func = mock.Mock()
# func.side_effect
func.side_effect = loopingcall.LoopingCallDone(retvalue='return value')
retvalue = loopingcall.BackOffLoopingCall(func).start().wait()
self.assertFalse(sleep_mock.called)
self.assertTrue(retvalue, 'return value')
@mock.patch('random.SystemRandom.gauss')
@mock.patch('eventlet.greenthread.sleep')
def test_max_interval(self, sleep_mock, random_mock):
def false():
return False
random_mock.return_value = .8
self.assertRaises(loopingcall.LoopingCallTimeOut,
loopingcall.BackOffLoopingCall(false).start(
max_interval=60)
.wait)
expected_times = [mock.call(1.6000000000000001),
mock.call(2.5600000000000005),
mock.call(4.096000000000001),
mock.call(6.5536000000000021),
mock.call(10.485760000000004),
mock.call(16.777216000000006),
mock.call(26.843545600000013),
mock.call(42.949672960000022),
mock.call(60),
mock.call(60),
mock.call(60)]
self.assertEqual(expected_times, sleep_mock.call_args_list)
class AnException(Exception):
pass
class UnknownException(Exception):
pass
class RetryDecoratorTest(test_base.BaseTestCase):
"""Tests for retry decorator class."""
def test_retry(self):
result = "RESULT"
@loopingcall.RetryDecorator()
def func(*args, **kwargs):
return result
self.assertEqual(result, func())
def func2(*args, **kwargs):
return result
retry = loopingcall.RetryDecorator()
self.assertEqual(result, retry(func2)())
self.assertTrue(retry._retry_count == 0)
def test_retry_with_expected_exceptions(self):
result = "RESULT"
responses = [AnException(None),
AnException(None),
result]
def func(*args, **kwargs):
response = responses.pop(0)
if isinstance(response, Exception):
raise response
return response
sleep_time_incr = 0.01
retry_count = 2
retry = loopingcall.RetryDecorator(10, sleep_time_incr, 10,
(AnException,))
self.assertEqual(result, retry(func)())
self.assertTrue(retry._retry_count == retry_count)
self.assertEqual(retry_count * sleep_time_incr, retry._sleep_time)
def test_retry_with_max_retries(self):
responses = [AnException(None),
AnException(None),
AnException(None)]
def func(*args, **kwargs):
response = responses.pop(0)
if isinstance(response, Exception):
raise response
return response
retry = loopingcall.RetryDecorator(2, 0, 0,
(AnException,))
self.assertRaises(AnException, retry(func))
self.assertTrue(retry._retry_count == 2)
def test_retry_with_unexpected_exception(self):
def func(*args, **kwargs):
raise UnknownException(None)
retry = loopingcall.RetryDecorator()
self.assertRaises(UnknownException, retry(func))
self.assertTrue(retry._retry_count == 0)
|