[tahoe-lafs-trac-stream] [tahoe-lafs] #1301: use a mock decorator for test methods that return Deferreds

tahoe-lafs trac at tahoe-lafs.org
Wed Aug 21 15:34:53 UTC 2013


#1301: use a mock decorator for test methods that return Deferreds
-------------------------------------+-------------------------------------
     Reporter:  davidsarah           |      Owner:  somebody
         Type:  defect               |     Status:  closed
     Priority:  minor                |  Milestone:  undecided
    Component:  code                 |    Version:  1.8.1
   Resolution:  somebody else's      |   Keywords:  mock twisted deferred
  problem                            |  test
Launchpad Bug:                       |
-------------------------------------+-------------------------------------
Changes (by zooko):

 * status:  new => closed
 * resolution:   => somebody else's problem


Old description:

> Using the @mock.patch decorator for Trial test methods that return
> Deferreds does not work, because the patch will be undone when the method
> returns synchronously, not when the Deferred chain completes.
>
> We should add a decorator that can be used instead to
> [source:src/allmydata/test/common_util.py]. Something like:
> {{{
> def trialPatch(*patch_args, **patch_kwargs):
>     patcher = mock.patch(*patch_args, **patch_kwargs)
>     def decorator(f):
>         def inner(*test_args, **test_kwargs):
>             mock = patcher.__enter__()
>             def cleanup(res):
>                 patcher.__exit__()
>                 return res
>             test_args += (mock,)
>             d = f(*test_args, **test_kwargs)
>             d.addBoth(cleanup)
>             return d
>         return inner
>     return decorator
> }}}
>
> This would require mock 0.6, I think. Thanks to ducki2p and voidspace for
> this code.

New description:

 Using the @mock.patch decorator for Trial test methods that return
 Deferreds does not work, because the patch will be undone when the method
 returns synchronously, not when the Deferred chain completes.

 We should add a decorator that can be used instead to
 [source:src/allmydata/test/common_util.py]. Something like:
 {{{
 def trialPatch(*patch_args, **patch_kwargs):
     patcher = mock.patch(*patch_args, **patch_kwargs)
     def decorator(f):
         def inner(*test_args, **test_kwargs):
             mock = patcher.__enter__()
             def cleanup(res):
                 patcher.__exit__()
                 return res
             test_args += (mock,)
             d = f(*test_args, **test_kwargs)
             d.addBoth(cleanup)
             return d
         return inner
     return decorator
 }}}

 This would require mock 0.6, I think. Thanks to ducki2p and voidspace for
 this code.

--

Comment:

 Everyone who finds this ticket, please use the Twisted "patch" utility:
 http://twistedmatrix.com/documents/10.1.0/api/twisted.trial.unittest.TestCase.html#patch
 for your unit tests.

-- 
Ticket URL: <https://tahoe-lafs.org/trac/tahoe-lafs/ticket/1301#comment:5>
tahoe-lafs <https://tahoe-lafs.org>
secure decentralized storage


More information about the tahoe-lafs-trac-stream mailing list