Files
deb-python-taskflow/taskflow/tests/unit/test_decorators.py
Joshua Harlow 23dfff4105 Engine, task, linear_flow unification
In order to move away from the existing flows having their
own implementation of running, start moving the existing
flows to be  patterns that only structure tasks (and impose
constraints about how the group of tasks can run) in useful
ways.

Let the concept of running those patterns be handled by an
engine instead of being handled by the flow itself. This
will allow for varying engines to be able to run flows in
whichever way the engine chooses (as long as the constraints
set up by the flow are observed).

Currently threaded flow and graph flow are broken by this
commit, since they have not been converted to being a
structure of tasks + constraints. The existing engine has
not yet been modified to run those structures either, work
is underway  to remediate this.

Part of: blueprint patterns-and-engines

Followup bugs that must be addressed:
  Bug: 1221448
  Bug: 1221505

Change-Id: I3a8b96179f336d1defe269728ebae0caa3d832d7
2013-09-05 19:26:36 -07:00

139 lines
4.1 KiB
Python

# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (C) 2012-2013 Yahoo! Inc. 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.
from taskflow import decorators
from taskflow.patterns import linear_flow
from taskflow import test
from taskflow.engines.action_engine import engine as eng
def _make_engine(flow):
e = eng.SingleThreadedActionEngine(flow)
e.compile()
return e
class WrapableObjectsTest(test.TestCase):
def test_simple_function(self):
values = []
def revert_one(*args, **kwargs):
values.append('revert one')
@decorators.task(revert=revert_one)
def run_one(*args, **kwargs):
values.append('one')
@decorators.task
def run_fail(*args, **kwargs):
values.append('fail')
raise RuntimeError('Woot!')
flow = linear_flow.Flow('test')
flow.add(
run_one,
run_fail
)
with self.assertRaisesRegexp(RuntimeError, '^Woot'):
e = _make_engine(flow)
e.run()
self.assertEquals(values, ['one', 'fail', 'revert one'])
def test_simple_method(self):
class MyTasks(object):
def __init__(self):
# NOTE(imelnikov): that's really *bad thing* to pass
# data between task like this; though, its good enough
# for our testing here
self.values = []
@decorators.task
def run_one(self, *args, **kwargs):
self.values.append('one')
@decorators.task
def run_fail(self, *args, **kwargs):
self.values.append('fail')
raise RuntimeError('Woot!')
tasks = MyTasks()
flow = linear_flow.Flow('test')
flow.add(
tasks.run_one,
tasks.run_fail
)
with self.assertRaisesRegexp(RuntimeError, '^Woot'):
e = _make_engine(flow)
e.run()
self.assertEquals(tasks.values, ['one', 'fail'])
def test_static_method(self):
values = []
class MyTasks(object):
@decorators.task
@staticmethod
def run_one(*args, **kwargs):
values.append('one')
# NOTE(imelnikov): decorators should work in any order:
@staticmethod
@decorators.task
def run_fail(*args, **kwargs):
values.append('fail')
raise RuntimeError('Woot!')
flow = linear_flow.Flow('test')
flow.add(
MyTasks.run_one,
MyTasks.run_fail
)
with self.assertRaisesRegexp(RuntimeError, '^Woot'):
e = _make_engine(flow)
e.run()
self.assertEquals(values, ['one', 'fail'])
def test_class_method(self):
class MyTasks(object):
values = []
@decorators.task
@classmethod
def run_one(cls, *args, **kwargs):
cls.values.append('one')
# NOTE(imelnikov): decorators should work in any order:
@classmethod
@decorators.task
def run_fail(cls, *args, **kwargs):
cls.values.append('fail')
raise RuntimeError('Woot!')
flow = linear_flow.Flow('test')
flow.add(
MyTasks.run_one,
MyTasks.run_fail
)
with self.assertRaisesRegexp(RuntimeError, '^Woot'):
e = _make_engine(flow)
e.run()
self.assertEquals(MyTasks.values, ['one', 'fail'])