| 1 |
# Copyright (C) 2014 Yahoo! Inc. All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
from automaton import exceptions as excp |
| 16 |
from automaton import runners |
| 17 |
|
| 18 |
from taskflow.engines.action_engine import builder |
| 19 |
from taskflow.engines.action_engine import compiler |
| 20 |
from taskflow.engines.action_engine import executor |
| 21 |
from taskflow.engines.action_engine import runtime |
| 22 |
from taskflow.patterns import linear_flow as lf |
| 23 |
from taskflow import states as st |
| 24 |
from taskflow import storage |
| 25 |
from taskflow import test |
| 26 |
from taskflow.tests import utils as test_utils |
| 27 |
from taskflow.types import notifier |
| 28 |
from taskflow.utils import persistence_utils as pu |
| 29 |
|
| 30 |
|
| 31 |
class BuildersTest(test.TestCase): |
| 32 |
|
| 33 |
def _make_runtime(self, flow, initial_state=None): |
| 34 |
compilation = compiler.PatternCompiler(flow).compile() |
| 35 |
flow_detail = pu.create_flow_detail(flow) |
| 36 |
store = storage.Storage(flow_detail) |
| 37 |
nodes_iter = compilation.execution_graph.nodes(data=True) |
| 38 |
for node, node_attrs in nodes_iter: |
| 39 |
if node_attrs['kind'] in ('task', 'retry'): |
| 40 |
store.ensure_atom(node) |
| 41 |
if initial_state: |
| 42 |
store.set_flow_state(initial_state) |
| 43 |
atom_notifier = notifier.Notifier() |
| 44 |
task_executor = executor.SerialTaskExecutor() |
| 45 |
retry_executor = executor.SerialRetryExecutor() |
| 46 |
task_executor.start() |
| 47 |
self.addCleanup(task_executor.stop) |
| 48 |
r = runtime.Runtime(compilation, store, |
| 49 |
atom_notifier, task_executor, |
| 50 |
retry_executor) |
| 51 |
r.compile() |
| 52 |
return r |
| 53 |
|
| 54 |
def _make_machine(self, flow, initial_state=None): |
| 55 |
runtime = self._make_runtime(flow, initial_state=initial_state) |
| 56 |
machine, memory = runtime.builder.build({}) |
| 57 |
machine_runner = runners.FiniteRunner(machine) |
| 58 |
return (runtime, machine, memory, machine_runner) |
| 59 |
|
| 60 |
def test_run_iterations(self): |
| 61 |
flow = lf.Flow("root") |
| 62 |
tasks = test_utils.make_many( |
| 63 |
1, task_cls=test_utils.TaskNoRequiresNoReturns) |
| 64 |
flow.add(*tasks) |
| 65 |
|
| 66 |
runtime, machine, memory, machine_runner = self._make_machine( |
| 67 |
flow, initial_state=st.RUNNING) |
| 68 |
|
| 69 |
it = machine_runner.run_iter(builder.START) |
| 70 |
prior_state, new_state = next(it) |
| 71 |
self.assertEqual(st.RESUMING, new_state) |
| 72 |
self.assertEqual(0, len(memory.failures)) |
| 73 |
|
| 74 |
prior_state, new_state = next(it) |
| 75 |
self.assertEqual(st.SCHEDULING, new_state) |
| 76 |
self.assertEqual(0, len(memory.failures)) |
| 77 |
|
| 78 |
prior_state, new_state = next(it) |
| 79 |
self.assertEqual(st.WAITING, new_state) |
| 80 |
self.assertEqual(0, len(memory.failures)) |
| 81 |
|
| 82 |
prior_state, new_state = next(it) |
| 83 |
self.assertEqual(st.ANALYZING, new_state) |
| 84 |
self.assertEqual(0, len(memory.failures)) |
| 85 |
|
| 86 |
prior_state, new_state = next(it) |
| 87 |
self.assertEqual(builder.GAME_OVER, new_state) |
| 88 |
self.assertEqual(0, len(memory.failures)) |
| 89 |
prior_state, new_state = next(it) |
| 90 |
self.assertEqual(st.SUCCESS, new_state) |
| 91 |
self.assertEqual(0, len(memory.failures)) |
| 92 |
|
| 93 |
self.assertRaises(StopIteration, next, it) |
| 94 |
|
| 95 |
def test_run_iterations_reverted(self): |
| 96 |
flow = lf.Flow("root") |
| 97 |
tasks = test_utils.make_many( |
| 98 |
1, task_cls=test_utils.TaskWithFailure) |
| 99 |
flow.add(*tasks) |
| 100 |
|
| 101 |
runtime, machine, memory, machine_runner = self._make_machine( |
| 102 |
flow, initial_state=st.RUNNING) |
| 103 |
|
| 104 |
transitions = list(machine_runner.run_iter(builder.START)) |
| 105 |
prior_state, new_state = transitions[-1] |
| 106 |
self.assertEqual(st.REVERTED, new_state) |
| 107 |
self.assertEqual([], memory.failures) |
| 108 |
self.assertEqual(st.REVERTED, |
| 109 |
runtime.storage.get_atom_state(tasks[0].name)) |
| 110 |
|
| 111 |
def test_run_iterations_failure(self): |
| 112 |
flow = lf.Flow("root") |
| 113 |
tasks = test_utils.make_many( |
| 114 |
1, task_cls=test_utils.NastyFailingTask) |
| 115 |
flow.add(*tasks) |
| 116 |
|
| 117 |
runtime, machine, memory, machine_runner = self._make_machine( |
| 118 |
flow, initial_state=st.RUNNING) |
| 119 |
|
| 120 |
transitions = list(machine_runner.run_iter(builder.START)) |
| 121 |
prior_state, new_state = transitions[-1] |
| 122 |
self.assertEqual(st.FAILURE, new_state) |
| 123 |
self.assertEqual(1, len(memory.failures)) |
| 124 |
failure = memory.failures[0] |
| 125 |
self.assertTrue(failure.check(RuntimeError)) |
| 126 |
self.assertEqual(st.REVERT_FAILURE, |
| 127 |
runtime.storage.get_atom_state(tasks[0].name)) |
| 128 |
|
| 129 |
def test_run_iterations_suspended(self): |
| 130 |
flow = lf.Flow("root") |
| 131 |
tasks = test_utils.make_many( |
| 132 |
2, task_cls=test_utils.TaskNoRequiresNoReturns) |
| 133 |
flow.add(*tasks) |
| 134 |
|
| 135 |
runtime, machine, memory, machine_runner = self._make_machine( |
| 136 |
flow, initial_state=st.RUNNING) |
| 137 |
|
| 138 |
transitions = [] |
| 139 |
for prior_state, new_state in machine_runner.run_iter(builder.START): |
| 140 |
transitions.append((new_state, memory.failures)) |
| 141 |
if new_state == st.ANALYZING: |
| 142 |
runtime.storage.set_flow_state(st.SUSPENDED) |
| 143 |
state, failures = transitions[-1] |
| 144 |
self.assertEqual(st.SUSPENDED, state) |
| 145 |
self.assertEqual([], failures) |
| 146 |
|
| 147 |
self.assertEqual(st.SUCCESS, |
| 148 |
runtime.storage.get_atom_state(tasks[0].name)) |
| 149 |
self.assertEqual(st.PENDING, |
| 150 |
runtime.storage.get_atom_state(tasks[1].name)) |
| 151 |
|
| 152 |
def test_run_iterations_suspended_failure(self): |
| 153 |
flow = lf.Flow("root") |
| 154 |
sad_tasks = test_utils.make_many( |
| 155 |
1, task_cls=test_utils.NastyFailingTask) |
| 156 |
flow.add(*sad_tasks) |
| 157 |
happy_tasks = test_utils.make_many( |
| 158 |
1, task_cls=test_utils.TaskNoRequiresNoReturns, offset=1) |
| 159 |
flow.add(*happy_tasks) |
| 160 |
|
| 161 |
runtime, machine, memory, machine_runner = self._make_machine( |
| 162 |
flow, initial_state=st.RUNNING) |
| 163 |
|
| 164 |
transitions = [] |
| 165 |
for prior_state, new_state in machine_runner.run_iter(builder.START): |
| 166 |
transitions.append((new_state, memory.failures)) |
| 167 |
if new_state == st.ANALYZING: |
| 168 |
runtime.storage.set_flow_state(st.SUSPENDED) |
| 169 |
state, failures = transitions[-1] |
| 170 |
self.assertEqual(st.SUSPENDED, state) |
| 171 |
self.assertEqual([], failures) |
| 172 |
|
| 173 |
self.assertEqual(st.PENDING, |
| 174 |
runtime.storage.get_atom_state(happy_tasks[0].name)) |
| 175 |
self.assertEqual(st.FAILURE, |
| 176 |
runtime.storage.get_atom_state(sad_tasks[0].name)) |
| 177 |
|
| 178 |
def test_builder_manual_process(self): |
| 179 |
flow = lf.Flow("root") |
| 180 |
tasks = test_utils.make_many( |
| 181 |
1, task_cls=test_utils.TaskNoRequiresNoReturns) |
| 182 |
flow.add(*tasks) |
| 183 |
|
| 184 |
runtime, machine, memory, machine_runner = self._make_machine( |
| 185 |
flow, initial_state=st.RUNNING) |
| 186 |
self.assertRaises(excp.NotInitialized, machine.process_event, 'poke') |
| 187 |
|
| 188 |
# Should now be pending... |
| 189 |
self.assertEqual(st.PENDING, |
| 190 |
runtime.storage.get_atom_state(tasks[0].name)) |
| 191 |
|
| 192 |
machine.initialize() |
| 193 |
self.assertEqual(builder.UNDEFINED, machine.current_state) |
| 194 |
self.assertFalse(machine.terminated) |
| 195 |
self.assertRaises(excp.NotFound, machine.process_event, 'poke') |
| 196 |
last_state = machine.current_state |
| 197 |
|
| 198 |
reaction, terminal = machine.process_event(builder.START) |
| 199 |
self.assertFalse(terminal) |
| 200 |
self.assertIsNotNone(reaction) |
| 201 |
self.assertEqual(st.RESUMING, machine.current_state) |
| 202 |
self.assertRaises(excp.NotFound, machine.process_event, 'poke') |
| 203 |
|
| 204 |
last_state = machine.current_state |
| 205 |
cb, args, kwargs = reaction |
| 206 |
next_event = cb(last_state, machine.current_state, |
| 207 |
builder.START, *args, **kwargs) |
| 208 |
reaction, terminal = machine.process_event(next_event) |
| 209 |
self.assertFalse(terminal) |
| 210 |
self.assertIsNotNone(reaction) |
| 211 |
self.assertEqual(st.SCHEDULING, machine.current_state) |
| 212 |
self.assertRaises(excp.NotFound, machine.process_event, 'poke') |
| 213 |
|
| 214 |
last_state = machine.current_state |
| 215 |
cb, args, kwargs = reaction |
| 216 |
next_event = cb(last_state, machine.current_state, |
| 217 |
next_event, *args, **kwargs) |
| 218 |
reaction, terminal = machine.process_event(next_event) |
| 219 |
self.assertFalse(terminal) |
| 220 |
self.assertEqual(st.WAITING, machine.current_state) |
| 221 |
self.assertRaises(excp.NotFound, machine.process_event, 'poke') |
| 222 |
|
| 223 |
# Should now be running... |
| 224 |
self.assertEqual(st.RUNNING, |
| 225 |
runtime.storage.get_atom_state(tasks[0].name)) |
| 226 |
|
| 227 |
last_state = machine.current_state |
| 228 |
cb, args, kwargs = reaction |
| 229 |
next_event = cb(last_state, machine.current_state, |
| 230 |
next_event, *args, **kwargs) |
| 231 |
reaction, terminal = machine.process_event(next_event) |
| 232 |
self.assertFalse(terminal) |
| 233 |
self.assertIsNotNone(reaction) |
| 234 |
self.assertEqual(st.ANALYZING, machine.current_state) |
| 235 |
self.assertRaises(excp.NotFound, machine.process_event, 'poke') |
| 236 |
|
| 237 |
last_state = machine.current_state |
| 238 |
cb, args, kwargs = reaction |
| 239 |
next_event = cb(last_state, machine.current_state, |
| 240 |
next_event, *args, **kwargs) |
| 241 |
reaction, terminal = machine.process_event(next_event) |
| 242 |
self.assertFalse(terminal) |
| 243 |
self.assertEqual(builder.GAME_OVER, machine.current_state) |
| 244 |
|
| 245 |
# Should now be done... |
| 246 |
self.assertEqual(st.SUCCESS, |
| 247 |
runtime.storage.get_atom_state(tasks[0].name)) |
| 248 |
|
| 249 |
def test_builder_automatic_process(self): |
| 250 |
flow = lf.Flow("root") |
| 251 |
tasks = test_utils.make_many( |
| 252 |
1, task_cls=test_utils.TaskNoRequiresNoReturns) |
| 253 |
flow.add(*tasks) |
| 254 |
|
| 255 |
runtime, machine, memory, machine_runner = self._make_machine( |
| 256 |
flow, initial_state=st.RUNNING) |
| 257 |
|
| 258 |
transitions = list(machine_runner.run_iter(builder.START)) |
| 259 |
self.assertEqual((builder.UNDEFINED, st.RESUMING), transitions[0]) |
| 260 |
self.assertEqual((builder.GAME_OVER, st.SUCCESS), transitions[-1]) |
| 261 |
self.assertEqual(st.SUCCESS, |
| 262 |
runtime.storage.get_atom_state(tasks[0].name)) |
| 263 |
|
| 264 |
def test_builder_automatic_process_failure(self): |
| 265 |
flow = lf.Flow("root") |
| 266 |
tasks = test_utils.make_many(1, task_cls=test_utils.NastyFailingTask) |
| 267 |
flow.add(*tasks) |
| 268 |
|
| 269 |
runtime, machine, memory, machine_runner = self._make_machine( |
| 270 |
flow, initial_state=st.RUNNING) |
| 271 |
|
| 272 |
transitions = list(machine_runner.run_iter(builder.START)) |
| 273 |
self.assertEqual((builder.GAME_OVER, st.FAILURE), transitions[-1]) |
| 274 |
self.assertEqual(1, len(memory.failures)) |
| 275 |
|
| 276 |
def test_builder_automatic_process_reverted(self): |
| 277 |
flow = lf.Flow("root") |
| 278 |
tasks = test_utils.make_many(1, task_cls=test_utils.TaskWithFailure) |
| 279 |
flow.add(*tasks) |
| 280 |
|
| 281 |
runtime, machine, memory, machine_runner = self._make_machine( |
| 282 |
flow, initial_state=st.RUNNING) |
| 283 |
|
| 284 |
transitions = list(machine_runner.run_iter(builder.START)) |
| 285 |
self.assertEqual((builder.GAME_OVER, st.REVERTED), transitions[-1]) |
| 286 |
self.assertEqual(st.REVERTED, |
| 287 |
runtime.storage.get_atom_state(tasks[0].name)) |
| 288 |
|
| 289 |
def test_builder_expected_transition_occurrences(self): |
| 290 |
flow = lf.Flow("root") |
| 291 |
tasks = test_utils.make_many( |
| 292 |
10, task_cls=test_utils.TaskNoRequiresNoReturns) |
| 293 |
flow.add(*tasks) |
| 294 |
|
| 295 |
runtime, machine, memory, machine_runner = self._make_machine( |
| 296 |
flow, initial_state=st.RUNNING) |
| 297 |
transitions = list(machine_runner.run_iter(builder.START)) |
| 298 |
|
| 299 |
occurrences = {t: transitions.count(t) for t in transitions} |
| 300 |
self.assertEqual(10, occurrences.get((st.SCHEDULING, st.WAITING))) |
| 301 |
self.assertEqual(10, occurrences.get((st.WAITING, st.ANALYZING))) |
| 302 |
self.assertEqual(9, occurrences.get((st.ANALYZING, st.SCHEDULING))) |
| 303 |
self.assertEqual(1, occurrences.get((builder.GAME_OVER, st.SUCCESS))) |
| 304 |
self.assertEqual(1, occurrences.get((builder.UNDEFINED, st.RESUMING))) |
| 305 |
|
| 306 |
self.assertEqual(0, len(memory.next_up)) |
| 307 |
self.assertEqual(0, len(memory.not_done)) |
| 308 |
self.assertEqual(0, len(memory.failures)) |