Пример #1
0
    def test_class_to_test_weakness(self):
        # regrtest for bug 1522, adapted from test code submitted by Matt Brinkley

        # work around the fact that we can't look at PyType directly
        # by using this helper function that reflects on PyType (and
        # demonstrates here that it's the same as the builtin function
        # `type`!)
        class_to_type_map = getField(type, 'class_to_type').get(None)

        def make_clean():
            # gc a few times just to be really sure, since in this
            # case we don't really care if it takes a few cycles of GC
            # for the garbage to be reached
            gc.collect()
            time.sleep(0.1)
            gc.collect()
            time.sleep(0.5)
            gc.collect()

        def create_proxies():
            pi = PythonInterpreter()
            pi.exec("""
from java.lang import Comparable

class Dog(Comparable):
    def compareTo(self, o):
        return 0
    def bark(self):
        return 'woof woof'

Dog().bark()
""")
            pi.cleanup();
            make_clean()
    
        # get to steady state first, then verify we don't create new proxies
        for i in xrange(2):
            create_proxies()
        start_size = class_to_type_map.size()
        for i in xrange(5):
            create_proxies()
        make_clean()
        self.assertEqual(start_size, class_to_type_map.size())
Пример #2
0
    def test_class_to_test_weakness(self):
        # regrtest for bug 1522, adapted from test code submitted by Matt Brinkley

        # work around the fact that we can't look at PyType directly
        # by using this helper function that reflects on PyType (and
        # demonstrates here that it's the same as the builtin function
        # `type`!)
        class_to_type_map = getField(type, 'class_to_type').get(None)

        def make_clean():
            # gc a few times just to be really sure, since in this
            # case we don't really care if it takes a few cycles of GC
            # for the garbage to be reached
            gc.collect()
            time.sleep(0.1)
            gc.collect()
            time.sleep(0.5)
            gc.collect()

        def create_proxies():
            pi = PythonInterpreter()
            pi.exec("""
from java.lang import Comparable

class Dog(Comparable):
    def compareTo(self, o):
        return 0
    def bark(self):
        return 'woof woof'

Dog().bark()
""")
            make_clean()
    
        # get to steady state first, then verify we don't create new proxies
        for i in xrange(2):
            create_proxies()
        start_size = class_to_type_map.size()
        for i in xrange(5):
            create_proxies()
        make_clean()
        self.assertEqual(start_size, class_to_type_map.size())
Пример #3
0
    def test_class_to_test_weakness(self):
        # regrtest for bug 1522, adapted from test code submitted by Matt Brinkley

        # work around the fact that we can't look at PyType directly
        # by using this helper function that reflects on PyType (and
        # demonstrates here that it's the same as the builtin function
        # `type`!)
        class_to_type_map = getField(type, 'class_to_type').get(None)

        def create_proxies():
            pi = PythonInterpreter()
            pi.exec("""
from java.lang import Comparable

class Dog(Comparable):
    def compareTo(self, o):
        return 0
    def bark(self):
        return 'woof woof'

Dog().bark()
""")
        # get to steady state first, then verify we don't create new proxies
        for i in xrange(2):
            create_proxies()
        # Ensure the reaper thread can run and clear out weak refs, so
        # use this supporting function
        test_support.gc_collect()
        # Given that taking the len (or size()) of Guava weak maps is
        # eventually consistent, we should instead take a len of its
        # keys.
        start_size = len(list(class_to_type_map))
        for i in xrange(5):
            create_proxies()
        test_support.gc_collect()
        self.assertEqual(start_size, len(list(class_to_type_map)))