test_engine_executor.py 10.2 KB
Newer Older
David Trattnig's avatar
David Trattnig committed
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

#
# Aura Engine (https://gitlab.servus.at/aura/engine)
#
# Copyright (C) 2017-now() - The Aura Engine Team.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program 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 Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from src.engine import Engine
import unittest
import time

from src.base.utils import SimpleUtil as SU
from src.control import EngineExecutor




class TestEngineExecutor(unittest.TestCase):
    """
    Testing the EngineExecutor.
    """

    def setUp(self):
        None



    def test_single_executor(self):
        # Initialize state and executor params
        EngineExecutor.timer_store = {}
        global_state = ["none"]
        due_time = SU.timestamp() + 0.3
        def f(param):
            global_state[0] = param

        # Before the executor is done there should be the initial value
        e = EngineExecutor("RANDOM_NAMESPACE", None, due_time, f, "hello world")
        self.assertEqual("none", global_state[0])
        self.assertNotEqual("hello world", global_state[0])

        # After 0.5 seconds there should be the updated value
        time.sleep(0.5)
        self.assertEqual("hello world", global_state[0])



    def test_two_executors(self):
        # Initialize state and executor params
        EngineExecutor.timer_store = {}
        global_state = ["none"]
        def f(param):
            global_state[0] = param

        # Before the executor 1 is done there should be the initial value
        due_time1 = SU.timestamp() + 1
        e1 = EngineExecutor("EXECUTOR_1", None, due_time1, f, "hello world from executor 1")
        self.assertEqual("none", global_state[0])
        self.assertNotEqual("hello world from executor 1", global_state[0])

        # Before the executor 2 is done there should be still the initial value
        due_time2 = SU.timestamp() + 0.5
        e2 = EngineExecutor("EXECUTOR_2", None, due_time2, f, "hello world from executor 2")
        self.assertEqual("none", global_state[0])
        self.assertNotEqual("hello world from executor 2", global_state[0])

        # After 0.3 seconds there still should be the initial value
        time.sleep(0.3)
        self.assertEqual("none", global_state[0])

        # After 0.6 seconds max there should be the updated value from executor 2
        time.sleep(0.3)
        self.assertEqual("hello world from executor 2", global_state[0])

        # After 1.1 seconds max there should be the updated value from executor 1
        time.sleep(0.5)
        self.assertEqual("hello world from executor 1", global_state[0])



    def test_parent_child_executors_in_order(self):
        # Initialize state and executor params
        EngineExecutor.timer_store = {}
        global_state = ["none"]
        def f(param):
            global_state[0] = param

        # Before the the parent is done there should be the initial value
        due_time1 = SU.timestamp() + 0.5
        parent = EngineExecutor("EXECUTOR_PARENT", None, due_time1, f, "hello world from parent")
        self.assertEqual("none", global_state[0])

        # Before the the child is done there should be the initial value
        due_time2 = SU.timestamp() + 1
        child = EngineExecutor("EXECUTOR_CHILD", parent, due_time2, f, "hello world from child")
        self.assertEqual("none", global_state[0])

        # After 0.3 seconds there still should be the initial value
        time.sleep(0.3)
        self.assertEqual("none", global_state[0])

        # After 0.6 seconds max there should be the updated value from parent executor
        time.sleep(0.3)
        self.assertEqual("hello world from parent", global_state[0])

        # After 1.2 seconds max there should be the updated value from child executor
        time.sleep(1.2)
        self.assertEqual("hello world from child", global_state[0])



    def test_parent_child_executors_with_child_before(self):
        # Initialize state and executor params
        EngineExecutor.timer_store = {}
        global_state = ["none", "never called by parent"]
        def f(param):
            global_state[0] = param
            if param == "hello world from parent":
                global_state[1] = param


        # Before the the parent is done there should be the initial value
        due_time1 = SU.timestamp() + 0.5
        parent = EngineExecutor("EXECUTOR_PARENT", None, due_time1, f, "hello world from parent")
        self.assertEqual("none", global_state[0])

        # Before the the child is done there should be the initial value
        due_time2 = SU.timestamp() + 1.5
        child = EngineExecutor("EXECUTOR_CHILD", parent, due_time2, f, "hello world from child")
        self.assertEqual("none", global_state[0])

        # After 0.2 seconds there still should be the initial value
        time.sleep(0.2)
        self.assertEqual("none", global_state[0])

        # After 0.4 seconds max there isn't a setting from the child yet, because it's waiting for the parent
        time.sleep(0.2)
        self.assertNotEqual("hello world from child", global_state[0])

        # But the parent didn't set anything either, because it's scheduled for later
        self.assertNotEqual("hello world from parent", global_state[0])
        self.assertEqual("none", global_state[0])

        # Double check if it has ever been called by parent
        self.assertEqual("never called by parent", global_state[1])

        # After 2.2 seconds max there should be the updated value from parent & child
        # Because the child is due before the parent, it is executed right away,
        # hence overwriting the value just set by the parent
        time.sleep(2.2)
        self.assertNotEqual("hello world from parent", global_state[0])
        self.assertEqual("hello world from child", global_state[0])

        # But we do not just believe what we expect, but check if it really has ever been called by a parent
        self.assertEqual("hello world from parent", global_state[1])




    def test_timer_store_replacement(self):
        # Initialize state and executor params
        EngineExecutor.timer_store = {}
        global_state = ["none"]
        def f(param):
            global_state[0] = param

        # There should be a total of 0 timers
        timers = EngineExecutor.command_history()
        self.assertEqual(0, len(timers))

        # Before the the parent is done there should be the initial value
        due_time1 = SU.timestamp() + 0.5
        parent = EngineExecutor("EXECUTOR_PARENT", None, due_time1, f, "hello world from parent")
        self.assertEqual("none", global_state[0])

        # Before the the child is done there should be the initial value
        due_time2 = SU.timestamp() + 1.5
        child = EngineExecutor("EXECUTOR_CHILD", parent, due_time2, f, "hello world from child")
        self.assertEqual("none", global_state[0])

        # There should be a total of 2 timers
        timers = EngineExecutor.command_history()
        self.assertEqual(2, len(timers))

        # Replacing the parent with a new instance
        parent = EngineExecutor("EXECUTOR_PARENT", None, due_time1, f, "hello world from alternative parent")
        self.assertEqual("none", global_state[0])

        # Before the the child is done there should be the initial value
        child = EngineExecutor("EXECUTOR_CHILD", parent, due_time2, f, "hello world from alternative child")
        self.assertEqual("none", global_state[0])

        # After 1 seconds max there should be the updated value from the alternative parent
        time.sleep(1)
        self.assertEqual("hello world from alternative parent", global_state[0])

        # After 2 seconds max there should be the updated value from the alternative child
        time.sleep(2)
        self.assertEqual("hello world from alternative child", global_state[0])

        # There should be a total of 2 timers, even though 4 got instantiated
        timers = EngineExecutor.command_history()
        self.assertEqual(2, len(timers))




    def test_dead_parent_with_lively_child(self):
        # Initialize state and executor params
        EngineExecutor.timer_store = {}
        global_state = ["none"]
        def f(param):
            global_state[0] = param

        # Before the the parent is done there should be the initial value
        due_time1 = SU.timestamp() + 0.5
        parent = EngineExecutor("EXECUTOR_PARENT", None, due_time1, f, "hello world from parent")
        self.assertEqual("none", global_state[0])

        # Before the the child is done there should be the initial value
        due_time2 = SU.timestamp() + 1.5
        child = EngineExecutor("EXECUTOR_CHILD", parent, due_time2, f, "hello world from child")
        self.assertEqual("none", global_state[0])

        # Wait until the parent timer got executed
        time.sleep(1)
        self.assertEqual("hello world from parent", global_state[0])

        # Parent dead - child alive
        self.assertEqual(False, parent.is_alive())
        self.assertEqual(True, child.is_alive())

        # Replacing the parent & child with a new instance
        parent = EngineExecutor("EXECUTOR_PARENT", None, due_time1, f, "hello world from late parent")
        child = EngineExecutor("EXECUTOR_PARENT", None, due_time2, f, "hello world from alternative child")

        # New parent = dead before finished initialization already, so actually never born
        self.assertEqual(False, parent.is_alive())

        # Even though the late parent would be executed by now, it wasn't, because the initial parent
        # was finished at instatiation time already
        self.assertEqual("hello world from parent", global_state[0])

        # Finally there should be the updated value from the alternative child
        time.sleep(2)
        self.assertEqual("hello world from alternative child", global_state[0])




if __name__ == '__main__':
    unittest.main()