mirror of
https://github.com/ARM-software/workload-automation.git
synced 2025-09-01 10:52:33 +01:00
Adding intialize and finalize methods to workloads that will only be invoked once per run
- added initialze and finalize methods to workloads, which were the only major extension types that did not have them - Semanatics for initialize/finalize for *all* Extensions are changed so that now they will always run at most once per run. They will not be executed twice even if invoke via istances of different subclasses (if those subclasses defined their own verions, then their versions will be invoked once each, but the base version will only get invoked once).
This commit is contained in:
@@ -27,7 +27,7 @@ from wlauto.core.execution import BySpecRunner, ByIterationRunner
|
||||
from wlauto.exceptions import DeviceError
|
||||
from wlauto.core.configuration import WorkloadRunSpec, RebootPolicy
|
||||
from wlauto.core.instrumentation import Instrument
|
||||
from wlauto.core.device import Device
|
||||
from wlauto.core.device import Device, DeviceMeta
|
||||
from wlauto.core import instrumentation, signal
|
||||
from wlauto.core.workload import Workload
|
||||
from wlauto.core.result import IterationResult
|
||||
@@ -61,8 +61,37 @@ class Mock(object):
|
||||
pass
|
||||
|
||||
|
||||
class BadDeviceMeta(DeviceMeta):
|
||||
|
||||
@classmethod
|
||||
def _implement_virtual(mcs, cls, bases):
|
||||
"""
|
||||
This version of _implement_virtual does not inforce "call global virutals only once"
|
||||
policy, so that intialize() and finalize() my be invoked multiple times to test that
|
||||
the errors they generated are handled correctly.
|
||||
|
||||
"""
|
||||
# pylint: disable=cell-var-from-loop,unused-argument
|
||||
methods = {}
|
||||
for vmname in mcs.virtual_methods:
|
||||
clsmethod = getattr(cls, vmname, None)
|
||||
if clsmethod:
|
||||
basemethods = [getattr(b, vmname) for b in bases if hasattr(b, vmname)]
|
||||
methods[vmname] = [bm for bm in basemethods if bm != clsmethod]
|
||||
methods[vmname].append(clsmethod)
|
||||
def generate_method_wrapper(vname):
|
||||
name__ = vmname
|
||||
def wrapper(self, *args, **kwargs):
|
||||
for dm in methods[name__]:
|
||||
dm(self, *args, **kwargs)
|
||||
return wrapper
|
||||
setattr(cls, vmname, generate_method_wrapper(vmname))
|
||||
|
||||
|
||||
class BadDevice(Device):
|
||||
|
||||
__metaclass__ = BadDeviceMeta
|
||||
|
||||
def __init__(self, when_to_fail, exception=DeviceError):
|
||||
#pylint: disable=super-init-not-called
|
||||
self.when_to_fail = when_to_fail
|
||||
|
@@ -220,6 +220,63 @@ class ExtensionMetaTest(TestCase):
|
||||
assert_equal(acid.v2, 2)
|
||||
assert_equal(acid.vv2, 2)
|
||||
|
||||
def test_initialization(self):
|
||||
class MyExt(Extension):
|
||||
name = 'myext'
|
||||
values = {'a': 0}
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(MyExt, self).__init__(*args, **kwargs)
|
||||
self.instance_init = 0
|
||||
def initialize(self):
|
||||
self.values['a'] += 1
|
||||
|
||||
class MyChildExt(MyExt):
|
||||
name = 'mychildext'
|
||||
def initialize(self):
|
||||
self.instance_init += 1
|
||||
|
||||
ext = _instantiate(MyChildExt)
|
||||
ext.initialize()
|
||||
|
||||
assert_equal(MyExt.values['a'], 1)
|
||||
assert_equal(ext.instance_init, 1)
|
||||
|
||||
def test_initialization_happens_once(self):
|
||||
class MyExt(Extension):
|
||||
name = 'myext'
|
||||
values = {'a': 0}
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(MyExt, self).__init__(*args, **kwargs)
|
||||
self.instance_init = 0
|
||||
self.instance_validate = 0
|
||||
def initialize(self):
|
||||
self.values['a'] += 1
|
||||
def validate(self):
|
||||
self.instance_validate += 1
|
||||
|
||||
class MyChildExt(MyExt):
|
||||
name = 'mychildext'
|
||||
def initialize(self):
|
||||
self.instance_init += 1
|
||||
def validate(self):
|
||||
self.instance_validate += 1
|
||||
|
||||
ext1 = _instantiate(MyExt)
|
||||
ext2 = _instantiate(MyExt)
|
||||
ext3 = _instantiate(MyChildExt)
|
||||
ext1.initialize()
|
||||
ext2.initialize()
|
||||
ext3.initialize()
|
||||
ext1.validate()
|
||||
ext2.validate()
|
||||
ext3.validate()
|
||||
|
||||
assert_equal(MyExt.values['a'], 1)
|
||||
assert_equal(ext1.instance_init, 0)
|
||||
assert_equal(ext3.instance_init, 1)
|
||||
assert_equal(ext1.instance_validate, 1)
|
||||
assert_equal(ext3.instance_validate, 2)
|
||||
|
||||
|
||||
class ParametersTest(TestCase):
|
||||
|
||||
|
Reference in New Issue
Block a user