/usr/lib/python2.7/dist-packages/futurist/tests/test_periodics.py is in python-futurist 1.6.0-0ubuntu1.
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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | # -*- coding: utf-8 -*-
# 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.
import contextlib
import functools
import threading
import time
import eventlet
from eventlet.green import threading as green_threading
import mock
import testscenarios
import futurist
from futurist import periodics
from futurist.tests import base
@periodics.periodic(1)
def every_one_sec(cb):
cb()
@periodics.periodic(0.5)
def every_half_sec(cb):
cb()
@contextlib.contextmanager
def create_destroy_thread(run_what, *args, **kwargs):
t = threading.Thread(target=run_what, args=args, kwargs=kwargs)
t.daemon = True
t.start()
try:
yield
finally:
t.join()
@contextlib.contextmanager
def create_destroy_green_thread(run_what, *args, **kwargs):
t = eventlet.spawn(run_what, *args, **kwargs)
try:
yield
finally:
t.wait()
class TestPeriodicsStrategies(base.TestCase):
def test_invalids(self):
self.assertRaises(ValueError,
periodics.PeriodicWorker, [],
schedule_strategy='not_a_strategy')
class TestPeriodics(testscenarios.TestWithScenarios, base.TestCase):
scenarios = [
('sync', {'executor_cls': futurist.SynchronousExecutor,
'executor_kwargs': {},
'create_destroy': create_destroy_thread,
'sleep': time.sleep,
'event_cls': threading.Event,
'worker_kwargs': {}}),
('thread', {'executor_cls': futurist.ThreadPoolExecutor,
'executor_kwargs': {'max_workers': 2},
'create_destroy': create_destroy_thread,
'sleep': time.sleep,
'event_cls': threading.Event,
'worker_kwargs': {}}),
('green', {'executor_cls': futurist.GreenThreadPoolExecutor,
'executor_kwargs': {'max_workers': 10},
'sleep': eventlet.sleep,
'event_cls': green_threading.Event,
'create_destroy': create_destroy_green_thread,
'worker_kwargs': {'cond_cls': green_threading.Condition,
'event_cls': green_threading.Event}}),
]
def _test_strategy(self, schedule_strategy, nows,
last_now, expected_next):
nows = list(nows)
ev = self.event_cls()
def now_func():
if len(nows) == 1:
ev.set()
return last_now
return nows.pop()
@periodics.periodic(2, run_immediately=False)
def slow_periodic():
pass
callables = [
(slow_periodic, None, None),
]
worker_kwargs = self.worker_kwargs.copy()
worker_kwargs['schedule_strategy'] = schedule_strategy
worker_kwargs['now_func'] = now_func
w = periodics.PeriodicWorker(callables, **worker_kwargs)
with self.create_destroy(w.start):
ev.wait()
w.stop()
schedule_order = w._schedule._ordering
self.assertEqual([(expected_next, 0)], schedule_order)
def _run_work_up_to(self, stop_after_num_calls):
ev = self.event_cls()
called_tracker = []
@periodics.periodic(0.1, run_immediately=True)
def fast_periodic():
called_tracker.append(True)
if len(called_tracker) >= stop_after_num_calls:
ev.set()
callables = [
(fast_periodic, None, None),
]
worker_kwargs = self.worker_kwargs.copy()
w = periodics.PeriodicWorker(callables, **worker_kwargs)
with self.create_destroy(w.start):
ev.wait()
w.stop()
return list(w.iter_watchers())[0], fast_periodic
def test_watching_work(self):
for i in [3, 5, 9, 11]:
watcher, cb = self._run_work_up_to(i)
self.assertEqual(cb, watcher.work.callback)
self.assertGreaterEqual(i, watcher.runs)
self.assertGreaterEqual(i, watcher.successes)
self.assertEqual((), watcher.work.args)
self.assertEqual({}, watcher.work.kwargs)
def test_last_finished_strategy(self):
last_now = 3.2
nows = [
# Initial schedule building.
0,
# Worker run loop fetch time (to see how long to wait).
2,
# Function call start time.
2,
# Function call end time.
3,
# Stop.
-1,
]
nows = list(reversed(nows))
self._test_strategy('last_finished', nows, last_now, 5.0)
def test_last_finished_strategy_jitter(self):
ev = self.event_cls()
calls = []
stop_after_calls = 3
@periodics.periodic(0.1, run_immediately=False)
def fast_periodic():
calls.append(True)
if len(calls) > stop_after_calls:
ev.set()
worker_kwargs = self.worker_kwargs.copy()
worker_kwargs['schedule_strategy'] = 'last_finished_jitter'
callables = [
(fast_periodic, None, None),
]
w = periodics.PeriodicWorker(callables, **worker_kwargs)
with self.create_destroy(w.start):
ev.wait()
w.stop()
self.assertGreaterEqual(len(calls), stop_after_calls)
def test_waiting_immediate_add_processed(self):
ran_at = []
@periodics.periodic(0.1, run_immediately=True)
def activated_periodic():
ran_at.append(time.time())
w = periodics.PeriodicWorker([], **self.worker_kwargs)
with self.create_destroy(w.start, allow_empty=True):
# Give some time for the thread to start...
self.sleep(0.5)
w.add(activated_periodic)
while len(ran_at) == 0:
self.sleep(0.1)
w.stop()
def test_double_start_fail(self):
w = periodics.PeriodicWorker([], **self.worker_kwargs)
with self.create_destroy(w.start, allow_empty=True):
# Give some time for the thread to start...
self.sleep(0.5)
# Now ensure we can't start it again...
self.assertRaises(RuntimeError, w.start)
w.stop()
def test_last_started_strategy(self):
last_now = 3.2
nows = [
# Initial schedule building.
0,
# Worker run loop fetch time (to see how long to wait).
2,
# Function call start time.
2,
# Function call end time.
3,
# Stop.
-1,
]
nows = list(reversed(nows))
self._test_strategy('last_started', nows, last_now, 4.0)
def test_failure_callback_fail(self):
worker_kwargs = self.worker_kwargs.copy()
worker_kwargs['on_failure'] = 'not-a-func'
self.assertRaises(ValueError,
periodics.PeriodicWorker, [], **worker_kwargs)
def test_failure_callback(self):
captures = []
ev = self.event_cls()
@periodics.periodic(0.1)
def failing_always():
raise RuntimeError("Broke!")
def trap_failures(cb, kind, periodic_spacing,
exc_info, traceback=None):
captures.append([cb, kind, periodic_spacing, traceback])
ev.set()
callables = [
(failing_always, None, None),
]
worker_kwargs = self.worker_kwargs.copy()
worker_kwargs['on_failure'] = trap_failures
w = periodics.PeriodicWorker(callables, **worker_kwargs)
with self.create_destroy(w.start, allow_empty=True):
ev.wait()
w.stop()
self.assertEqual(captures[0], [failing_always, periodics.PERIODIC,
0.1, mock.ANY])
def test_aligned_strategy(self):
last_now = 5.5
nows = [
# Initial schedule building.
0,
# Worker run loop fetch time (to see how long to wait).
2,
# Function call start time.
2,
# Function call end time.
5,
# Stop.
-1,
]
nows = list(reversed(nows))
self._test_strategy('aligned_last_finished', nows, last_now, 6.0)
def test_add_on_demand(self):
called = set()
def cb(name):
called.add(name)
callables = []
for i in range(0, 10):
i_cb = functools.partial(cb, '%s_has_called' % i)
callables.append((every_half_sec, (i_cb,), {}))
leftover_callables = list(callables)
w = periodics.PeriodicWorker([], **self.worker_kwargs)
with self.create_destroy(w.start, allow_empty=True):
# NOTE(harlowja): if this never happens, the test will fail
# eventually, with a timeout error..., probably can make it fail
# slightly faster in the future...
while len(called) != len(callables):
if leftover_callables:
cb, args, kwargs = leftover_callables.pop()
w.add(cb, *args, **kwargs)
self.sleep(0.1)
w.stop()
def test_disabled(self):
@periodics.periodic(0.5, enabled=False)
def no_add_me():
pass
@periodics.periodic(0.5)
def add_me():
pass
w = periodics.PeriodicWorker([], **self.worker_kwargs)
self.assertEqual(0, len(w))
self.assertIsNone(w.add(no_add_me))
self.assertEqual(0, len(w))
self.assertIsNotNone(w.add(add_me))
self.assertEqual(1, len(w))
def test_interval_checking(self):
@periodics.periodic(-0.5, enabled=False)
def no_add_me():
pass
w = periodics.PeriodicWorker([], **self.worker_kwargs)
self.assertEqual(0, len(w))
self.assertIsNone(w.add(no_add_me))
self.assertEqual(0, len(w))
self.assertRaises(ValueError, periodics.periodic, -0.5)
def test_is_periodic(self):
@periodics.periodic(0.5, enabled=False)
def no_add_me():
pass
@periodics.periodic(0.5)
def add_me():
pass
self.assertTrue(periodics.is_periodic(add_me))
self.assertTrue(periodics.is_periodic(no_add_me))
self.assertFalse(periodics.is_periodic(self.test_is_periodic))
self.assertFalse(periodics.is_periodic(42))
def test_watcher(self):
def cb():
pass
callables = [
(every_one_sec, (cb,), None),
(every_half_sec, (cb,), None),
]
executor_factory = lambda: self.executor_cls(**self.executor_kwargs)
w = periodics.PeriodicWorker(callables,
executor_factory=executor_factory,
**self.worker_kwargs)
with self.create_destroy(w.start):
self.sleep(2.0)
w.stop()
for watcher in w.iter_watchers():
self.assertGreaterEqual(watcher.runs, 1)
w.reset()
for watcher in w.iter_watchers():
self.assertEqual(watcher.runs, 0)
self.assertEqual(watcher.successes, 0)
self.assertEqual(watcher.failures, 0)
self.assertEqual(watcher.elapsed, 0)
self.assertEqual(watcher.elapsed_waiting, 0)
def test_worker(self):
called = []
def cb():
called.append(1)
callables = [
(every_one_sec, (cb,), None),
(every_half_sec, (cb,), None),
]
executor = self.executor_cls(**self.executor_kwargs)
executor_factory = lambda: executor
w = periodics.PeriodicWorker(callables,
executor_factory=executor_factory,
**self.worker_kwargs)
with self.create_destroy(w.start):
self.sleep(2.0)
w.stop()
am_called = sum(called)
self.assertGreaterEqual(am_called, 4)
self.assertFalse(executor.alive)
def test_existing_executor(self):
called = []
def cb():
called.append(1)
callables = [
(every_one_sec, (cb,), None),
(every_half_sec, (cb,), None),
]
executor = self.executor_cls(**self.executor_kwargs)
executor_factory = periodics.ExistingExecutor(executor)
w = periodics.PeriodicWorker(callables,
executor_factory=executor_factory,
**self.worker_kwargs)
with self.create_destroy(w.start):
self.sleep(2.0)
w.stop()
am_called = sum(called)
self.assertGreaterEqual(am_called, 4)
self.assertTrue(executor.alive)
def test_create_with_arguments(self):
m = mock.Mock()
class Object(object):
@periodics.periodic(0.5)
def func1(self, *args, **kwargs):
m(*args, **kwargs)
executor_factory = lambda: self.executor_cls(**self.executor_kwargs)
w = periodics.PeriodicWorker.create(objects=[Object()],
executor_factory=executor_factory,
args=('foo',),
kwargs={'bar': 'baz'},
**self.worker_kwargs)
with self.create_destroy(w.start):
self.sleep(2.0)
w.stop()
m.assert_called_with('foo', bar='baz')
def test_never_again_exc(self):
m_1 = mock.MagicMock()
m_2 = mock.MagicMock()
@periodics.periodic(0.5)
def run_only_once():
m_1()
raise periodics.NeverAgain("No need to run again !!")
@periodics.periodic(0.5)
def keep_running():
m_2()
callables = [
(run_only_once, None, None),
(keep_running, None, None),
]
executor_factory = lambda: self.executor_cls(**self.executor_kwargs)
w = periodics.PeriodicWorker(callables,
executor_factory=executor_factory,
**self.worker_kwargs)
with self.create_destroy(w.start):
self.sleep(2.0)
w.stop()
for watcher in w.iter_watchers():
self.assertGreaterEqual(watcher.runs, 1)
self.assertGreaterEqual(watcher.successes, 1)
self.assertEqual(watcher.failures, 0)
self.assertEqual(m_1.call_count, 1)
self.assertGreaterEqual(m_2.call_count, 3)
def test_start_with_auto_stop_when_empty_set(self):
@periodics.periodic(0.5)
def run_only_once():
raise periodics.NeverAgain("No need to run again !!")
callables = [
(run_only_once, None, None),
(run_only_once, None, None),
]
executor_factory = lambda: self.executor_cls(**self.executor_kwargs)
w = periodics.PeriodicWorker(callables,
executor_factory=executor_factory,
**self.worker_kwargs)
with self.create_destroy(w.start, auto_stop_when_empty=True):
self.sleep(2.0)
for watcher in w.iter_watchers():
self.assertGreaterEqual(watcher.runs, 1)
self.assertGreaterEqual(watcher.successes, 1)
self.assertEqual(watcher.failures, 0)
self.assertEqual(watcher.requested_stop, True)
def test_add_with_auto_stop_when_empty_set(self):
m = mock.Mock()
@periodics.periodic(0.5)
def run_only_once():
raise periodics.NeverAgain("No need to run again !!")
callables = [
(run_only_once, None, None),
]
executor_factory = lambda: self.executor_cls(**self.executor_kwargs)
w = periodics.PeriodicWorker(callables,
executor_factory=executor_factory,
**self.worker_kwargs)
with self.create_destroy(w.start, auto_stop_when_empty=True):
self.sleep(2.0)
w.add(every_half_sec, m, None)
m.assert_not_called()
class RejectingExecutor(futurist.GreenThreadPoolExecutor):
MAX_REJECTIONS_COUNT = 2
def _reject(self, *args):
if self._rejections_count < self.MAX_REJECTIONS_COUNT:
self._rejections_count += 1
raise futurist.RejectedSubmission()
def __init__(self):
self._rejections_count = 0
super(RejectingExecutor, self).__init__(check_and_reject=self._reject)
class TestPformat(base.TestCase):
def test_invalid(self):
@periodics.periodic(1)
def a():
pass
@periodics.periodic(2)
def b():
pass
@periodics.periodic(3)
def c():
pass
callables = [
(a, None, None),
(b, None, None),
(c, None, None)
]
w = periodics.PeriodicWorker(callables)
self.assertRaises(ValueError, w.pformat, columns=[])
self.assertRaises(ValueError, w.pformat, columns=['not a column'])
class TestRetrySubmission(base.TestCase):
def test_retry_submission(self):
called = []
def cb():
called.append(1)
callables = [
(every_one_sec, (cb,), None),
(every_half_sec, (cb,), None),
]
w = periodics.PeriodicWorker(callables,
executor_factory=RejectingExecutor,
cond_cls=green_threading.Condition,
event_cls=green_threading.Event)
w._RESCHEDULE_DELAY = 0
w._RESCHEDULE_JITTER = 0
with create_destroy_green_thread(w.start):
eventlet.sleep(2.0)
w.stop()
am_called = sum(called)
self.assertGreaterEqual(am_called, 4)
|