Welcome to mirror list, hosted at ThFree Co, Russian Federation.

parallel_helper_pipeline_tests.py « tests « helpers « pypeline « src - github.com/ianj-als/pypeline.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c16c89231ca5f4b7bfe19223f5118550b04d4214 (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#
# Copyright Applied Language Solutions 2012
#
# This file is part of Pypeline.
#
# Pypeline is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pypeline is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pypeline.  If not, see <http://www.gnu.org/licenses/>.
#

#
# Unit tests for building a pipeline using the helper functions.
# The pipeline is a mixture of components that are sub-processes and plain old
# Python functions.
#
import os
import subprocess
import sys
import unittest

from concurrent.futures import ThreadPoolExecutor
from pypeline.helpers.parallel_helpers import cons_function_component, \
     cons_wire, \
     cons_dictionary_wire, \
     cons_split_wire, \
     cons_unsplit_wire, \
     run_pipeline, \
     eval_pipeline, \
     exec_pipeline


class ParallelPypelineHelperUnitTest(unittest.TestCase):
     @staticmethod
     def test(no_workers, pipeline, input, state, run_function = run_pipeline):
          executor = ThreadPoolExecutor(max_workers = no_workers)
          result = run_function(executor, pipeline, input, state)
          executor.shutdown(True)
          return result


     def test_serial_pypeline_with_function_components(self):
          rev_msg_one = "reverse(1)"
          rev_msg_two = "reverse(2)"
          upper_msg = "upper"

          reverse_function = lambda a, s: a[::-1]
          upper_function = lambda a, s: a.upper()

          comp_rev_one = cons_function_component(reverse_function,
                                                 state_mutator = lambda s: s.append(rev_msg_one) or s)
          comp_rev_two = cons_function_component(reverse_function,
                                                 state_mutator = lambda s: s.append(rev_msg_two) or s)
          comp_upper = cons_function_component(upper_function,
                                               state_mutator = lambda s: s.append(upper_msg) or s)

          pipeline = comp_rev_one >> comp_rev_two >> comp_upper

          value = "hello world"
          target = (upper_function(value, None), [rev_msg_one, rev_msg_two, upper_msg])
          result = ParallelPypelineHelperUnitTest.test(2, pipeline, "hello world", list())

          self.assertEquals(target, result)


     def test_parallel_pypeline_with_split_and_unsplit_wires(self):
          rev_msg_one = "reverse(top)"
          rev_msg_two = "reverse(bottom)"
          top_msg = "top"
          bottom_msg = "bottom"

          reverse_func = lambda a, s: a[::-1]
          top_func = lambda a, s: " ".join([a, top_msg])
          bottom_func = lambda a, s: " ".join([a, bottom_msg])

          comp_rev_top = cons_function_component(reverse_func,
                                                 state_mutator = lambda s: s.append(rev_msg_one) or s)
          comp_rev_bottom = cons_function_component(reverse_func,
                                                    state_mutator = lambda s: s.append(rev_msg_two) or s)
          comp_para_top = cons_function_component(top_func,
                                                  state_mutator = lambda s: s.append(top_msg) or s)
          comp_para_bottom = cons_function_component(bottom_func,
                                                     state_mutator = lambda s: s.append(bottom_msg) or s)

          unsplit_func = lambda t, b: {'top': t, 'bottom': b}

          pipeline = (comp_rev_top & comp_rev_bottom) >> \
                     (comp_para_top ** comp_para_bottom) >> \
                     cons_unsplit_wire(unsplit_func)

          value = "hello world"
          target = (unsplit_func(top_func(reverse_func(value, None), None),
                                 bottom_func(reverse_func(value, None), None)),
                    [rev_msg_one, rev_msg_two, top_msg, bottom_msg])

          result = ParallelPypelineHelperUnitTest.test(2, pipeline, "hello world", list())
          
          self.assertEquals(target, result)


     def test_parallel_run_eval_and_exec(self):
          value = "hello world"
          state = 0

          input_msg = "input"
          output_msg = "output"

          input_func = lambda a, s: " ".join([input_msg, a])
          output_func = lambda a, s: " ".join([a, output_msg])
          function = lambda a, s: a.upper()
          composition = lambda a, s: output_func(function(input_func(a, s), s), s)
          state_func = lambda s: s + 1

          pipeline = cons_function_component(function,
                                             input_func,
                                             output_func,
                                             state_mutator = state_func)

          result = ParallelPypelineHelperUnitTest.test(1, pipeline, value, state, run_pipeline)
          target = (composition(value, state), state_func(state))
          self.assertEquals(target, result)

          result = ParallelPypelineHelperUnitTest.test(1, pipeline, value, state, eval_pipeline)
          target = composition(value, state)
          self.assertEquals(target, result)

          result = ParallelPypelineHelperUnitTest.test(1, pipeline, value, state, exec_pipeline)
          target = state_func(state)
          self.assertEquals(target, result)


     def test_parallel_wire(self):
          value = {'PI' : 3.141, 'E' : 2.718}
          pipeline = cons_wire(lambda a, s: {'pi' : a['PI'], 'e' : a['E']})
          result = ParallelPypelineHelperUnitTest.test(1, pipeline, value, None, eval_pipeline)
          self.assertEquals({'pi' : 3.141, 'e' : 2.718}, result)


     def test_parallel_dictionary_wire(self):
          value = {'pi' : 3.141, 'e' : 2.718}
          pipeline = cons_dictionary_wire({'pi' : 'PI', 'e' : 'E'})
          result = ParallelPypelineHelperUnitTest.test(1, pipeline, value, None, eval_pipeline)
          self.assertEquals({'PI' : 3.141, 'E' : 2.718}, result)