Skip to content

Instantly share code, notes, and snippets.

@brettdh
Last active May 14, 2016 00:37
Show Gist options
  • Save brettdh/b6e741227b2297f19d2118077f14dfa5 to your computer and use it in GitHub Desktop.
Save brettdh/b6e741227b2297f19d2118077f14dfa5 to your computer and use it in GitHub Desktop.
Difference in output for a simple failed test with testserver.Server, with the server socket interruption suppressed vs. raised

In the newly added test_server_finishes_on_error test for testserver.Server, I changed the pass in the except block to raise, to temporarily cause the test to fail. The two files in this gist show the difference in output between letting the select.error/socket.error from interrupting _accept_connection be raised vs. suppressed.

In socket_interruption_suppressed.txt, it is clear where the test failed - at the exception in the test function. In socket_interruption_raised.txt, there is extra output that distracts from the real failure. The test server is functioning as it should - allowing the server thread to be interrupted and exit gracefully when the test code leaves the with block - so the error output in this case is misleading. Nothing went wrong in the test server; it simply exited before accepting the number of expected connections. If this is an error in the context of the code under test, that must be expressed in the test code, so errors from the test server are irrelevant.

This is obviously a contrived example, but similar things happened as I was working on the tests for the ALL_PROXY support as they were failing.

That said, I don't feel terribly strong about this. If you still think this exception should be propagated outside the test server, I'll gladly change it to do so.

~/src/requests$ py.test -k test_server_finishes_on_error tests 1 ↵ ✹ ✭testserver-refactor
========================================================================================== test session starts ==========================================================================================
platform darwin -- Python 2.7.10, pytest-2.8.7, py-1.4.31, pluggy-0.3.1
rootdir: /Users/brhiggins/src/requests, inifile:
plugins: cov-2.2.1, httpbin-0.2.0, mock-0.11.0
collected 319 items
tests/test_testserver.py F
=============================================================================================== FAILURES ================================================================================================
_____________________________________________________________________________ TestTestServer.test_server_finishes_on_error ______________________________________________________________________________
self = <tests.test_testserver.TestTestServer instance at 0x104eea098>
def test_server_finishes_on_error(self):
"""the server thread exits even if an exception exits the context manager"""
server = Server.basic_response_server()
try:
with server:
> raise Exception()
E Exception
tests/test_testserver.py:144: Exception
----------------------------------------------------------------------------------------- Captured stderr call ------------------------------------------------------------------------------------------
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/Users/brhiggins/src/requests/tests/testserver/server.py", line 68, in run
self._handle_requests(self.server_sock)
File "/Users/brhiggins/src/requests/tests/testserver/server.py", line 91, in _handle_requests
sock = self._accept_connection(server_sock)
File "/Users/brhiggins/src/requests/tests/testserver/server.py", line 107, in _accept_connection
raise e
error: (9, 'Bad file descriptor')
======================================================================= 318 tests deselected by '-ktest_server_finishes_on_error' =======================================================================
=============================================================================== 1 failed, 318 deselected in 0.09 seconds ================================================================================
~/src/requests$ py.test -k test_server_finishes_on_error tests 1 ↵ ✹ ✭testserver-refactor
========================================================================================== test session starts ==========================================================================================
platform darwin -- Python 2.7.10, pytest-2.8.7, py-1.4.31, pluggy-0.3.1
rootdir: /Users/brhiggins/src/requests, inifile:
plugins: cov-2.2.1, httpbin-0.2.0, mock-0.11.0
collected 319 items
tests/test_testserver.py F
=============================================================================================== FAILURES ================================================================================================
_____________________________________________________________________________ TestTestServer.test_server_finishes_on_error ______________________________________________________________________________
self = <tests.test_testserver.TestTestServer instance at 0x10749d050>
def test_server_finishes_on_error(self):
"""the server thread exits even if an exception exits the context manager"""
server = Server.basic_response_server()
try:
with server:
> raise Exception()
E Exception
tests/test_testserver.py:144: Exception
======================================================================= 318 tests deselected by '-ktest_server_finishes_on_error' =======================================================================
=============================================================================== 1 failed, 318 deselected in 0.10 seconds ================================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment