testutils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. import os
  16. import sys
  17. sys.path.append('testmodels')
  18. import pypdevs.middleware as middleware
  19. import unittest
  20. import logging
  21. from pypdevs.logger import setLogger
  22. setLogger('None', ('localhost', 514), logging.WARN)
  23. from pypdevs.controller import Controller
  24. import threading
  25. from pypdevs.basesimulator import BaseSimulator
  26. from pypdevs.message import NetworkMessage
  27. from models import *
  28. from collections import defaultdict
  29. class StubController(Controller):
  30. def __init__(self, name):
  31. Controller.__init__(self, name, None, None)
  32. self.reverted = False
  33. # Just don't create an int, as this indicates remote locations
  34. self.destinations = defaultdict(lambda : None)
  35. from pypdevs.relocators.manualRelocator import ManualRelocator
  36. self.relocator = ManualRelocator()
  37. self.initialAllocator = None
  38. def revert(self, a):
  39. self.reverted = True
  40. def receiveControl(self, msg):
  41. thrd = threading.Thread(target=BaseSimulator.receiveControl, args=[self, msg])
  42. thrd.start()
  43. def equalStateVectors(v1, v2):
  44. if len(v1) != len(v2):
  45. return False
  46. for i in range(len(v1)):
  47. if v1[i][0] != v2[i][0]:
  48. return False
  49. if v1[i][1] != v2[i][1]:
  50. return False
  51. # Don't check the state, as this contains addresses
  52. return True
  53. def vcdEqual(f1, f2):
  54. f1 = open(f1, 'r')
  55. f2 = open(f2, 'r')
  56. line = 0
  57. for l1, l2 in zip(f1, f2):
  58. if l1 != l2 and line != 1:
  59. return False
  60. line += 1
  61. return True
  62. def removeFile(f1):
  63. try:
  64. os.remove(f1)
  65. except OSError:
  66. # File was not there, so result is the same
  67. pass
  68. def basicSim():
  69. class StubModel(object):
  70. def __init__(self):
  71. self.local_model_ids = set([0, 1])
  72. class StubServer(object):
  73. def getProxy(self, name):
  74. return None
  75. setLogger('None', ('localhost', 514), logging.WARN)
  76. sim = StubController(0)
  77. # Kernels doesn't really matter in the tests, though use a value > 1 to prevent localised optimisations
  78. sim.server = StubServer()
  79. sim.setGlobals(
  80. tracers=[],
  81. address=('localhost', 514),
  82. loglevel=logging.WARN,
  83. checkpoint_name="(none)",
  84. memoization=False,
  85. statesaver=2,
  86. kernels=3,
  87. checkpoint_frequency=-1,
  88. msg_copy=0)
  89. # Set it so that it should be initialised to a decent prevtime
  90. sim.prevtime = (0, 1)
  91. sim.model = StubModel()
  92. sim.simlock.release()
  93. return sim
  94. def basicMsg():
  95. class StubDEVS(object):
  96. def __init__(self):
  97. self.model_id = 0
  98. class StubPort(object):
  99. def __init__(self):
  100. self.port_id = 0
  101. self.hostDEVS = StubDEVS()
  102. time = 1
  103. age = 1
  104. content = {StubPort(): None}
  105. uuid = 12345
  106. color = False
  107. return NetworkMessage((time, age), content, uuid, color, None)