From aeac71f481695cf4677cb6b16e18bde33b7eb81c Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Fri, 21 Jan 2022 12:04:07 +0100
Subject: [PATCH] TMSS-671: added (skipped) test
 test_time_bound_unit_wins_even_at_lower_priority. To be fixed in a new
 dynsched ticket.

---
 .../scheduling/test/t_dynamic_scheduling.py   | 34 +++++++++----------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/SAS/TMSS/backend/services/scheduling/test/t_dynamic_scheduling.py b/SAS/TMSS/backend/services/scheduling/test/t_dynamic_scheduling.py
index fdb4dc28115..49abb0b8cb9 100755
--- a/SAS/TMSS/backend/services/scheduling/test/t_dynamic_scheduling.py
+++ b/SAS/TMSS/backend/services/scheduling/test/t_dynamic_scheduling.py
@@ -853,54 +853,54 @@ class TestDynamicScheduling(BaseDynamicSchedulingTestCase):
         self.assertGreaterEqual(scheduling_unit_blueprint_medium.scheduled_start_time - scheduling_unit_blueprint_high.scheduled_stop_time, DEFAULT_INTER_OBSERVATION_GAP)
         self.assertGreaterEqual(scheduling_unit_blueprint_low.scheduled_start_time - scheduling_unit_blueprint_medium.scheduled_stop_time, DEFAULT_INTER_OBSERVATION_GAP)
 
-    @unittest.skip("Skipped because the corrected 'before' constraint broke scheduler behavior. See TMSS-705")
+
+    @unittest.skip("JS 2022-01-21: This test is correct, but fails due to unfinished work in find_best_next_schedulable_unit. Fix that.")
     def test_time_bound_unit_wins_even_at_lower_priority(self):
         # create two schedule units, one with high one with low prio.
         # first create them without any further constraints, and check if high prio wins.
-        scheduling_unit_draft_low = self.create_simple_observation_scheduling_unit("scheduling unit low", scheduling_set=self.scheduling_set_low)
+        scheduling_unit_draft_low = self.create_simple_observation_scheduling_unit("scheduling unit low", scheduling_set=self.scheduling_set_low, obs_duration=3600)
         scheduling_unit_blueprint_low = create_scheduling_unit_blueprint_and_tasks_and_subtasks_from_scheduling_unit_draft(scheduling_unit_draft_low)
 
-        scheduling_unit_draft_high = self.create_simple_observation_scheduling_unit("scheduling unit high", scheduling_set=self.scheduling_set_high)
+        scheduling_unit_draft_high = self.create_simple_observation_scheduling_unit("scheduling unit high", scheduling_set=self.scheduling_set_high, obs_duration=3600)
         scheduling_unit_blueprint_high = create_scheduling_unit_blueprint_and_tasks_and_subtasks_from_scheduling_unit_draft(scheduling_unit_draft_high)
 
         now = datetime.utcnow()
         tomorrow = now+timedelta(days=1)
 
         # call the method-under-test.
-        best_scored_scheduling_unit = find_best_next_schedulable_unit([scheduling_unit_blueprint_low, scheduling_unit_blueprint_high], now, tomorrow)
+        best_scored_scheduling_unit = self.scheduler.find_best_next_schedulable_unit([scheduling_unit_blueprint_low, scheduling_unit_blueprint_high], now, tomorrow)
 
         # we expect the scheduling_unit with the highest project rank to be scheduled first
         self.assertEqual(scheduling_unit_blueprint_high.id, best_scored_scheduling_unit.scheduling_unit.id)
 
