Pārlūkot izejas kodu

Do a better job cleaning up state between tests

Two problems are fixed here. The first dealt with the faking of a PyPy
version; unfortunately on my system this caused us to completely skip
the database backend tests because we leaked the pypy_version_info
attribute into later tests, and these tests are conditional on this
attribute not being defined. Fix this by deleting the attribute
completely if it was not defined when entering the context manager.

The second one is more subtle- because request is a Context() object,
which is a thread local, setting the chord object to a value persisted
it for all remaining tests. Clear out the context at the end of the
test, and set it to a more realistic value.
Dan McGee 13 gadi atpakaļ
vecāks
revīzija
b0fc8fe00d
2 mainītis faili ar 15 papildinājumiem un 1 dzēšanām
  1. 3 1
      celery/tests/test_task/__init__.py
  2. 12 0
      celery/tests/utils.py

+ 3 - 1
celery/tests/test_task/__init__.py

@@ -335,6 +335,7 @@ class TestCeleryTasks(unittest.TestCase):
         request.foo = 32
         self.assertEqual(request.get("foo"), 32)
         self.assertEqual(request.get("bar", 36), 36)
+        request.clear()
 
     def test_task_class_repr(self):
         task = self.createTaskCls("T1", "c.unittest.t.repr")
@@ -343,9 +344,10 @@ class TestCeleryTasks(unittest.TestCase):
     def test_after_return(self):
         task = self.createTaskCls("T1", "c.unittest.t.after_return")()
         task.backend = Mock()
-        task.request.chord = 123
+        task.request.chord = {}
         task.after_return("SUCCESS", 1.0, "foobar", (), {}, None)
         task.backend.on_chord_part_return.assert_called_with(task)
+        task.request.clear()
 
     def test_send_task_sent_event(self):
         T1 = self.createTaskCls("T1", "c.unittest.t.t1")

+ 12 - 0
celery/tests/utils.py

@@ -286,6 +286,7 @@ def patch(module, name, mocked):
 @contextmanager
 def platform_pyimp(replace=None):
     import platform
+    has_prev = hasattr(platform, "python_implementation")
     prev = getattr(platform, "python_implementation", None)
     if replace:
         platform.python_implementation = replace
@@ -297,6 +298,11 @@ def platform_pyimp(replace=None):
     yield
     if prev is not None:
         platform.python_implementation = prev
+    if not has_prev:
+        try:
+            delattr(platform, "python_implementation")
+        except AttributeError:
+            pass
 
 
 @contextmanager
@@ -308,6 +314,7 @@ def sys_platform(value):
 
 @contextmanager
 def pypy_version(value=None):
+    has_prev = hasattr(sys, "pypy_version_info")
     prev = getattr(sys, "pypy_version_info", None)
     if value:
         sys.pypy_version_info = value
@@ -319,6 +326,11 @@ def pypy_version(value=None):
     yield
     if prev is not None:
         sys.pypy_version_info = prev
+    if not has_prev:
+        try:
+            delattr(sys, "pypy_version_info")
+        except AttributeError:
+            pass
 
 
 @contextmanager