Bugfix thread local singleton reset (#218)

* Fix issue causing ThreadLocalSingleton provider to return None after reset

* Add test for ThreadLocalSingleton provider reset functionality
This commit is contained in:
Jeroen Rietveld 2019-03-22 03:04:20 +01:00 committed by Roman Mogylatov
parent 69602dc3de
commit 72d5741ece
3 changed files with 19 additions and 6 deletions

View File

@ -31149,11 +31149,11 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_20ThreadLocalSingleto
/* "dependency_injector/providers.pyx":1811 /* "dependency_injector/providers.pyx":1811
* :rtype: None * :rtype: None
* """ * """
* self.__storage.instance = None # <<<<<<<<<<<<<< * del self.__storage.instance # <<<<<<<<<<<<<<
* *
* cpdef object _provide(self, tuple args, dict kwargs): * cpdef object _provide(self, tuple args, dict kwargs):
*/ */
if (__Pyx_PyObject_SetAttrStr(__pyx_v_self->__pyx___storage, __pyx_n_s_instance, Py_None) < 0) __PYX_ERR(1, 1811, __pyx_L1_error) if (__Pyx_PyObject_DelAttrStr(__pyx_v_self->__pyx___storage, __pyx_n_s_instance) < 0) __PYX_ERR(1, 1811, __pyx_L1_error)
/* "dependency_injector/providers.pyx":1806 /* "dependency_injector/providers.pyx":1806
* super(ThreadLocalSingleton, self).__init__(provides, *args, **kwargs) * super(ThreadLocalSingleton, self).__init__(provides, *args, **kwargs)
@ -31176,7 +31176,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_20ThreadLocalSingleto
} }
/* "dependency_injector/providers.pyx":1813 /* "dependency_injector/providers.pyx":1813
* self.__storage.instance = None * del self.__storage.instance
* *
* cpdef object _provide(self, tuple args, dict kwargs): # <<<<<<<<<<<<<< * cpdef object _provide(self, tuple args, dict kwargs): # <<<<<<<<<<<<<<
* """Return single instance.""" * """Return single instance."""
@ -31461,7 +31461,7 @@ static PyObject *__pyx_f_19dependency_injector_9providers_20ThreadLocalSingleton
} }
/* "dependency_injector/providers.pyx":1813 /* "dependency_injector/providers.pyx":1813
* self.__storage.instance = None * del self.__storage.instance
* *
* cpdef object _provide(self, tuple args, dict kwargs): # <<<<<<<<<<<<<< * cpdef object _provide(self, tuple args, dict kwargs): # <<<<<<<<<<<<<<
* """Return single instance.""" * """Return single instance."""

View File

@ -1808,7 +1808,7 @@ cdef class ThreadLocalSingleton(BaseSingleton):
:rtype: None :rtype: None
""" """
self.__storage.instance = None del self.__storage.instance
cpdef object _provide(self, tuple args, dict kwargs): cpdef object _provide(self, tuple args, dict kwargs):
"""Return single instance.""" """Return single instance."""

View File

@ -347,7 +347,7 @@ class _BaseSingletonTestCase(object):
provider.reset() provider.reset()
instance2 = provider() instance2 = provider()
self.assertIsInstance(instance1, object) self.assertIsInstance(instance2, object)
self.assertIsNot(instance1, instance2) self.assertIsNot(instance1, instance2)
@ -397,6 +397,19 @@ class ThreadLocalSingletonTests(_BaseSingletonTestCase, unittest.TestCase):
repr(Example), repr(Example),
hex(id(provider)))) hex(id(provider))))
def test_reset(self):
provider = providers.ThreadLocalSingleton(Example)
instance1 = provider()
self.assertIsInstance(instance1, Example)
provider.reset()
instance2 = provider()
self.assertIsInstance(instance2, Example)
self.assertIsNot(instance1, instance2)
class DelegatedThreadLocalSingletonTests(_BaseSingletonTestCase, class DelegatedThreadLocalSingletonTests(_BaseSingletonTestCase,
unittest.TestCase): unittest.TestCase):