File: test_pipeline.py

package info (click to toggle)
python-taskflow 5.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,564 kB
  • sloc: python: 28,241; sh: 269; makefile: 24
file content (102 lines) | stat: -rw-r--r-- 3,812 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-

#    Copyright (C) 2014 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.

import futurist
from futurist import waiters
from oslo_utils import uuidutils

from taskflow.engines.action_engine import executor as base_executor
from taskflow.engines.worker_based import endpoint
from taskflow.engines.worker_based import executor as worker_executor
from taskflow.engines.worker_based import server as worker_server
from taskflow import test
from taskflow.tests import utils as test_utils
from taskflow.types import failure
from taskflow.utils import threading_utils


TEST_EXCHANGE, TEST_TOPIC = ('test-exchange', 'test-topic')
WAIT_TIMEOUT = 1.0
POLLING_INTERVAL = 0.01


class TestPipeline(test.TestCase):
    def _fetch_server(self, task_classes):
        endpoints = []
        for cls in task_classes:
            endpoints.append(endpoint.Endpoint(cls))
        server = worker_server.Server(
            TEST_TOPIC, TEST_EXCHANGE,
            futurist.ThreadPoolExecutor(max_workers=1), endpoints,
            transport='memory',
            transport_options={
                'polling_interval': POLLING_INTERVAL,
            })
        server_thread = threading_utils.daemon_thread(server.start)
        return (server, server_thread)

    def _fetch_executor(self):
        executor = worker_executor.WorkerTaskExecutor(
            uuidutils.generate_uuid(),
            TEST_EXCHANGE,
            [TEST_TOPIC],
            transport='memory',
            transport_options={
                'polling_interval': POLLING_INTERVAL,
            })
        return executor

    def _start_components(self, task_classes):
        server, server_thread = self._fetch_server(task_classes)
        executor = self._fetch_executor()
        self.addCleanup(executor.stop)
        self.addCleanup(server_thread.join)
        self.addCleanup(server.stop)
        executor.start()
        server_thread.start()
        server.wait()
        return (executor, server)

    def test_execution_pipeline(self):
        executor, server = self._start_components([test_utils.TaskOneReturn])
        self.assertEqual(0, executor.wait_for_workers(timeout=WAIT_TIMEOUT))

        t = test_utils.TaskOneReturn()
        progress_callback = lambda *args, **kwargs: None
        f = executor.execute_task(t, uuidutils.generate_uuid(), {},
                                  progress_callback=progress_callback)
        waiters.wait_for_any([f])

        event, result = f.result()
        self.assertEqual(1, result)
        self.assertEqual(base_executor.EXECUTED, event)

    def test_execution_failure_pipeline(self):
        task_classes = [
            test_utils.TaskWithFailure,
        ]
        executor, server = self._start_components(task_classes)

        t = test_utils.TaskWithFailure()
        progress_callback = lambda *args, **kwargs: None
        f = executor.execute_task(t, uuidutils.generate_uuid(), {},
                                  progress_callback=progress_callback)
        waiters.wait_for_any([f])

        action, result = f.result()
        self.assertIsInstance(result, failure.Failure)
        self.assertEqual(RuntimeError, result.check(RuntimeError))
        self.assertEqual(base_executor.EXECUTED, action)