mock: don't stop a patcher when the test function returns, if it returned a Twisted Deferred

What steps will reproduce the problem?
1. Write a test function to test some asynchronous code in Twisted, such that 
your test function returns a Deferred, and once the test is finished then that 
Deferred gets fired.
2. Use a mock.patch() to inject some code for this test.
3. Run "trial" to execute the test.

What is the expected output? What do you see instead?

I expect the patch to stay in place until the test is finished. Instead, the 
patch is stopped when the test function returns.

What version of the product are you using? On what operating system?

Mock 0.7.2 on Linux

Please provide any additional information below.

Attached is some code that I wrote which overrides some of the guts of mock.py 
to make it do the right thing for functions that return Deferreds (as well as 
continuing to do the right thing for functions that return values of another 
type). This code actually works and makes my current unit test do the right 
thing, but it is potentially a bit too weird of a thing to add to the 
Tahoe-LAFS source code tree. For one thing it could easily break when new 
versions of mock are used with Tahoe-LAFS. Would you consider adopting this 
patch into mock proper?

Here is the salient diff that would make mock leave a patch in place until a 
Deferred is finished:

--- mock-0.7.2/mock.py  2011-06-16 23:57:22.207717503 -0600
+++ mock-0.7.2-zookopatch/mock.py       2011-06-16 23:59:04.128717498 -0600
@@ -68,6 +68,13 @@
     # Python 3
     long = int

+Deferred = None
+try:
+    from twisted.internet import defer
+    Deferred = defer.Deferred
+except ImportError:
+    pass
+
 inPy3k = sys.version_info[0] == 3

 if inPy3k:
@@ -562,15 +569,27 @@
                 if patching.new is DEFAULT:
                     extra_args.append(arg)
             args += tuple(extra_args)
-            try:
-                return func(*args, **keywargs)
-            finally:
+            def cleanup(res):
                 for patching in reversed(getattr(patched, 'patchings', [])):
                     patching.__exit__()
+                return res
+            singleton = {}
+            retval = singleton
+            try:
+                retval = func(*args, **keywargs)
+            except:
+                cleanup(None)
+                raise
+            if Deferred is None or not isinstance(retval, Deferred):
+                return cleanup(retval)
+            retval.addBoth(cleanup)
+            return retval

         patched.patchings = [self]
         if hasattr(func, 'func_code'):
             # not in Python 3
-            patched.compat_co_firstlineno = getattr(func, 
"compat_co_firstlineno",
-                                                    
func.func_code.co_firstlineno)
+            patched.compat_co_firstlineno = getattr(
+                func, "compat_co_firstlineno",
+                func.func_code.co_firstlineno
+            )
         return patched

Original issue reported on code.google.com by zoo...@gmail.com on 17 Jun 2011 at 6:00

Attachments:

About this issue

  • Original URL
  • State: closed
  • Created 12 years ago
  • Comments: 20

Most upvoted comments