mocksocket.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. """
  2. SleekXMPP: The Sleek XMPP Library
  3. Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout
  4. This file is part of SleekXMPP.
  5. See the file LICENSE for copying permission.
  6. """
  7. import socket
  8. from sleekxmpp.util import Queue
  9. class TestSocket(object):
  10. """
  11. A dummy socket that reads and writes to queues instead
  12. of an actual networking socket.
  13. Methods:
  14. next_sent -- Return the next sent stanza.
  15. recv_data -- Make a stanza available to read next.
  16. recv -- Read the next stanza from the socket.
  17. send -- Write a stanza to the socket.
  18. makefile -- Dummy call, returns self.
  19. read -- Read the next stanza from the socket.
  20. """
  21. def __init__(self, *args, **kwargs):
  22. """
  23. Create a new test socket.
  24. Arguments:
  25. Same as arguments for socket.socket
  26. """
  27. self.socket = socket.socket(*args, **kwargs)
  28. self.recv_queue = Queue()
  29. self.send_queue = Queue()
  30. self.is_live = False
  31. self.disconnected = False
  32. def __getattr__(self, name):
  33. """
  34. Return attribute values of internal, dummy socket.
  35. Some attributes and methods are disabled to prevent the
  36. socket from connecting to the network.
  37. Arguments:
  38. name -- Name of the attribute requested.
  39. """
  40. def dummy(*args):
  41. """Method to do nothing and prevent actual socket connections."""
  42. return None
  43. overrides = {'connect': dummy,
  44. 'close': dummy,
  45. 'shutdown': dummy}
  46. return overrides.get(name, getattr(self.socket, name))
  47. # ------------------------------------------------------------------
  48. # Testing Interface
  49. def next_sent(self, timeout=None):
  50. """
  51. Get the next stanza that has been 'sent'.
  52. Arguments:
  53. timeout -- Optional timeout for waiting for a new value.
  54. """
  55. args = {'block': False}
  56. if timeout is not None:
  57. args = {'block': True, 'timeout': timeout}
  58. try:
  59. return self.send_queue.get(**args)
  60. except:
  61. return None
  62. def recv_data(self, data):
  63. """
  64. Add data to the receiving queue.
  65. Arguments:
  66. data -- String data to 'write' to the socket to be received
  67. by the XMPP client.
  68. """
  69. self.recv_queue.put(data)
  70. def disconnect_error(self):
  71. """
  72. Simulate a disconnect error by raising a socket.error exception
  73. for any current or further socket operations.
  74. """
  75. self.disconnected = True
  76. # ------------------------------------------------------------------
  77. # Socket Interface
  78. def recv(self, *args, **kwargs):
  79. """
  80. Read a value from the received queue.
  81. Arguments:
  82. Placeholders. Same as for socket.Socket.recv.
  83. """
  84. if self.disconnected:
  85. raise socket.error
  86. return self.read(block=True)
  87. def send(self, data):
  88. """
  89. Send data by placing it in the send queue.
  90. Arguments:
  91. data -- String value to write.
  92. """
  93. if self.disconnected:
  94. raise socket.error
  95. self.send_queue.put(data)
  96. return len(data)
  97. # ------------------------------------------------------------------
  98. # File Socket
  99. def makefile(self, *args, **kwargs):
  100. """
  101. File socket version to use with ElementTree.
  102. Arguments:
  103. Placeholders, same as socket.Socket.makefile()
  104. """
  105. return self
  106. def read(self, block=True, timeout=None, **kwargs):
  107. """
  108. Implement the file socket interface.
  109. Arguments:
  110. block -- Indicate if the read should block until a
  111. value is ready.
  112. timeout -- Time in seconds a block should last before
  113. returning None.
  114. """
  115. if self.disconnected:
  116. raise socket.error
  117. if timeout is not None:
  118. block = True
  119. try:
  120. return self.recv_queue.get(block, timeout)
  121. except:
  122. return None