Refactor periodic tasks.
This review allows periodic tasks to be enabled or disabled in the decorator, as well as by specifying an interval which is negative. The spacing between runs of a periodic task is now specified in seconds, with zero meaning the default spacing which is currently 60 seconds. There is also a new argument to the decorator which indicates if a periodic task _needs_ to be run in the nova-compute process. There is also a flag (run_external_periodic_tasks) which can be used to move these periodic tasks out of the nova-compute process. I also remove the periodic_interval flag to services, as the interval between runs is now dynamic based on the number of seconds that a periodic task wants to wait for its next run. For callers who want to twiddle the sleep period (for example unit tests), there is a create() argument periodic_interval_max which lets the period periodic_tasks() specifies be overridden. This is not exposed as a flag because I cannot see a use case for that. It is needed for unit testing however. DocImpact. Resolves bug 939087. Change-Id: I7f245a88b8d229a481c1b65a4c0f1e2769bf3901
This commit is contained in:
109
nova/tests/test_periodic_tasks.py
Normal file
109
nova/tests/test_periodic_tasks.py
Normal file
@@ -0,0 +1,109 @@
|
||||
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||
|
||||
# Copyright 2012 OpenStack LLC
|
||||
# All Rights Reserved.
|
||||
#
|
||||
# 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 fixtures
|
||||
|
||||
from nova import manager
|
||||
from nova import test
|
||||
|
||||
|
||||
class ManagerMetaTestCase(test.TestCase):
|
||||
"""Tests for the meta class which manages the creation of periodic tasks.
|
||||
"""
|
||||
|
||||
def test_meta(self):
|
||||
class Manager(object):
|
||||
__metaclass__ = manager.ManagerMeta
|
||||
|
||||
@manager.periodic_task
|
||||
def foo(self):
|
||||
return 'foo'
|
||||
|
||||
@manager.periodic_task(spacing=4)
|
||||
def bar(self):
|
||||
return 'bar'
|
||||
|
||||
@manager.periodic_task(enabled=False)
|
||||
def baz(self):
|
||||
return 'baz'
|
||||
|
||||
m = Manager()
|
||||
self.assertEqual(2, len(m._periodic_tasks))
|
||||
self.assertEqual(None, m._periodic_spacing['foo'])
|
||||
self.assertEqual(4, m._periodic_spacing['bar'])
|
||||
self.assertFalse('baz' in m._periodic_spacing)
|
||||
|
||||
|
||||
class Manager(test.TestCase):
|
||||
"""Tests the periodic tasks portion of the manager class."""
|
||||
|
||||
def test_periodic_tasks_with_idle(self):
|
||||
class Manager(manager.Manager):
|
||||
@manager.periodic_task(spacing=200)
|
||||
def bar(self):
|
||||
return 'bar'
|
||||
|
||||
m = Manager()
|
||||
self.assertEqual(1, len(m._periodic_tasks))
|
||||
self.assertEqual(200, m._periodic_spacing['bar'])
|
||||
|
||||
# Now a single pass of the periodic tasks
|
||||
idle = m.periodic_tasks(None)
|
||||
self.assertAlmostEqual(60, idle, 1)
|
||||
|
||||
def test_periodic_tasks_constant(self):
|
||||
class Manager(manager.Manager):
|
||||
@manager.periodic_task(spacing=0)
|
||||
def bar(self):
|
||||
return 'bar'
|
||||
|
||||
m = Manager()
|
||||
idle = m.periodic_tasks(None)
|
||||
self.assertAlmostEqual(60, idle, 1)
|
||||
|
||||
def test_periodic_tasks_disabled(self):
|
||||
class Manager(manager.Manager):
|
||||
@manager.periodic_task(spacing=-1)
|
||||
def bar(self):
|
||||
return 'bar'
|
||||
|
||||
m = Manager()
|
||||
idle = m.periodic_tasks(None)
|
||||
self.assertAlmostEqual(60, idle, 1)
|
||||
|
||||
def test_external_running_here(self):
|
||||
self.flags(run_external_periodic_tasks=True)
|
||||
|
||||
class Manager(manager.Manager):
|
||||
@manager.periodic_task(spacing=200, external_process_ok=True)
|
||||
def bar(self):
|
||||
return 'bar'
|
||||
|
||||
m = Manager()
|
||||
self.assertEqual(1, len(m._periodic_tasks))
|
||||
|
||||
def test_external_running_elsewhere(self):
|
||||
self.flags(run_external_periodic_tasks=False)
|
||||
|
||||
class Manager(manager.Manager):
|
||||
@manager.periodic_task(spacing=200, external_process_ok=True)
|
||||
def bar(self):
|
||||
return 'bar'
|
||||
|
||||
m = Manager()
|
||||
self.assertEqual(0, len(m._periodic_tasks))
|
Reference in New Issue
Block a user