From e7ca792f2287725ec8e2f71e2ef30b2e908b8ac0 Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Thu, 8 Jul 2021 17:02:52 +0200
Subject: [PATCH] Added test cases for EngineExecutor. #78

---
 tests/test.py | 111 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 109 insertions(+), 2 deletions(-)

diff --git a/tests/test.py b/tests/test.py
index 5ba891f8..6ef286a9 100644
--- a/tests/test.py
+++ b/tests/test.py
@@ -96,13 +96,13 @@ class TestEngineExecutor(unittest.TestCase):
 
 
     def test_single_executor(self):
-        # Initialize stae and executor params
+        # Initialize state and executor params
         global_state = ["none"]
         due_time = SU.timestamp() + 2
         def f(param):
             global_state[0] = param
 
-        # Before the executor is done ther should be the initial value
+        # 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])
@@ -113,6 +113,113 @@ class TestEngineExecutor(unittest.TestCase):
 
 
 
+    def test_two_executors(self):
+        # Initialize state and executor params
+        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() + 4
+        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() + 2
+        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 1 second there still should be the initial value
+        time.sleep(1)
+        self.assertEqual("none", global_state[0])
+
+        # After 3 seconds max there should be the updated value from executor 2
+        time.sleep(2)
+        self.assertEqual("hello world from executor 2", global_state[0])
+
+        # After 5 seconds max there should be the updated value from executor 1
+        time.sleep(5)
+        self.assertEqual("hello world from executor 1", global_state[0])
+
+
+
+    def test_parent_child_executors_in_order(self):
+        # Initialize state and executor params
+        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() + 1
+        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() + 3
+        child = EngineExecutor("EXECUTOR_CHILD", parent, due_time2, f, "hello world from child")
+        self.assertEqual("none", global_state[0])
+
+        # After 0.5 seconds there still should be the initial value
+        time.sleep(0.5)
+        self.assertEqual("none", global_state[0])
+
+        # After 2 seconds max there should be the updated value from parent executor
+        time.sleep(2)
+        self.assertEqual("hello world from parent", global_state[0])
+
+        # After 4 seconds max there should be the updated value from child executor
+        time.sleep(4)
+        self.assertEqual("hello world from child", global_state[0])
+
+
+
+    def test_parent_child_executors_with_child_before(self):
+        # Initialize state and executor params
+        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() + 3
+        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.5 seconds there still should be the initial value
+        time.sleep(0.5)
+        self.assertEqual("none", global_state[0])
+
+        # After 2 seconds max there isn't a setting from the child yet, because it's waiting for the parent
+        time.sleep(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 4 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(4)
+        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])
+
+
 
 if __name__ == '__main__':
     unittest.main()
\ No newline at end of file
-- 
GitLab