Michael Merickel
2011-07-15 eff1cb657b787771aeb2ed0be28c3709ae019fc3
Modified tests to use global_registries.remove() instead of relying on gc.
4 files modified
48 ■■■■■ changed files
pyramid/tests/test_config.py 9 ●●●●● patch | view | raw | blame | history
pyramid/tests/test_scripting.py 1 ●●●● patch | view | raw | blame | history
pyramid/tests/test_util.py 19 ●●●●● patch | view | raw | blame | history
pyramid/util.py 19 ●●●● patch | view | raw | blame | history
pyramid/tests/test_config.py
@@ -687,16 +687,15 @@
        self.assertEqual(pyramid.config.global_registries.last, app.registry)
        self.assertEqual(len(subscriber), 1)
        self.assertTrue(IApplicationCreated.providedBy(subscriber[0]))
        pyramid.config.global_registries.empty()
    def test_global_registries_empty(self):
        import gc
        from pyramid.config import global_registries
        gc.collect() # force weakref updates
        self.assertEqual(global_registries.last, None)
    def test_global_registries(self):
        import gc
        from pyramid.config import global_registries
        global_registries.empty()
        config1 = self._makeOne()
        config1.make_wsgi_app()
        self.assertEqual(global_registries.last, config1.registry)
@@ -705,9 +704,9 @@
        self.assertEqual(global_registries.last, config2.registry)
        self.assertEqual(list(global_registries),
                         [config1.registry, config2.registry])
        del config2
        gc.collect() # force weakref updates
        global_registries.remove(config2.registry)
        self.assertEqual(global_registries.last, config1.registry)
        global_registries.empty()
    def test_include_with_dotted_name(self):
        from pyramid import tests
pyramid/tests/test_scripting.py
@@ -100,6 +100,7 @@
        request = self._callFUT('/hello')
        self.assertEqual(request.environ['path'], '/hello')
        self.assertEqual(request.registry, registry)
        global_registries.empty()
class Dummy:
    pass
pyramid/tests/test_util.py
@@ -216,28 +216,35 @@
        self.assertEqual(wos.last, reg)
    def test_weakref_removal(self):
        import gc
        wos = self._makeOne()
        reg = Dummy()
        wos.add(reg)
        del reg
        gc.collect() # force gc
        wos.remove(reg)
        self.assertEqual(len(wos), 0)
        self.assertEqual(list(wos), [])
        self.assertEqual(wos.last, None)
    def test_last_updated(self):
        import gc
        wos = self._makeOne()
        reg = Dummy()
        reg2 = Dummy()
        wos.add(reg)
        wos.add(reg2)
        del reg2
        gc.collect() # force gc
        wos.remove(reg2)
        self.assertEqual(len(wos), 1)
        self.assertEqual(list(wos), [reg])
        self.assertEqual(wos.last, reg)
    def test_empty(self):
        wos = self._makeOne()
        reg = Dummy()
        reg2 = Dummy()
        wos.add(reg)
        wos.add(reg2)
        wos.empty()
        self.assertEqual(len(wos), 0)
        self.assertEqual(list(wos), [])
        self.assertEqual(wos.last, None)
class Dummy(object):
    pass
pyramid/util.py
@@ -158,17 +158,26 @@
        self._order = []
    def add(self, item):
        """ Add a registry to the set."""
        """ Add an item to the set."""
        oid = id(item)
        if oid in self._items:
            return
        def cleanup(ref):
            del self._items[oid]
            self._order.remove(oid)
        ref = weakref.ref(item, cleanup)
        ref = weakref.ref(item, lambda x: self.remove(item))
        self._items[oid] = ref
        self._order.append(oid)
    def remove(self, item):
        """ Remove an item from the set."""
        oid = id(item)
        if oid in self._items:
            del self._items[oid]
            self._order.remove(oid)
    def empty(self):
        """ Clear all objects from the set."""
        self._items = {}
        self._order = []
    def __len__(self):
        return len(self._order)