testScheduler.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright 2014 Modelling, Simulation and Design Lab (MSDL) at
  2. # McGill University and the University of Antwerp (http://msdl.cs.mcgill.ca/)
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from testutils import *
  16. from pypdevs.schedulers.schedulerAH import SchedulerAH
  17. class TestScheduler(unittest.TestCase):
  18. def setUp(self):
  19. self.models = []
  20. for i in range(10):
  21. ne = Generator()
  22. ne.model_id = i
  23. ne.time_next = (1, 1)
  24. self.models.append(ne)
  25. for i in range(10):
  26. ne = Processor()
  27. ne.model_id = i + 10
  28. ne.time_next = (float('inf'), 1)
  29. self.models.append(ne)
  30. self.scheduler = SchedulerAH(self.models, 1e-9, len(self.models))
  31. def tearDown(self):
  32. pass
  33. def test_scheduler_schedule(self):
  34. # Only 10, since the Processors should not get scheduled
  35. # due to their time_next
  36. self.assertTrue(len(self.scheduler.heap) == 10)
  37. def test_scheduler_unschedule(self):
  38. self.assertTrue(len(self.scheduler.heap) == 10)
  39. for i in self.models:
  40. self.scheduler.unschedule(i)
  41. # Heap should have the same length, as they became invalid
  42. self.assertTrue(len(self.scheduler.heap) == 10)
  43. # Clean up
  44. self.scheduler.cleanFirst()
  45. self.assertTrue(len(self.scheduler.heap) == 0)
  46. for i in self.models:
  47. self.scheduler.schedule(i)
  48. self.scheduler.unschedule(self.models[5])
  49. self.assertTrue(len(self.scheduler.heap) == 10)
  50. def test_scheduler_get_imminent(self):
  51. self.assertTrue(len(self.scheduler.heap) == 10)
  52. self.scheduler.unschedule(self.models[2])
  53. self.scheduler.unschedule(self.models[4])
  54. self.scheduler.unschedule(self.models[0])
  55. self.scheduler.unschedule(self.models[7])
  56. verifylist = list(self.models[:10])
  57. verifylist.remove(self.models[0])
  58. verifylist.remove(self.models[2])
  59. verifylist.remove(self.models[4])
  60. verifylist.remove(self.models[7])
  61. # Heap should have the same length, as they became invalid
  62. self.assertTrue(len(self.scheduler.heap) == 10)
  63. res = self.scheduler.getImminent((1, 1))
  64. self.assertTrue(res == verifylist)
  65. for i in self.models:
  66. try:
  67. self.scheduler.unschedule(i)
  68. except TypeError:
  69. # Some are possibly already None
  70. pass
  71. # List should be completely empty now
  72. res = self.scheduler.getImminent((1, 1))
  73. self.assertTrue(res == [])