testWait.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.basesimulator import BaseSimulator
  17. class TestWait(unittest.TestCase):
  18. def setUp(self):
  19. self.sim = basicSim()
  20. def tearDown(self):
  21. self.sim.run_gvt = False
  22. def checkWait(self, finish):
  23. self.checkWaitStart()
  24. self.checkWaitStop(finish)
  25. def checkWaitStart(self):
  26. # A thread, otherwise the state of the lock cannot be checked
  27. # Though this is somewhat more difficult to terminate...
  28. self.proc = threading.Thread(target=BaseSimulator.waitUntilOK,
  29. args=[self.sim, 0])
  30. self.proc.start()
  31. def checkWaitStop(self, finish=True):
  32. # Give it not too much time to finish
  33. self.proc.join(0.1)
  34. # Still running, but shouldn't finish --> OK
  35. if self.proc.is_alive() and not finish:
  36. return
  37. # Still running, but should finish --> NOK
  38. elif self.proc.is_alive() and finish: #pragma: nocover
  39. # Force thread shutdown
  40. self.sim.V[0] = float('inf')
  41. self.sim.Vchange.set()
  42. self.fail("Thread should finish, but was not finished")
  43. # No longer running, and should finish --> OK
  44. elif not self.proc.is_alive() and finish:
  45. return
  46. # No longer running, but should not finish --> NOK
  47. elif not self.proc.is_alive() and not finish: #pragma: nocover
  48. self.fail("Thread should keep running, but has finished")
  49. def test_GVT_wait(self):
  50. # Run a seperate thread for the waiting,
  51. # after each modification, we just check wheter it is still live
  52. self.sim.Vlock.acquire()
  53. self.sim.V = [{0: 0}, {}]
  54. self.sim.control_msg = [0, 0, {0: 0}]
  55. self.checkWait(finish=True)
  56. self.sim.V = [{0: -3}, {}]
  57. self.sim.control_msg = [0, 0, {0: 0}]
  58. self.checkWait(finish=True)
  59. self.sim.V = [{0: -3}, {}]
  60. self.sim.control_msg = [0, 0, {0: -4}]
  61. self.checkWait(finish=True)
  62. self.sim.V = [{0: 0}, {}]
  63. self.sim.control_msg = [0, 0, {0: -5}]
  64. self.checkWait(finish=True)
  65. self.sim.V = [{0: 3}, {}]
  66. self.sim.control_msg = [0, 0, {0: -3}]
  67. self.checkWait(finish=True)
  68. self.sim.V = [{0: -3}, {}]
  69. self.sim.control_msg = [0, 0, {0: 3}]
  70. self.checkWait(finish=True)
  71. self.sim.V = [{0: 1}, {}]
  72. self.sim.control_msg = [0, 0, {0: 0}]
  73. self.checkWait(finish=False)
  74. self.sim.notifyReceive(False)
  75. self.checkWaitStop(finish=True)
  76. self.sim.V = [{0: 0}, {}]
  77. self.sim.control_msg = [0, 0, {0: 1}]
  78. self.checkWait(finish=False)
  79. self.sim.notifyReceive(False)
  80. self.checkWaitStop(finish=True)
  81. self.sim.V = [{0: 3}, {}]
  82. self.sim.control_msg = [0, 0, {0: -2}]
  83. self.checkWait(finish=False)
  84. self.sim.notifyReceive(False)
  85. self.checkWaitStop(finish=True)
  86. # Should stop at exactly the correct time
  87. self.sim.V = [{0: -3}, {}]
  88. self.sim.control_msg = [0, 0, {0: 6}]
  89. self.checkWait(finish=False)
  90. self.sim.notifyReceive(False)
  91. self.checkWaitStop(finish=False)
  92. self.sim.notifyReceive(False)
  93. self.checkWaitStop(finish=False)
  94. self.sim.notifyReceive(False)
  95. self.checkWaitStop(finish=True)
  96. # Do multiple receives at once
  97. self.sim.V = [{0: -3}, {}]
  98. self.sim.control_msg = [0, 0, {0: 6}]
  99. self.checkWait(finish=False)
  100. self.sim.notifyReceive(False)
  101. self.sim.notifyReceive(False)
  102. self.sim.notifyReceive(False)
  103. self.checkWaitStop(finish=True)