|
@@ -316,25 +316,153 @@ class TestBehaviours(BaseTest):
|
316
|
316
|
|
317
|
317
|
|
318
|
318
|
class TestMechanisms(BaseTest):
|
319
|
|
- def test_mechanism_called_once_for_multiple_subject_behaviors(self):
|
|
319
|
+ def test_mechanism_called_once_for_multiple_subject_behaviors(
|
|
320
|
+ self,
|
|
321
|
+ do_nothing_process_manager: ProcessManager,
|
|
322
|
+ ):
|
320
|
323
|
shared.reset()
|
|
324
|
+ called = 0
|
|
325
|
+ global called
|
321
|
326
|
|
322
|
|
- pass
|
|
327
|
+ class MySubjectMechanism(SubjectMechanism):
|
|
328
|
+ def run(self):
|
|
329
|
+ global called
|
|
330
|
+ called += 1
|
|
331
|
+ return {'foo': 42}
|
|
332
|
+
|
|
333
|
+ class MySubjectBehaviour1(SubjectBehaviour):
|
|
334
|
+ use = [MySubjectMechanism]
|
323
|
335
|
|
324
|
|
- def test_mechanism_called_once_for_multiple_simulation_behaviors(self):
|
|
336
|
+ def run(self, data):
|
|
337
|
+ return {'bar': data[MySubjectMechanism]['foo'] + 100}
|
|
338
|
+
|
|
339
|
+ class MySubjectBehaviour2(SubjectBehaviour):
|
|
340
|
+ use = [MySubjectMechanism]
|
|
341
|
+
|
|
342
|
+ def run(self, data):
|
|
343
|
+ return {'bar': data[MySubjectMechanism]['foo'] + 100}
|
|
344
|
+
|
|
345
|
+ class MySubject(Subject):
|
|
346
|
+ behaviours_classes = [MySubjectBehaviour1, MySubjectBehaviour2]
|
|
347
|
+
|
|
348
|
+ simulation = Simulation(config)
|
|
349
|
+ my_subject = MySubject(config, simulation)
|
|
350
|
+ subjects = Subjects(simulation=simulation)
|
|
351
|
+ subjects.append(my_subject)
|
|
352
|
+ simulation.subjects = subjects
|
|
353
|
+
|
|
354
|
+ cycle_manager = CycleManager(
|
|
355
|
+ config,
|
|
356
|
+ logger,
|
|
357
|
+ simulation=simulation,
|
|
358
|
+ process_manager=do_nothing_process_manager,
|
|
359
|
+ )
|
|
360
|
+ cycle_manager._job_subjects(worker_id=0, process_count=1)
|
|
361
|
+ assert called == 1
|
|
362
|
+
|
|
363
|
+ def test_mechanism_called_once_for_multiple_simulation_behaviors(
|
|
364
|
+ self,
|
|
365
|
+ do_nothing_process_manager: ProcessManager,
|
|
366
|
+ ):
|
325
|
367
|
shared.reset()
|
|
368
|
+ called = 0
|
|
369
|
+ global called
|
326
|
370
|
|
327
|
|
- pass
|
|
371
|
+ class MySimulationMechanism(SimulationMechanism):
|
|
372
|
+ def run(self, process_number: int = None, process_count: int = None):
|
|
373
|
+ global called
|
|
374
|
+ called += 1
|
|
375
|
+ return {'foo': 42}
|
|
376
|
+
|
|
377
|
+ class MySimulationBehaviour1(SimulationBehaviour):
|
|
378
|
+ use = [MySimulationMechanism]
|
|
379
|
+
|
|
380
|
+ def run(self, data):
|
|
381
|
+ return {'bar': data[MySimulationMechanism]['foo'] + 100}
|
|
382
|
+
|
|
383
|
+ class MySimulationBehaviour2(SimulationBehaviour):
|
|
384
|
+ use = [MySimulationMechanism]
|
|
385
|
+
|
|
386
|
+ def run(self, data):
|
|
387
|
+ return {'bar': data[MySimulationMechanism]['foo'] + 100}
|
|
388
|
+
|
|
389
|
+ class MySimulation(Simulation):
|
|
390
|
+ behaviours_classes = [MySimulationBehaviour1, MySimulationBehaviour2]
|
|
391
|
+
|
|
392
|
+ simulation = MySimulation(config)
|
|
393
|
+ subjects = Subjects(simulation=simulation)
|
|
394
|
+ simulation.subjects = subjects
|
|
395
|
+
|
|
396
|
+ cycle_manager = CycleManager(
|
|
397
|
+ config,
|
|
398
|
+ logger,
|
|
399
|
+ simulation=simulation,
|
|
400
|
+ process_manager=do_nothing_process_manager,
|
|
401
|
+ )
|
|
402
|
+ cycle_manager._job_simulation(worker_id=0, process_count=1)
|
|
403
|
+ assert called == 1
|
328
|
404
|
|
329
|
|
- def test_mechanism_not_called_if_no_subject_behavior(self):
|
|
405
|
+ def test_mechanism_not_called_if_no_subject_behavior(
|
|
406
|
+ self,
|
|
407
|
+ do_nothing_process_manager: ProcessManager,
|
|
408
|
+ ):
|
330
|
409
|
shared.reset()
|
|
410
|
+ called = 0
|
|
411
|
+ global called
|
331
|
412
|
|
332
|
|
- pass
|
|
413
|
+ class MySubjectMechanism(SubjectMechanism):
|
|
414
|
+ def run(self):
|
|
415
|
+ global called
|
|
416
|
+ called += 1
|
|
417
|
+ return {'foo': 42}
|
333
|
418
|
|
334
|
|
- def test_mechanism_not_called_if_no_simulation_behavior(self):
|
|
419
|
+ class MySubject(Subject):
|
|
420
|
+ behaviours_classes = []
|
|
421
|
+
|
|
422
|
+ simulation = Simulation(config)
|
|
423
|
+ my_subject = MySubject(config, simulation)
|
|
424
|
+ subjects = Subjects(simulation=simulation)
|
|
425
|
+ subjects.append(my_subject)
|
|
426
|
+ simulation.subjects = subjects
|
|
427
|
+
|
|
428
|
+ cycle_manager = CycleManager(
|
|
429
|
+ config,
|
|
430
|
+ logger,
|
|
431
|
+ simulation=simulation,
|
|
432
|
+ process_manager=do_nothing_process_manager,
|
|
433
|
+ )
|
|
434
|
+ cycle_manager._job_subjects(worker_id=0, process_count=1)
|
|
435
|
+ assert called == 0
|
|
436
|
+
|
|
437
|
+ def test_mechanism_not_called_if_no_simulation_behavior(
|
|
438
|
+ self,
|
|
439
|
+ do_nothing_process_manager: ProcessManager,
|
|
440
|
+ ):
|
335
|
441
|
shared.reset()
|
|
442
|
+ called = 0
|
|
443
|
+ global called
|
336
|
444
|
|
337
|
|
- pass
|
|
445
|
+ class MySimulationMechanism(SimulationMechanism):
|
|
446
|
+ def run(self, process_number: int = None, process_count: int = None):
|
|
447
|
+ global called
|
|
448
|
+ called += 1
|
|
449
|
+ return {'foo': 42}
|
|
450
|
+
|
|
451
|
+ class MySimulation(Simulation):
|
|
452
|
+ pass
|
|
453
|
+
|
|
454
|
+ simulation = MySimulation(config)
|
|
455
|
+ subjects = Subjects(simulation=simulation)
|
|
456
|
+ simulation.subjects = subjects
|
|
457
|
+
|
|
458
|
+ cycle_manager = CycleManager(
|
|
459
|
+ config,
|
|
460
|
+ logger,
|
|
461
|
+ simulation=simulation,
|
|
462
|
+ process_manager=do_nothing_process_manager,
|
|
463
|
+ )
|
|
464
|
+ cycle_manager._job_simulation(worker_id=0, process_count=1)
|
|
465
|
+ assert called == 0
|
338
|
466
|
|
339
|
467
|
def test_mechanism_not_called_if_subject_behavior_timebase_not_active_yet(self):
|
340
|
468
|
shared.reset()
|