unittest_fakesession.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright (C) 2010 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. import isc
  16. class WouldBlockForever(Exception):
  17. """
  18. This is thrown by the FakeModuleCCSession if it would need
  19. to block forever for incoming message.
  20. """
  21. pass
  22. #
  23. # We can probably use a more general version of this
  24. #
  25. class FakeModuleCCSession:
  26. def __init__(self):
  27. self.subscriptions = {}
  28. # each entry is of the form [ channel, instance, message, want_answer ]
  29. self.message_queue = []
  30. self._socket = "ok we just need something not-None here atm"
  31. # if self.timeout is set to anything other than 0, and
  32. # the message_queue is empty when receive is called, throw
  33. # a SessionTimeout
  34. self._timeout = 0
  35. self._closed = False
  36. def group_subscribe(self, group_name, instance_name = None):
  37. if not group_name in self.subscriptions:
  38. self.subscriptions[group_name] = []
  39. if instance_name:
  40. self.subscriptions[group_name].append(instance_name)
  41. def group_unsubscribe(self, group_name, instance_name = None):
  42. # raises SessionError if the session has been already closed.
  43. if self._closed:
  44. raise isc.cc.SessionError("Session has been closed.")
  45. if group_name in self.subscriptions:
  46. if instance_name:
  47. if len(self.subscriptions[group_name]) > 1:
  48. del self.subscriptions[group_name][instance_name]
  49. else:
  50. del self.subscriptions[group_name]
  51. else:
  52. del self.subscriptions[group_name]
  53. def has_subscription(self, group_name, instance_name = None):
  54. if group_name in self.subscriptions:
  55. if instance_name:
  56. return instance_name in self.subscriptions[group_name]
  57. else:
  58. return True
  59. else:
  60. return False
  61. def group_sendmsg(self, msg, group, instance=None, to=None,
  62. want_answer=False):
  63. self.message_queue.append([ group, instance, msg, want_answer ])
  64. return 42
  65. def group_reply(self, env, msg):
  66. if 'group' in env:
  67. self.message_queue.append([ env['group'], None, msg, False])
  68. def group_recvmsg(self, nonblock=True, seq = None):
  69. for qm in self.message_queue:
  70. if qm[0] in self.subscriptions and (qm[1] == None or qm[1] in
  71. self.subscriptions[qm[0]]):
  72. self.message_queue.remove(qm)
  73. return qm[2], {'group': qm[0], 'from': qm[1]}
  74. if self._timeout == 0:
  75. if nonblock:
  76. return None, None
  77. else:
  78. raise WouldBlockForever(
  79. "Blocking read without timeout and no message ready")
  80. else:
  81. raise isc.cc.SessionTimeout("Timeout set but no data to "
  82. "return to group_recvmsg()")
  83. def get_message(self, channel, target = None):
  84. for qm in self.message_queue:
  85. if qm[0] == channel and qm[1] == target:
  86. self.message_queue.remove(qm)
  87. return qm[2]
  88. return None
  89. def close(self):
  90. # need to pass along somehow that this function has been called,
  91. self._socket = None
  92. self._closed = True
  93. def set_timeout(self, timeout):
  94. self._timeout = timeout
  95. def get_timeout(self):
  96. return self._timeout