mockpatch: fix a potential race condition

If start() fails at some point, we may have a partial mock applied, and
no cleanup function. So let's start by adding the cleanup function
before starting anything.

Change-Id: Ic41d65b2da826f982ff020ebd269b7f749dc63a4
This commit is contained in:
Julien Danjou 2015-03-12 14:31:54 +01:00
parent 5020f1a831
commit fd60b9c025
1 changed files with 3 additions and 3 deletions

View File

@ -31,8 +31,8 @@ class PatchObject(fixtures.Fixture):
def setUp(self):
super(PatchObject, self).setUp()
_p = mock.patch.object(self.obj, self.attr, self.new, **self.kwargs)
self.mock = _p.start()
self.addCleanup(_p.stop)
self.mock = _p.start()
class Patch(fixtures.Fixture):
@ -46,8 +46,8 @@ class Patch(fixtures.Fixture):
def setUp(self):
super(Patch, self).setUp()
_p = mock.patch(self.obj, self.new, **self.kwargs)
self.mock = _p.start()
self.addCleanup(_p.stop)
self.mock = _p.start()
class Multiple(fixtures.Fixture):
@ -76,5 +76,5 @@ class Multiple(fixtures.Fixture):
def setUp(self):
super(Multiple, self).setUp()
_p = mock.patch.multiple(self.obj, **self.kwargs)
self.mock = _p.start()
self.addCleanup(_p.stop)
self.mock = _p.start()