Debian Codegraph @

taskflow/tests/unit/action_engine/test_creation.py

raw
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 import futurist
16 import testtools
17
18 from taskflow.engines.action_engine import engine
19 from taskflow.engines.action_engine import executor
20 from taskflow.patterns import linear_flow as lf
21 from taskflow.persistence import backends
22 from taskflow import test
23 from taskflow.tests import utils
24 from taskflow.utils import eventlet_utils as eu
25 from taskflow.utils import persistence_utils as pu
26
27
28 class ParallelCreationTest(test.TestCase):
29 @staticmethod
30 def _create_engine(**kwargs):
31 flow = lf.Flow('test-flow').add(utils.DummyTask())
32 backend = backends.fetch({'connection': 'memory'})
33 flow_detail = pu.create_flow_detail(flow, backend=backend)
34 options = kwargs.copy()
35 return engine.ParallelActionEngine(flow, flow_detail,
36 backend, options)
37
38 def test_thread_string_creation(self):
39 for s in ['threads', 'threaded', 'thread']:
40 eng = self._create_engine(executor=s)
41 self.assertIsInstance(eng._task_executor,
42 executor.ParallelThreadTaskExecutor)
43
44 def test_thread_executor_creation(self):
45 with futurist.ThreadPoolExecutor(1) as e:
46 eng = self._create_engine(executor=e)
47 self.assertIsInstance(eng._task_executor,
48 executor.ParallelThreadTaskExecutor)
49
50 @testtools.skipIf(not eu.EVENTLET_AVAILABLE, 'eventlet is not available')
51 def test_green_executor_creation(self):
52 with futurist.GreenThreadPoolExecutor(1) as e:
53 eng = self._create_engine(executor=e)
54 self.assertIsInstance(eng._task_executor,
55 executor.ParallelThreadTaskExecutor)
56
57 def test_sync_executor_creation(self):
58 with futurist.SynchronousExecutor() as e:
59 eng = self._create_engine(executor=e)
60 self.assertIsInstance(eng._task_executor,
61 executor.ParallelThreadTaskExecutor)
62
63 def test_invalid_creation(self):
64 self.assertRaises(ValueError, self._create_engine, executor='crap')
65 self.assertRaises(TypeError, self._create_engine, executor=2)
66 self.assertRaises(TypeError, self._create_engine, executor=object())