unittest_fakesession.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. # $Id$
  16. import isc
  17. #
  18. # We can probably use a more general version of this
  19. #
  20. class FakeModuleCCSession:
  21. def __init__(self):
  22. self.subscriptions = {}
  23. # each entry is of the form [ channel, instance, message ]
  24. self.message_queue = []
  25. self._socket = "ok we just need something not-None here atm"
  26. # if self.timeout is set to anything other than 0, and
  27. # the message_queue is empty when receive is called, throw
  28. # a SessionTimeout
  29. self._timeout = 0
  30. def group_subscribe(self, group_name, instance_name = None):
  31. if not group_name in self.subscriptions:
  32. self.subscriptions[group_name] = []
  33. if instance_name:
  34. self.subscriptions[group_name].append(instance_name)
  35. def group_unsubscribe(self, group_name, instance_name = None):
  36. if group_name in self.subscriptions:
  37. if instance_name:
  38. if len(self.subscriptions[group_name]) > 1:
  39. del self.subscriptions[group_name][instance_name]
  40. else:
  41. del self.subscriptions[group_name]
  42. else:
  43. del self.subscriptions[group_name]
  44. def has_subscription(self, group_name, instance_name = None):
  45. if group_name in self.subscriptions:
  46. if instance_name:
  47. return instance_name in self.subscriptions[group_name]
  48. else:
  49. return True
  50. else:
  51. return False
  52. def group_sendmsg(self, msg, channel, target = None):
  53. self.message_queue.append([ channel, target, msg ])
  54. def group_reply(self, env, msg):
  55. if 'group' in env:
  56. self.message_queue.append([ env['group'], None, msg])
  57. def group_recvmsg(self, blocking, seq = None):
  58. for qm in self.message_queue:
  59. if qm[0] in self.subscriptions and (qm[1] == None or qm[1] in self.subscriptions[qm[0]]):
  60. self.message_queue.remove(qm)
  61. return qm[2], {'group': qm[0], 'from': qm[1]}
  62. if self._timeout == 0:
  63. return None, None
  64. else:
  65. raise isc.cc.SessionTimeout("Timeout set but no data to "
  66. "return to group_recvmsg()")
  67. def get_message(self, channel, target = None):
  68. for qm in self.message_queue:
  69. if qm[0] == channel and qm[1] == target:
  70. self.message_queue.remove(qm)
  71. return qm[2]
  72. return None
  73. def close(self):
  74. # need to pass along somehow that this function has been called,
  75. self._socket = "closed"
  76. def set_timeout(self, timeout):
  77. self._timeout = timeout