/indra/llmessage/tests/testrunner.py

https://bitbucket.org/lindenlab/viewer-beta/ · Python · 262 lines · 137 code · 32 blank · 93 comment · 37 complexity · 8f3adf06d3bd1c66d01f87be3f42add5 MD5 · raw file

  1. #!/usr/bin/env python
  2. """\
  3. @file testrunner.py
  4. @author Nat Goodspeed
  5. @date 2009-03-20
  6. @brief Utilities for writing wrapper scripts for ADD_COMM_BUILD_TEST unit tests
  7. $LicenseInfo:firstyear=2009&license=viewerlgpl$
  8. Second Life Viewer Source Code
  9. Copyright (C) 2010, Linden Research, Inc.
  10. This library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation;
  13. version 2.1 of the License only.
  14. This library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  22. $/LicenseInfo$
  23. """
  24. from __future__ import with_statement
  25. import os
  26. import sys
  27. import re
  28. import errno
  29. import socket
  30. VERBOSE = os.environ.get("INTEGRATION_TEST_VERBOSE", "1") # default to verbose
  31. # Support usage such as INTEGRATION_TEST_VERBOSE=off -- distressing to user if
  32. # that construct actually turns on verbosity...
  33. VERBOSE = not re.match(r"(0|off|false|quiet)$", VERBOSE, re.IGNORECASE)
  34. if VERBOSE:
  35. def debug(fmt, *args):
  36. print fmt % args
  37. sys.stdout.flush()
  38. else:
  39. debug = lambda *args: None
  40. def freeport(portlist, expr):
  41. """
  42. Find a free server port to use. Specifically, evaluate 'expr' (a
  43. callable(port)) until it stops raising EADDRINUSE exception.
  44. Pass:
  45. portlist: an iterable (e.g. xrange()) of ports to try. If you exhaust the
  46. range, freeport() lets the socket.error exception propagate. If you want
  47. unbounded, you could pass itertools.count(baseport), though of course in
  48. practice the ceiling is 2^16-1 anyway. But it seems prudent to constrain
  49. the range much more sharply: if we're iterating an absurd number of times,
  50. probably something else is wrong.
  51. expr: a callable accepting a port number, specifically one of the items
  52. from portlist. If calling that callable raises socket.error with
  53. EADDRINUSE, freeport() retrieves the next item from portlist and retries.
  54. Returns: (expr(port), port)
  55. port: the value from portlist for which expr(port) succeeded
  56. Raises:
  57. Any exception raised by expr(port) other than EADDRINUSE.
  58. socket.error if, for every item from portlist, expr(port) raises
  59. socket.error. The exception you see is the one from the last item in
  60. portlist.
  61. StopIteration if portlist is completely empty.
  62. Example:
  63. class Server(HTTPServer):
  64. # If you use BaseHTTPServer.HTTPServer, turning off this flag is
  65. # essential for proper operation of freeport()!
  66. allow_reuse_address = False
  67. # ...
  68. server, port = freeport(xrange(8000, 8010),
  69. lambda port: Server(("localhost", port),
  70. MyRequestHandler))
  71. # pass 'port' to client code
  72. # call server.serve_forever()
  73. """
  74. try:
  75. # If portlist is completely empty, let StopIteration propagate: that's an
  76. # error because we can't return meaningful values. We have no 'port',
  77. # therefore no 'expr(port)'.
  78. portiter = iter(portlist)
  79. port = portiter.next()
  80. while True:
  81. try:
  82. # If this value of port works, return as promised.
  83. value = expr(port)
  84. except socket.error, err:
  85. # Anything other than 'Address already in use', propagate
  86. if err.args[0] != errno.EADDRINUSE:
  87. raise
  88. # Here we want the next port from portiter. But on StopIteration,
  89. # we want to raise the original exception rather than
  90. # StopIteration. So save the original exc_info().
  91. type, value, tb = sys.exc_info()
  92. try:
  93. try:
  94. port = portiter.next()
  95. except StopIteration:
  96. raise type, value, tb
  97. finally:
  98. # Clean up local traceback, see docs for sys.exc_info()
  99. del tb
  100. else:
  101. debug("freeport() returning %s on port %s", value, port)
  102. return value, port
  103. # Recap of the control flow above:
  104. # If expr(port) doesn't raise, return as promised.
  105. # If expr(port) raises anything but EADDRINUSE, propagate that
  106. # exception.
  107. # If portiter.next() raises StopIteration -- that is, if the port
  108. # value we just passed to expr(port) was the last available -- reraise
  109. # the EADDRINUSE exception.
  110. # If we've actually arrived at this point, portiter.next() delivered a
  111. # new port value. Loop back to pass that to expr(port).
  112. except Exception, err:
  113. debug("*** freeport() raising %s: %s", err.__class__.__name__, err)
  114. raise
  115. def run(*args, **kwds):
  116. """All positional arguments collectively form a command line, executed as
  117. a synchronous child process.
  118. In addition, pass server=new_thread_instance as an explicit keyword (to
  119. differentiate it from an additional command-line argument).
  120. new_thread_instance should be an instantiated but not yet started Thread
  121. subclass instance, e.g.:
  122. run("python", "-c", 'print "Hello, world!"', server=TestHTTPServer(name="httpd"))
  123. """
  124. # If there's no server= keyword arg, don't start a server thread: simply
  125. # run a child process.
  126. try:
  127. thread = kwds.pop("server")
  128. except KeyError:
  129. pass
  130. else:
  131. # Start server thread. Note that this and all other comm server
  132. # threads should be daemon threads: we'll let them run "forever,"
  133. # confident that the whole process will terminate when the main thread
  134. # terminates, which will be when the child process terminates.
  135. thread.setDaemon(True)
  136. thread.start()
  137. # choice of os.spawnv():
  138. # - [v vs. l] pass a list of args vs. individual arguments,
  139. # - [no p] don't use the PATH because we specifically want to invoke the
  140. # executable passed as our first arg,
  141. # - [no e] child should inherit this process's environment.
  142. debug("Running %s...", " ".join(args))
  143. rc = os.spawnv(os.P_WAIT, args[0], args)
  144. debug("%s returned %s", args[0], rc)
  145. return rc
  146. # ****************************************************************************
  147. # test code -- manual at this point, see SWAT-564
  148. # ****************************************************************************
  149. def test_freeport():
  150. # ------------------------------- Helpers --------------------------------
  151. from contextlib import contextmanager
  152. # helper Context Manager for expecting an exception
  153. # with exc(SomeError):
  154. # raise SomeError()
  155. # raises AssertionError otherwise.
  156. @contextmanager
  157. def exc(exception_class, *args):
  158. try:
  159. yield
  160. except exception_class, err:
  161. for i, expected_arg in enumerate(args):
  162. assert expected_arg == err.args[i], \
  163. "Raised %s, but args[%s] is %r instead of %r" % \
  164. (err.__class__.__name__, i, err.args[i], expected_arg)
  165. print "Caught expected exception %s(%s)" % \
  166. (err.__class__.__name__, ', '.join(repr(arg) for arg in err.args))
  167. else:
  168. assert False, "Failed to raise " + exception_class.__class__.__name__
  169. # helper to raise specified exception
  170. def raiser(exception):
  171. raise exception
  172. # the usual
  173. def assert_equals(a, b):
  174. assert a == b, "%r != %r" % (a, b)
  175. # ------------------------ Sanity check the above ------------------------
  176. class SomeError(Exception): pass
  177. # Without extra args, accept any err.args value
  178. with exc(SomeError):
  179. raiser(SomeError("abc"))
  180. # With extra args, accept only the specified value
  181. with exc(SomeError, "abc"):
  182. raiser(SomeError("abc"))
  183. with exc(AssertionError):
  184. with exc(SomeError, "abc"):
  185. raiser(SomeError("def"))
  186. with exc(AssertionError):
  187. with exc(socket.error, errno.EADDRINUSE):
  188. raiser(socket.error(errno.ECONNREFUSED, 'Connection refused'))
  189. # ----------- freeport() without engaging socket functionality -----------
  190. # If portlist is empty, freeport() raises StopIteration.
  191. with exc(StopIteration):
  192. freeport([], None)
  193. assert_equals(freeport([17], str), ("17", 17))
  194. # This is the magic exception that should prompt us to retry
  195. inuse = socket.error(errno.EADDRINUSE, 'Address already in use')
  196. # Get the iterator to our ports list so we can check later if we've used all
  197. ports = iter(xrange(5))
  198. with exc(socket.error, errno.EADDRINUSE):
  199. freeport(ports, lambda port: raiser(inuse))
  200. # did we entirely exhaust 'ports'?
  201. with exc(StopIteration):
  202. ports.next()
  203. ports = iter(xrange(2))
  204. # Any exception but EADDRINUSE should quit immediately
  205. with exc(SomeError):
  206. freeport(ports, lambda port: raiser(SomeError()))
  207. assert_equals(ports.next(), 1)
  208. # ----------- freeport() with platform-dependent socket stuff ------------
  209. # This is what we should've had unit tests to begin with (see CHOP-661).
  210. def newbind(port):
  211. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  212. sock.bind(('127.0.0.1', port))
  213. return sock
  214. bound0, port0 = freeport(xrange(7777, 7780), newbind)
  215. assert_equals(port0, 7777)
  216. bound1, port1 = freeport(xrange(7777, 7780), newbind)
  217. assert_equals(port1, 7778)
  218. bound2, port2 = freeport(xrange(7777, 7780), newbind)
  219. assert_equals(port2, 7779)
  220. with exc(socket.error, errno.EADDRINUSE):
  221. bound3, port3 = freeport(xrange(7777, 7780), newbind)
  222. if __name__ == "__main__":
  223. test_freeport()