testTermination.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. class TestTermination(unittest.TestCase):
  17. # Tests the externalInput function, which takes messages of the form:
  18. # [[time, age], content, anti-message, UUID, color]
  19. def setUp(self):
  20. self.sim = basicSim()
  21. self.msg = basicMsg()
  22. def tearDown(self):
  23. self.sim.run_gvt = False
  24. def test_check_global(self):
  25. # If check returns True, it means that we should stop
  26. # This is for basic situations without input_scheduler
  27. self.sim.termination_time = (5, float('inf'))
  28. self.sim.model = Generator()
  29. self.assertFalse(self.sim.check())
  30. self.sim.model.time_next = (5, 1)
  31. self.assertFalse(self.sim.check())
  32. self.sim.model.time_next = (5, 2)
  33. self.assertFalse(self.sim.check())
  34. self.sim.model.time_next = (6, 1)
  35. self.assertTrue(self.sim.check())
  36. self.sim.model.time_next = (float('inf'), 1)
  37. self.assertTrue(self.sim.check())
  38. # Now there is an input_scheduler
  39. msg = NetworkMessage(self.msg.timestamp, self.msg.content, self.msg.uuid, self.msg.color, self.msg.destination)
  40. msg2 = NetworkMessage(self.msg.timestamp, self.msg.content, self.msg.uuid, self.msg.color, self.msg.destination)
  41. self.sim.input_scheduler.heap = [msg]
  42. self.sim.model.time_next = (float('inf'), 1)
  43. # Message in input_scheduler must still be sent
  44. msg.timestamp = (3, 1)
  45. self.assertFalse(self.sim.check())
  46. # Message in input_scheduler is after the termination time
  47. msg.timestamp = (6, 1)
  48. self.assertTrue(self.sim.check())
  49. # Messages in input_scheduler, only last one must still be sent
  50. msg.timestamp = (2, 1)
  51. msg2.timestamp = (4, 1)
  52. self.sim.prevtime = (3, 1)
  53. self.sim.input_scheduler.heap.append(msg2)
  54. self.assertFalse(self.sim.check())
  55. msg.timestamp = (6, 1)
  56. msg2.timestamp = (7, 1)
  57. self.sim.prevtime = (3, 1)
  58. self.sim.input_scheduler.heap.append(msg2)
  59. self.assertTrue(self.sim.check())
  60. self.sim.model.time_next = (3, 1)
  61. msg.timestamp = (2, 1)
  62. msg2.timestamp = (7, 1)
  63. self.sim.prevtime = (2, 4)
  64. self.sim.input_scheduler.heap.append(msg2)
  65. self.assertFalse(self.sim.check())