Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Final attempt to solve sporadic test failures.

tearDownClass is not called if setUpClass throws an exception, in our case
this means that LiveServerTestCase leaks LiveServerThread sockets if the
test happens to be skipped later on, and AdminSeleniumWebDriverTestCase
doesn't close it's already open browser window. To prevent this leakage
we catch errors where needed and manually call _tearDownClassInternal.
_tearDownClassInternal should be written as defensively as possible since
it is not allowed to make any assumptions on how far setUpClass got.

This patch should fix the sporadic "Address already in use"-errors on jenkins
and also the "This code isn't under transaction management"-error for sqlite
(also just on jenkins).

After discussion with koniiiik, jezdez, kmtracey, tos9, lifeless, nedbat and
voidspace it was decided that this is the safest approach (thanks to everyone
for their comments and help). Manually calling tearDownClass was shut down
cause we don't know how our users override our classes.

This is a private and very specialized API on purpose and should not be used
without a strong reason!

This patch partially reverts the earlier attempts to fix those issues,
namely:
	2fa0dd7 and
	3c5775d

Final note: If this patch breaks in a later version of Django, please be
very careful on how you fix it, you might not see test failures locally.
That said, this patch hopefully doesn't produce even more failures.
  • Loading branch information...
commit 73a610d2a81bc3bf2d3834786b2458bc85953ed0 1 parent 8e04ad9
@apollo13 apollo13 authored
Showing with 14 additions and 12 deletions.
  1. +7 −11 django/contrib/admin/tests.py
  2. +7 −1 django/test/testcases.py
View
18 django/contrib/admin/tests.py
@@ -22,22 +22,18 @@ def setUpClass(cls):
if not os.environ.get('DJANGO_SELENIUM_TESTS', False):
raise SkipTest('Selenium tests not requested')
try:
- webdriver_class = import_by_path(cls.webdriver_class)
- except ImportError as e:
- raise SkipTest('Selenium webdriver "%s" not installed: %s'
- % (cls.webdriver_class, str(e)))
- super(AdminSeleniumWebDriverTestCase, cls).setUpClass()
- try:
- cls.selenium = webdriver_class()
+ cls.selenium = import_by_path(cls.webdriver_class)()
except Exception as e:
- raise SkipTest('Selenium webdriver "%s" not operational: %s'
- % (cls.webdriver_class, str(e)))
+ raise SkipTest('Selenium webdriver "%s" not installed or not '
+ 'operational: %s' % (cls.webdriver_class, str(e)))
+ # This has to be last to ensure that resources are cleaned up properly!
+ super(AdminSeleniumWebDriverTestCase, cls).setUpClass()
@classmethod
- def tearDownClass(cls):
+ def _tearDownClassInternal(cls):
+ super(AdminSeleniumWebDriverTestCase, cls)._tearDownClassInternal()
if hasattr(cls, 'selenium'):
cls.selenium.quit()
- super(AdminSeleniumWebDriverTestCase, cls).tearDownClass()
def wait_until(self, callback, timeout=10):
"""
View
8 django/test/testcases.py
@@ -1125,12 +1125,15 @@ def setUpClass(cls):
# Wait for the live server to be ready
cls.server_thread.is_ready.wait()
if cls.server_thread.error:
+ # Clean up behind ourselves, since tearDownClass won't get called in
+ # case of errors.
+ cls._tearDownClassInternal()
raise cls.server_thread.error
super(LiveServerTestCase, cls).setUpClass()
@classmethod
- def tearDownClass(cls):
+ def _tearDownClassInternal(cls):
# There may not be a 'server_thread' attribute if setUpClass() for some
# reasons has raised an exception.
if hasattr(cls, 'server_thread'):
@@ -1143,4 +1146,7 @@ def tearDownClass(cls):
and conn.settings_dict['NAME'] == ':memory:'):
conn.allow_thread_sharing = False
+ @classmethod
+ def tearDownClass(cls):
+ cls._tearDownClassInternal()
super(LiveServerTestCase, cls).tearDownClass()
Please sign in to comment.
Something went wrong with that request. Please try again.