__init__.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. # Copyright (c) PyZMQ Developers.
  2. # Distributed under the terms of the Modified BSD License.
  3. import os
  4. import platform
  5. import signal
  6. import sys
  7. import time
  8. import warnings
  9. from functools import partial
  10. from threading import Thread
  11. from typing import List
  12. from unittest import SkipTest, TestCase
  13. from pytest import mark
  14. import zmq
  15. from zmq.utils import jsonapi
  16. try:
  17. import gevent
  18. from zmq import green as gzmq
  19. have_gevent = True
  20. except ImportError:
  21. have_gevent = False
  22. PYPY = platform.python_implementation() == 'PyPy'
  23. # -----------------------------------------------------------------------------
  24. # skip decorators (directly from unittest)
  25. # -----------------------------------------------------------------------------
  26. warnings.warn(
  27. "zmq.tests is deprecated in pyzmq 25, we recommend managing your own contexts and sockets.",
  28. DeprecationWarning,
  29. )
  30. def _id(x):
  31. return x
  32. skip_pypy = mark.skipif(PYPY, reason="Doesn't work on PyPy")
  33. require_zmq_4 = mark.skipif(zmq.zmq_version_info() < (4,), reason="requires zmq >= 4")
  34. # -----------------------------------------------------------------------------
  35. # Base test class
  36. # -----------------------------------------------------------------------------
  37. def term_context(ctx, timeout):
  38. """Terminate a context with a timeout"""
  39. t = Thread(target=ctx.term)
  40. t.daemon = True
  41. t.start()
  42. t.join(timeout=timeout)
  43. if t.is_alive():
  44. # reset Context.instance, so the failure to term doesn't corrupt subsequent tests
  45. zmq.sugar.context.Context._instance = None
  46. raise RuntimeError(
  47. "context could not terminate, open sockets likely remain in test"
  48. )
  49. class BaseZMQTestCase(TestCase):
  50. green = False
  51. teardown_timeout = 10
  52. test_timeout_seconds = int(os.environ.get("ZMQ_TEST_TIMEOUT") or 60)
  53. sockets: List[zmq.Socket]
  54. @property
  55. def _is_pyzmq_test(self):
  56. return self.__class__.__module__.split(".", 1)[0] == __name__.split(".", 1)[0]
  57. @property
  58. def _should_test_timeout(self):
  59. return (
  60. self._is_pyzmq_test
  61. and hasattr(signal, 'SIGALRM')
  62. and self.test_timeout_seconds
  63. )
  64. @property
  65. def Context(self):
  66. if self.green:
  67. return gzmq.Context
  68. else:
  69. return zmq.Context
  70. def socket(self, socket_type):
  71. s = self.context.socket(socket_type)
  72. self.sockets.append(s)
  73. return s
  74. def _alarm_timeout(self, timeout, *args):
  75. raise TimeoutError(f"Test did not complete in {timeout} seconds")
  76. def setUp(self):
  77. super().setUp()
  78. if self.green and not have_gevent:
  79. raise SkipTest("requires gevent")
  80. self.context = self.Context.instance()
  81. self.sockets = []
  82. if self._should_test_timeout:
  83. # use SIGALRM to avoid test hangs
  84. signal.signal(
  85. signal.SIGALRM, partial(self._alarm_timeout, self.test_timeout_seconds)
  86. )
  87. signal.alarm(self.test_timeout_seconds)
  88. def tearDown(self):
  89. if self._should_test_timeout:
  90. # cancel the timeout alarm, if there was one
  91. signal.alarm(0)
  92. contexts = {self.context}
  93. while self.sockets:
  94. sock = self.sockets.pop()
  95. contexts.add(sock.context) # in case additional contexts are created
  96. sock.close(0)
  97. for ctx in contexts:
  98. try:
  99. term_context(ctx, self.teardown_timeout)
  100. except Exception:
  101. # reset Context.instance, so the failure to term doesn't corrupt subsequent tests
  102. zmq.sugar.context.Context._instance = None
  103. raise
  104. super().tearDown()
  105. def create_bound_pair(
  106. self, type1=zmq.PAIR, type2=zmq.PAIR, interface='tcp://127.0.0.1'
  107. ):
  108. """Create a bound socket pair using a random port."""
  109. s1 = self.context.socket(type1)
  110. s1.setsockopt(zmq.LINGER, 0)
  111. port = s1.bind_to_random_port(interface)
  112. s2 = self.context.socket(type2)
  113. s2.setsockopt(zmq.LINGER, 0)
  114. s2.connect(f'{interface}:{port}')
  115. self.sockets.extend([s1, s2])
  116. return s1, s2
  117. def ping_pong(self, s1, s2, msg):
  118. s1.send(msg)
  119. msg2 = s2.recv()
  120. s2.send(msg2)
  121. msg3 = s1.recv()
  122. return msg3
  123. def ping_pong_json(self, s1, s2, o):
  124. if jsonapi.jsonmod is None:
  125. raise SkipTest("No json library")
  126. s1.send_json(o)
  127. o2 = s2.recv_json()
  128. s2.send_json(o2)
  129. o3 = s1.recv_json()
  130. return o3
  131. def ping_pong_pyobj(self, s1, s2, o):
  132. s1.send_pyobj(o)
  133. o2 = s2.recv_pyobj()
  134. s2.send_pyobj(o2)
  135. o3 = s1.recv_pyobj()
  136. return o3
  137. def assertRaisesErrno(self, errno, func, *args, **kwargs):
  138. try:
  139. func(*args, **kwargs)
  140. except zmq.ZMQError as e:
  141. self.assertEqual(
  142. e.errno,
  143. errno,
  144. f"wrong error raised, expected '{zmq.ZMQError(errno)}' \
  145. got '{zmq.ZMQError(e.errno)}'",
  146. )
  147. else:
  148. self.fail("Function did not raise any error")
  149. def _select_recv(self, multipart, socket, **kwargs):
  150. """call recv[_multipart] in a way that raises if there is nothing to receive"""
  151. # zmq 3.1 has a bug, where poll can return false positives,
  152. # so we wait a little bit just in case
  153. # See LIBZMQ-280 on JIRA
  154. time.sleep(0.1)
  155. r, w, x = zmq.select([socket], [], [], timeout=kwargs.pop('timeout', 5))
  156. assert len(r) > 0, "Should have received a message"
  157. kwargs['flags'] = zmq.DONTWAIT | kwargs.get('flags', 0)
  158. recv = socket.recv_multipart if multipart else socket.recv
  159. return recv(**kwargs)
  160. def recv(self, socket, **kwargs):
  161. """call recv in a way that raises if there is nothing to receive"""
  162. return self._select_recv(False, socket, **kwargs)
  163. def recv_multipart(self, socket, **kwargs):
  164. """call recv_multipart in a way that raises if there is nothing to receive"""
  165. return self._select_recv(True, socket, **kwargs)
  166. class PollZMQTestCase(BaseZMQTestCase):
  167. pass
  168. class GreenTest:
  169. """Mixin for making green versions of test classes"""
  170. green = True
  171. teardown_timeout = 10
  172. def assertRaisesErrno(self, errno, func, *args, **kwargs):
  173. if errno == zmq.EAGAIN:
  174. raise SkipTest("Skipping because we're green.")
  175. try:
  176. func(*args, **kwargs)
  177. except zmq.ZMQError:
  178. e = sys.exc_info()[1]
  179. self.assertEqual(
  180. e.errno,
  181. errno,
  182. f"wrong error raised, expected '{zmq.ZMQError(errno)}' \
  183. got '{zmq.ZMQError(e.errno)}'",
  184. )
  185. else:
  186. self.fail("Function did not raise any error")
  187. def tearDown(self):
  188. if self._should_test_timeout:
  189. # cancel the timeout alarm, if there was one
  190. signal.alarm(0)
  191. contexts = {self.context}
  192. while self.sockets:
  193. sock = self.sockets.pop()
  194. contexts.add(sock.context) # in case additional contexts are created
  195. sock.close()
  196. try:
  197. gevent.joinall(
  198. [gevent.spawn(ctx.term) for ctx in contexts],
  199. timeout=self.teardown_timeout,
  200. raise_error=True,
  201. )
  202. except gevent.Timeout:
  203. raise RuntimeError(
  204. "context could not terminate, open sockets likely remain in test"
  205. )
  206. def skip_green(self):
  207. raise SkipTest("Skipping because we are green")
  208. def skip_green(f):
  209. def skipping_test(self, *args, **kwargs):
  210. if self.green:
  211. raise SkipTest("Skipping because we are green")
  212. else:
  213. return f(self, *args, **kwargs)
  214. return skipping_test