-        #now update the low prio unit with a time constraint, "forcing" it to be run in a very thight upcoming time window.
-        scheduling_unit_draft_low.scheduling_constraints_doc['time'] = { 'before': (now+scheduling_unit_draft_low.duration+timedelta(seconds=10)).isoformat()+'Z' }
-        scheduling_unit_draft_low.save()
-        scheduling_unit_blueprint_low.refresh_from_db()
+        # now update the low prio unit with a time constraint, "forcing" it to be run in a very thight upcoming time window.
+        scheduling_unit_blueprint_low.scheduling_constraints_doc['time'] = { 'before': (now+scheduling_unit_blueprint_low.specified_main_observation_duration+DEFAULT_INTER_OBSERVATION_GAP).isoformat()+'Z' }
+        scheduling_unit_blueprint_low.save()
 
         # call the method-under-test.
-        best_scored_scheduling_unit = find_best_next_schedulable_unit([scheduling_unit_blueprint_low, scheduling_unit_blueprint_high], now, tomorrow)
+        best_scored_scheduling_unit = self.scheduler.find_best_next_schedulable_unit([scheduling_unit_blueprint_low, scheduling_unit_blueprint_high], now, tomorrow)
 
         # now we expect the scheduling_unit with the lowest project rank to be scheduled first because it can only run within this limited timewindow
         self.assertEqual(scheduling_unit_blueprint_low.id, best_scored_scheduling_unit.scheduling_unit.id)
 
 
-        #  update the low prio unit. enlarge the time window constraint a bit, so both low and high prio units can fit
+        # update the low prio unit. enlarge the time window constraint a bit, so both low and high prio units can fit
         # this should result that the high prio goes first, and the low prio (which now fits as well) goes second
-        scheduling_unit_draft_low.scheduling_constraints_doc['time'] = { 'before': (now+scheduling_unit_draft_low.duration+scheduling_unit_draft_high.duration+timedelta(seconds=10)).isoformat()+'Z' }
-        scheduling_unit_draft_low.save()
-        scheduling_unit_blueprint_low.refresh_from_db()
+        scheduling_unit_blueprint_low.scheduling_constraints_doc['time'] = { 'before': (now+scheduling_unit_blueprint_low.specified_main_observation_duration+scheduling_unit_draft_high.duration+DEFAULT_INTER_OBSERVATION_GAP).isoformat()+'Z' }
+        scheduling_unit_blueprint_low.save()
 
         # call the method-under-test.
-        best_scored_scheduling_unit = find_best_next_schedulable_unit([scheduling_unit_blueprint_low, scheduling_unit_blueprint_high], now, tomorrow)
+        best_scored_scheduling_unit = self.scheduler.find_best_next_schedulable_unit([scheduling_unit_blueprint_low, scheduling_unit_blueprint_high], now, tomorrow)
 
-        # now we again expect the scheduling_unit with the higher project rank to be scheduled first
+        # now we expect the scheduling_unit with the higher project rank to be scheduled first (because there is enough room to schedule the low prio later)
         self.assertEqual(scheduling_unit_blueprint_high.id, best_scored_scheduling_unit.scheduling_unit.id)
 
         # call the method-under-test again but search after first unit (should return low prio unit)
-        stop_time_of_first =  best_scored_scheduling_unit.scheduled_start_time + best_scored_scheduling_unit.scheduling_unit.duration
-        best_scored_scheduling_unit = find_best_next_schedulable_unit([schedulingt_unit_blueprint_low, scheduling_unit_blueprint_high], stop_time_of_first, tomorrow)
+        stop_time_of_first =  best_scored_scheduling_unit.scheduled_start_time + best_scored_scheduling_unit.scheduling_unit.specified_main_observation_duration
+        best_scored_scheduling_unit = self.scheduler.find_best_next_schedulable_unit([schedulingt_unit_blueprint_low, scheduling_unit_blueprint_high], stop_time_of_first, tomorrow)
         self.assertEqual(scheduling_unit_blueprint_low.id, best_scored_scheduling_unit.scheduling_unit.id)
 
+
     def test_priority_queues_and_project_priority_and_unit_priority(self):
         """
         All scheduling_units in queue "A" should be scheduled before any unit from queue "B" are scheduled
-- 
GitLab