]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Make CassetteContextDecorator decorator produce reentrant functions.
authorIvan Malison <imalison@uber.com>
Sun, 10 May 2015 06:12:51 +0000 (23:12 -0700)
committerIvan Malison <imalison@uber.com>
Sun, 10 May 2015 06:14:00 +0000 (23:14 -0700)
Closes #150.

tests/unit/test_cassettes.py
vcr/cassette.py

index bc7d2437812015c5267061050ac71591f7112c45..9b573bf2c08b8e43d46b6ce3bb8dc643993a7b9d 100644 (file)
@@ -201,3 +201,19 @@ def test_custom_patchers():
         assert issubclass(Test.attribute, VCRHTTPSConnection)
         assert VCRHTTPSConnection is not Test.attribute
         assert  Test.attribute is old_attribute
+
+
+def test_use_cassette_decorated_functions_are_reentrant():
+    info = {"second": False}
+    original_conn = httplib.HTTPConnection
+    @Cassette.use('whatever', inject=True)
+    def test_function(cassette):
+        if info['second']:
+            assert httplib.HTTPConnection is not info['first_conn']
+        else:
+            info['first_conn'] = httplib.HTTPConnection
+            info['second'] = True
+            test_function()
+            assert httplib.HTTPConnection is info['first_conn']
+    test_function()
+    assert httplib.HTTPConnection is original_conn
index c95f0695213048f67c6e8f70c2699bb894b99541..c01a763a2f08c4005bffc3177c1e8a77c7effe5e 100644 (file)
@@ -50,6 +50,14 @@ class CassetteContextDecorator(object):
             cassette._save()
 
     def __enter__(self):
+        # This assertion is here to prevent the dangerous behavior
+        # that would result from forgetting about a __finish before
+        # completing it.
+        # How might this condition be met? Here is an example:
+        # context_decorator = Cassette.use('whatever')
+        # with context_decorator:
+        #     with context_decorator:
+        #         pass
         assert self.__finish is None, "Cassette already open."
         path, kwargs = self._args_getter()
         self.__finish = self._patch_generator(self.cls.load(path, **kwargs))
@@ -61,7 +69,11 @@ class CassetteContextDecorator(object):
 
     @wrapt.decorator
     def __call__(self, function, instance, args, kwargs):
-        with self as cassette:
+        # This awkward cloning thing is done to ensure that decorated
+        # functions are reentrant. Reentrancy is required for thread
+        # safety and the correct operation of recursive functions.
+        clone = type(self)(self.cls, self._args_getter)
+        with clone as cassette:
             if cassette.inject:
                 return function(cassette, *args, **kwargs)
             else: