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:
parent
5020f1a831
commit
fd60b9c025
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue