From: <zy...@us...> - 2010-06-26 04:03:29
|
Revision: 7071 http://jython.svn.sourceforge.net/jython/?rev=7071&view=rev Author: zyasoft Date: 2010-06-26 04:03:22 +0000 (Sat, 26 Jun 2010) Log Message: ----------- Added test code to verify that proxies are not leaked by hard refs in PyType#class_to_test. This will cause the regrtest to fail until the patch in issue 1522 is also applied (other issues apply in its integration unfortunately). Modified Paths: -------------- trunk/jython/Lib/test/test_jy_internals.py trunk/jython/tests/java/javatests/TestSupport.java Modified: trunk/jython/Lib/test/test_jy_internals.py =================================================================== --- trunk/jython/Lib/test/test_jy_internals.py 2010-06-25 14:22:08 UTC (rev 7070) +++ trunk/jython/Lib/test/test_jy_internals.py 2010-06-26 04:03:22 UTC (rev 7071) @@ -1,6 +1,7 @@ """ test some jython internals """ +import gc import unittest import time from test.test_support import run_suite @@ -9,10 +10,59 @@ import jarray from org.python.core import Py +from org.python.util import PythonInterpreter +from javatests.TestSupport import getField from java.sql import Date, Time, Timestamp import datetime +class MemoryLeakTests(unittest.TestCase): + + 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()) + + class WeakIdentityMapTests(unittest.TestCase): def test_functionality(self): @@ -249,6 +299,7 @@ suite_add(IdTest) suite_add(FrameTest) suite_add(ModuleTest) + suite_add(MemoryLeakTests) run_suite(test_suite) if __name__ == "__main__": Modified: trunk/jython/tests/java/javatests/TestSupport.java =================================================================== --- trunk/jython/tests/java/javatests/TestSupport.java 2010-06-25 14:22:08 UTC (rev 7070) +++ trunk/jython/tests/java/javatests/TestSupport.java 2010-06-26 04:03:22 UTC (rev 7071) @@ -1,6 +1,9 @@ //Copyright (c) Corporation for National Research Initiatives package javatests; +import java.lang.reflect.Field; +import java.lang.NoSuchFieldException; + /** * @author updikca1 */ @@ -46,5 +49,16 @@ assertThat( !a.equals(b), message + "[not a.equals(b) failed]"); assertThat( !b.equals(a), message + "[not b.equals(a) failed]"); } + + public static Field getField(Class cls, String name) { + try { + Field f = cls.getDeclaredField(name); + f.setAccessible(true); + return f; + } catch (NoSuchFieldException ex) { + throw new RuntimeException(ex); + } + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |