|
@@ -1,23 +1,351 @@
|
|
1
|
+import datetime
|
|
2
|
+import time
|
|
3
|
+
|
|
4
|
+from synergine2.config import Config
|
|
5
|
+from synergine2.cycle import CycleManager
|
|
6
|
+from synergine2.log import SynergineLogger
|
|
7
|
+from synergine2.processing import ProcessManager
|
|
8
|
+from synergine2.share import shared
|
|
9
|
+from synergine2.simulation import Simulation
|
|
10
|
+from synergine2.simulation import Subjects
|
|
11
|
+from synergine2.simulation import SubjectBehaviour
|
|
12
|
+from synergine2.simulation import SubjectMechanism
|
|
13
|
+from synergine2.simulation import Subject
|
|
14
|
+from synergine2.simulation import SimulationMechanism
|
|
15
|
+from synergine2.simulation import SimulationBehaviour
|
1
|
16
|
from tests import BaseTest
|
2
|
17
|
|
|
18
|
+from freezegun import freeze_time
|
|
19
|
+
|
|
20
|
+config = Config()
|
|
21
|
+logger = SynergineLogger('test')
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+class MySubjectMechanism(SubjectMechanism):
|
|
25
|
+ def run(self):
|
|
26
|
+ return {'foo': 42}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+class MySimulationMechanism(SimulationMechanism):
|
|
30
|
+ def run(self, process_number: int=None, process_count: int=None):
|
|
31
|
+ return {'foo': 42}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+class MySubjectBehaviour(SubjectBehaviour):
|
|
35
|
+ use = [MySubjectMechanism]
|
|
36
|
+
|
|
37
|
+ def run(self, data):
|
|
38
|
+ return {'bar': data[MySubjectMechanism]['foo'] + 100}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+class MySimulationBehaviour(SimulationBehaviour):
|
|
42
|
+ use = [MySimulationMechanism]
|
|
43
|
+
|
|
44
|
+ def run(self, data):
|
|
45
|
+ return {'bar': data[MySimulationMechanism]['foo'] + 100}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+class MySimulation(Simulation):
|
|
49
|
+ behaviours_classes = [MySimulationBehaviour]
|
|
50
|
+
|
3
|
51
|
|
4
|
52
|
class TestBehaviours(BaseTest):
|
5
|
|
- def test_behaviour_produce_data(self):
|
|
53
|
+ def test_subject_behaviour_produce_data(
|
|
54
|
+ self,
|
|
55
|
+ do_nothing_process_manager: ProcessManager,
|
|
56
|
+ ):
|
|
57
|
+ shared.reset()
|
|
58
|
+
|
|
59
|
+ class MySubject(Subject):
|
|
60
|
+ behaviours_classes = [MySubjectBehaviour]
|
|
61
|
+
|
|
62
|
+ simulation = Simulation(config)
|
|
63
|
+ my_subject = MySubject(config, simulation)
|
|
64
|
+ subjects = Subjects(simulation=simulation)
|
|
65
|
+ subjects.append(my_subject)
|
|
66
|
+ simulation.subjects = subjects
|
|
67
|
+
|
|
68
|
+ cycle_manager = CycleManager(
|
|
69
|
+ config,
|
|
70
|
+ logger,
|
|
71
|
+ simulation=simulation,
|
|
72
|
+ process_manager=do_nothing_process_manager,
|
|
73
|
+ )
|
|
74
|
+ results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
|
|
75
|
+ assert results_by_subjects
|
|
76
|
+ assert id(my_subject) in results_by_subjects
|
|
77
|
+ assert MySubjectBehaviour in results_by_subjects[id(my_subject)]
|
|
78
|
+ assert 'bar' in results_by_subjects[id(my_subject)][MySubjectBehaviour]
|
|
79
|
+ assert 142 == results_by_subjects[id(my_subject)][MySubjectBehaviour]['bar']
|
|
80
|
+
|
|
81
|
+ def test_simulation_behaviour_produce_data(
|
|
82
|
+ self,
|
|
83
|
+ do_nothing_process_manager: ProcessManager,
|
|
84
|
+ ):
|
|
85
|
+ shared.reset()
|
|
86
|
+ simulation = MySimulation(config)
|
|
87
|
+ subjects = Subjects(simulation=simulation)
|
|
88
|
+ simulation.subjects = subjects
|
|
89
|
+
|
|
90
|
+ cycle_manager = CycleManager(
|
|
91
|
+ config,
|
|
92
|
+ logger,
|
|
93
|
+ simulation=simulation,
|
|
94
|
+ process_manager=do_nothing_process_manager,
|
|
95
|
+ )
|
|
96
|
+ data = cycle_manager._job_simulation(worker_id=0, process_count=1)
|
|
97
|
+ assert data
|
|
98
|
+ assert MySimulationBehaviour in data
|
|
99
|
+ assert 'bar' in data[MySimulationBehaviour]
|
|
100
|
+ assert 142 == data[MySimulationBehaviour]['bar']
|
|
101
|
+
|
|
102
|
+ def test_subject_behaviour_cycle_frequency(
|
|
103
|
+ self,
|
|
104
|
+ do_nothing_process_manager: ProcessManager,
|
|
105
|
+ ):
|
|
106
|
+ shared.reset()
|
|
107
|
+
|
|
108
|
+ class MyCycledSubjectBehaviour(MySubjectBehaviour):
|
|
109
|
+ @property
|
|
110
|
+ def cycle_frequency(self):
|
|
111
|
+ return 2
|
|
112
|
+
|
|
113
|
+ class MySubject(Subject):
|
|
114
|
+ behaviours_classes = [MyCycledSubjectBehaviour]
|
|
115
|
+
|
|
116
|
+ simulation = Simulation(config)
|
|
117
|
+ my_subject = MySubject(config, simulation)
|
|
118
|
+ subjects = Subjects(simulation=simulation)
|
|
119
|
+ subjects.append(my_subject)
|
|
120
|
+ simulation.subjects = subjects
|
|
121
|
+
|
|
122
|
+ cycle_manager = CycleManager(
|
|
123
|
+ config,
|
|
124
|
+ logger,
|
|
125
|
+ simulation=simulation,
|
|
126
|
+ process_manager=do_nothing_process_manager,
|
|
127
|
+ )
|
|
128
|
+
|
|
129
|
+ # Cycle 0: behaviour NOT executed
|
|
130
|
+ cycle_manager.current_cycle = 0
|
|
131
|
+ results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
|
|
132
|
+ assert results_by_subjects
|
|
133
|
+ assert id(my_subject) in results_by_subjects
|
|
134
|
+ assert not results_by_subjects[id(my_subject)]
|
|
135
|
+
|
|
136
|
+ # Cycle 1: behaviour executed
|
|
137
|
+ cycle_manager.current_cycle = 1
|
|
138
|
+ results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
|
|
139
|
+ assert results_by_subjects
|
|
140
|
+ assert id(my_subject) in results_by_subjects
|
|
141
|
+ assert results_by_subjects[id(my_subject)]
|
|
142
|
+
|
|
143
|
+ def test_subject_behaviour_seconds_frequency(
|
|
144
|
+ self,
|
|
145
|
+ do_nothing_process_manager: ProcessManager,
|
|
146
|
+ ):
|
|
147
|
+ shared.reset()
|
|
148
|
+
|
|
149
|
+ class MyTimedSubjectBehaviour(MySubjectBehaviour):
|
|
150
|
+ @property
|
|
151
|
+ def seconds_frequency(self):
|
|
152
|
+ return 1.0
|
|
153
|
+
|
|
154
|
+ def run(self, data):
|
|
155
|
+ self.last_execution_time = time.time()
|
|
156
|
+ return super().run(data)
|
|
157
|
+
|
|
158
|
+ class MySubject(Subject):
|
|
159
|
+ behaviours_classes = [MyTimedSubjectBehaviour]
|
|
160
|
+
|
|
161
|
+ simulation = Simulation(config)
|
|
162
|
+ my_subject = MySubject(config, simulation)
|
|
163
|
+ subjects = Subjects(simulation=simulation)
|
|
164
|
+ subjects.append(my_subject)
|
|
165
|
+ simulation.subjects = subjects
|
|
166
|
+
|
|
167
|
+ cycle_manager = CycleManager(
|
|
168
|
+ config,
|
|
169
|
+ logger,
|
|
170
|
+ simulation=simulation,
|
|
171
|
+ process_manager=do_nothing_process_manager,
|
|
172
|
+ )
|
|
173
|
+
|
|
174
|
+ # Thirst time, behaviour IS executed
|
|
175
|
+ with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0)):
|
|
176
|
+ data = cycle_manager._job_subjects(worker_id=0, process_count=1)
|
|
177
|
+ assert data
|
|
178
|
+ assert id(my_subject) in data
|
|
179
|
+ assert data[id(my_subject)]
|
|
180
|
+
|
|
181
|
+ # Less second after: NOT executed
|
|
182
|
+ with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0, 500000)):
|
|
183
|
+ data = cycle_manager._job_subjects(worker_id=0, process_count=1)
|
|
184
|
+ assert data
|
|
185
|
+ assert id(my_subject) in data
|
|
186
|
+ assert not data[id(my_subject)]
|
|
187
|
+
|
|
188
|
+ # Less second after: NOT executed
|
|
189
|
+ with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0, 700000)):
|
|
190
|
+ data = cycle_manager._job_subjects(worker_id=0, process_count=1)
|
|
191
|
+ assert data
|
|
192
|
+ assert id(my_subject) in data
|
|
193
|
+ assert not data[id(my_subject)]
|
|
194
|
+
|
|
195
|
+ # Less second after: NOT executed
|
|
196
|
+ with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 1, 500000)):
|
|
197
|
+ data = cycle_manager._job_subjects(worker_id=0, process_count=1)
|
|
198
|
+ assert data
|
|
199
|
+ assert id(my_subject) in data
|
|
200
|
+ assert data[id(my_subject)]
|
|
201
|
+
|
|
202
|
+ def test_simulation_behaviour_cycle_frequency(
|
|
203
|
+ self,
|
|
204
|
+ do_nothing_process_manager: ProcessManager,
|
|
205
|
+ ):
|
|
206
|
+ shared.reset()
|
|
207
|
+
|
|
208
|
+ class MyCycledSimulationBehaviour(MySimulationBehaviour):
|
|
209
|
+ @property
|
|
210
|
+ def cycle_frequency(self):
|
|
211
|
+ return 2
|
|
212
|
+
|
|
213
|
+ class MyCycledSimulation(Simulation):
|
|
214
|
+ behaviours_classes = [MyCycledSimulationBehaviour]
|
|
215
|
+
|
|
216
|
+ simulation = MyCycledSimulation(config)
|
|
217
|
+ subjects = Subjects(simulation=simulation)
|
|
218
|
+ simulation.subjects = subjects
|
|
219
|
+
|
|
220
|
+ cycle_manager = CycleManager(
|
|
221
|
+ config,
|
|
222
|
+ logger,
|
|
223
|
+ simulation=simulation,
|
|
224
|
+ process_manager=do_nothing_process_manager,
|
|
225
|
+ )
|
|
226
|
+
|
|
227
|
+ # Cycle 0: behaviour NOT executed
|
|
228
|
+ cycle_manager.current_cycle = 0
|
|
229
|
+ data = cycle_manager._job_simulation(worker_id=0, process_count=1)
|
|
230
|
+ assert not data
|
|
231
|
+
|
|
232
|
+ # Cycle 1: behaviour executed
|
|
233
|
+ cycle_manager.current_cycle = 1
|
|
234
|
+ data = cycle_manager._job_simulation(worker_id=0, process_count=1)
|
|
235
|
+ assert data
|
|
236
|
+
|
|
237
|
+ def test_simulation_behaviour_seconds_frequency(
|
|
238
|
+ self,
|
|
239
|
+ do_nothing_process_manager: ProcessManager,
|
|
240
|
+ ):
|
|
241
|
+ shared.reset()
|
|
242
|
+
|
|
243
|
+ class MyTimedSimulationBehaviour(MySimulationBehaviour):
|
|
244
|
+ @property
|
|
245
|
+ def seconds_frequency(self):
|
|
246
|
+ return 1.0
|
|
247
|
+
|
|
248
|
+ def run(self, data):
|
|
249
|
+ self.last_execution_time = time.time()
|
|
250
|
+ return super().run(data)
|
|
251
|
+
|
|
252
|
+ class MyTimedSimulation(Simulation):
|
|
253
|
+ behaviours_classes = [MyTimedSimulationBehaviour]
|
|
254
|
+
|
|
255
|
+ simulation = MyTimedSimulation(config)
|
|
256
|
+ subjects = Subjects(simulation=simulation)
|
|
257
|
+ simulation.subjects = subjects
|
|
258
|
+
|
|
259
|
+ cycle_manager = CycleManager(
|
|
260
|
+ config,
|
|
261
|
+ logger,
|
|
262
|
+ simulation=simulation,
|
|
263
|
+ process_manager=do_nothing_process_manager,
|
|
264
|
+ )
|
|
265
|
+
|
|
266
|
+ # Thirst time, behaviour IS executed
|
|
267
|
+ with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0)):
|
|
268
|
+ data = cycle_manager._job_simulation(worker_id=0, process_count=1)
|
|
269
|
+ assert data
|
|
270
|
+
|
|
271
|
+ # Less second after: NOT executed
|
|
272
|
+ with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0, 500000)):
|
|
273
|
+ data = cycle_manager._job_simulation(worker_id=0, process_count=1)
|
|
274
|
+ assert not data
|
|
275
|
+
|
|
276
|
+ # Less second after: NOT executed
|
|
277
|
+ with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 0, 700000)):
|
|
278
|
+ data = cycle_manager._job_simulation(worker_id=0, process_count=1)
|
|
279
|
+ assert not data
|
|
280
|
+
|
|
281
|
+ # More second after: IS executed
|
|
282
|
+ with freeze_time(datetime.datetime(2000, 12, 1, 0, 0, 1, 500000)):
|
|
283
|
+ data = cycle_manager._job_simulation(worker_id=0, process_count=1)
|
|
284
|
+ assert data
|
|
285
|
+
|
|
286
|
+ def test_subject_behavior_not_called_if_no_more_subjects(
|
|
287
|
+ self,
|
|
288
|
+ do_nothing_process_manager: ProcessManager,
|
|
289
|
+ ):
|
|
290
|
+ shared.reset()
|
|
291
|
+
|
|
292
|
+ class MySubject(Subject):
|
|
293
|
+ behaviours_classes = [MySubjectBehaviour]
|
|
294
|
+
|
|
295
|
+ simulation = Simulation(config)
|
|
296
|
+ my_subject = MySubject(config, simulation)
|
|
297
|
+ subjects = Subjects(simulation=simulation)
|
|
298
|
+ subjects.append(my_subject)
|
|
299
|
+ simulation.subjects = subjects
|
|
300
|
+
|
|
301
|
+ cycle_manager = CycleManager(
|
|
302
|
+ config,
|
|
303
|
+ logger,
|
|
304
|
+ simulation=simulation,
|
|
305
|
+ process_manager=do_nothing_process_manager,
|
|
306
|
+ )
|
|
307
|
+ results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
|
|
308
|
+ assert results_by_subjects
|
|
309
|
+ assert id(my_subject) in results_by_subjects
|
|
310
|
+ assert results_by_subjects[id(my_subject)]
|
|
311
|
+
|
|
312
|
+ # If we remove subject, no more data generated
|
|
313
|
+ subjects.remove(my_subject)
|
|
314
|
+ results_by_subjects = cycle_manager._job_subjects(worker_id=0, process_count=1)
|
|
315
|
+ assert not results_by_subjects
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+class TestMechanisms(BaseTest):
|
|
319
|
+ def test_mechanism_called_once_for_multiple_subject_behaviors(self):
|
|
320
|
+ shared.reset()
|
|
321
|
+
|
6
|
322
|
pass
|
7
|
323
|
|
8
|
|
- def test_behaviour_timebase(self):
|
|
324
|
+ def test_mechanism_called_once_for_multiple_simulation_behaviors(self):
|
|
325
|
+ shared.reset()
|
|
326
|
+
|
9
|
327
|
pass
|
10
|
328
|
|
11
|
|
- def test_behavior_not_called_if_no_more_subjects(self):
|
|
329
|
+ def test_mechanism_not_called_if_no_subject_behavior(self):
|
|
330
|
+ shared.reset()
|
|
331
|
+
|
12
|
332
|
pass
|
13
|
333
|
|
|
334
|
+ def test_mechanism_not_called_if_no_simulation_behavior(self):
|
|
335
|
+ shared.reset()
|
14
|
336
|
|
15
|
|
-class TestMechanisms(BaseTest):
|
16
|
|
- def test_mechanism_called_once_for_multiple_behaviors(self):
|
17
|
337
|
pass
|
18
|
338
|
|
19
|
|
- def test_mechanism_not_called_if_no_behavior(self):
|
|
339
|
+ def test_mechanism_not_called_if_subject_behavior_timebase_not_active_yet(self):
|
|
340
|
+ shared.reset()
|
|
341
|
+
|
20
|
342
|
pass
|
21
|
343
|
|
22
|
|
- def test_mechanism_not_called_if_behavior_timebase_not_active_yet(self):
|
|
344
|
+ def test_mechanism_not_called_if_simulation_behavior_timebase_not_active_yet(self):
|
|
345
|
+ shared.reset()
|
|
346
|
+
|
23
|
347
|
pass
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+# TODO: Test Simulation mechanism parralelisation
|
|
351
|
+# TODO: Test behaviour actions generation
|