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
* :rtype: None
* """
* self.__storage.instance = None # <<<<<<<<<<<<<<
* del self.__storage.instance # <<<<<<<<<<<<<<
*
* 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
* super(ThreadLocalSingleton, self).__init__(provides, *args, **kwargs)
@ -31176,7 +31176,7 @@ static PyObject *__pyx_pf_19dependency_injector_9providers_20ThreadLocalSingleto
}
/* "dependency_injector/providers.pyx":1813
* self.__storage.instance = None
* del self.__storage.instance
*
* cpdef object _provide(self, tuple args, dict kwargs): # <<<<<<<<<<<<<<
* """Return single instance."""
@ -31461,7 +31461,7 @@ static PyObject *__pyx_f_19dependency_injector_9providers_20ThreadLocalSingleton
}
/* "dependency_injector/providers.pyx":1813
* self.__storage.instance = None
* del self.__storage.instance
*
* cpdef object _provide(self, tuple args, dict kwargs): # <<<<<<<<<<<<<<
* """Return single instance."""

View File

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

View File

@ -347,7 +347,7 @@ class _BaseSingletonTestCase(object):
provider.reset()
instance2 = provider()
self.assertIsInstance(instance1, object)
self.assertIsInstance(instance2, object)
self.assertIsNot(instance1, instance2)
@ -397,6 +397,19 @@ class ThreadLocalSingletonTests(_BaseSingletonTestCase, unittest.TestCase):
repr(Example),
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,
unittest.TestCase):