|
7 | 7 | import uuid |
8 | 8 | from unittest.mock import Mock, patch |
9 | 9 |
|
| 10 | +import coolname |
10 | 11 | from jsonschema.validators import RefResolver |
11 | 12 |
|
12 | 13 | import twined |
@@ -560,33 +561,40 @@ def app(analysis): |
560 | 561 | "Error in <MockService('octue/yet-another-child:latest')>: Deliberately raised for testing.", |
561 | 562 | ) |
562 | 563 |
|
563 | | - def test_set_up_periodic_monitor_message(self): |
564 | | - """Test that periodic monitor messages can be set up from an analysis and that the `create_monitor_message` |
565 | | - callable returns new data each time. |
566 | | - """ |
| 564 | + def test_set_up_periodic_monitor_messages(self): |
| 565 | + """Test that multilple periodic monitor messages can be set up from an analysis.""" |
567 | 566 | monitor_messages = [] |
568 | 567 |
|
569 | 568 | def app(analysis): |
570 | | - create_monitor_message = lambda: {"random_integer": random.randint(0, 10000)} |
571 | | - analysis.set_up_periodic_monitor_message(create_monitor_message=create_monitor_message, period=0.05) |
| 569 | + analysis.set_up_periodic_monitor_message( |
| 570 | + create_monitor_message=lambda: {"random_integer": random.randint(0, 10000)}, |
| 571 | + period=0.05, |
| 572 | + ) |
| 573 | + |
| 574 | + analysis.set_up_periodic_monitor_message( |
| 575 | + create_monitor_message=lambda: {"random_word": coolname.generate_slug(2)}, |
| 576 | + period=0.01, |
| 577 | + ) |
| 578 | + |
572 | 579 | time.sleep(0.5) |
573 | 580 | analysis.output_values = {"The": "output"} |
574 | 581 |
|
575 | | - runner = Runner( |
576 | | - app_src=app, |
577 | | - twine={"monitor_message_schema": {"random_integer": {"type": "integer"}}, "output_values_schema": {}}, |
578 | | - ) |
579 | | - |
| 582 | + runner = Runner(app_src=app, twine={"monitor_message_schema": {}, "output_values_schema": {}}) |
580 | 583 | analysis = runner.run(handle_monitor_message=monitor_messages.append) |
581 | 584 | self.assertEqual(analysis.output_values, {"The": "output"}) |
582 | 585 |
|
583 | 586 | # Check that messages have been sent and that the data is different each time. |
584 | 587 | self.assertTrue(len(monitor_messages) > 2) |
585 | | - self.assertTrue(monitor_messages[0]["random_integer"] != monitor_messages[1]["random_integer"]) |
| 588 | + |
| 589 | + # Check that both types of monitor message were sent. |
| 590 | + monitor_message_types = {list(message.keys())[0] for message in monitor_messages} |
| 591 | + self.assertEqual(monitor_message_types, {"random_word", "random_integer"}) |
586 | 592 |
|
587 | 593 | # Check that the periodic monitor message thread has been stopped. |
588 | 594 | time.sleep(0.5) |
589 | | - self.assertFalse(analysis._periodic_monitor_message_sender_threads[0].is_alive()) |
| 595 | + |
| 596 | + for thread in analysis._periodic_monitor_message_sender_threads: |
| 597 | + self.assertFalse(thread.is_alive()) |
590 | 598 |
|
591 | 599 |
|
592 | 600 | class TestRunnerWithRequiredDatasetFileTags(BaseTestCase): |
|
0 commit comments