Esempio n. 1
0
 def test_access_call(self):
     from java.util import ArrayList
     start_memory = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
     obj = Object()
     for i in xrange(0,iterations,1):
         b = obj.hashCode()
     end_memory = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
     if start_memory*failure_threshold < end_memory:
         percent = end_memory*100/start_memory - 100
         raise Exception('Called methods are leaking memory, process size increased %d%% over %d iterations.' % (percent, iterations)) 
Esempio n. 2
0
 def test_access_call(self):
     from java.util import ArrayList
     start_memory = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
     obj = Object()
     for i in xrange(0, iterations, 1):
         b = obj.hashCode()
     end_memory = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
     if start_memory * failure_threshold < end_memory:
         percent = end_memory * 100 / start_memory - 100
         raise Exception(
             'Called methods are leaking memory, process size increased %d%% over %d iterations.'
             % (percent, iterations))
Esempio n. 3
0
class TestMethodMemory(unittest.TestCase):

    def setUp(self):
        from java.lang import Object
        self.obj = Object()

    def test_repeated_call(self):
        method = self.obj.hashCode
        test_leak(self, lambda:method(), "Called method")

    def test_access_call(self):
        test_leak(self, lambda:self.obj.hashCode(), "Access and call method")

    def test_access_no_call(self):
        test_leak(self, lambda:self.obj.hashCode, "Access method")
    def test_object_methods_implemented(self):
        from java.lang import Object

        class Implemented(dynamic_proxy(TP.Adder)):
            def toString(self):
                return "Override " + Object.toString(self)

            def hashCode(self):
                return Object.hashCode(self) + 1

            def equals(self, other):
                return True

        a1, a2 = Implemented(), Implemented()

        ts = cast(TP.Adder, a1).toString()
        self.assertRegexpMatches(ts, r"^Override .*\$Proxy.*@")
        self.assertEqual(ts, str(a1))
        self.assertNotEqual(str(a1), str(a2))

        self.assertTrue(a1.equals(a1))
        self.assertTrue(a1.equals(a2))
        self.assertEqual(Object.hashCode(a1) + 1, a1.hashCode())
Esempio n. 5
0
class TestPythonRefCounts(unittest.TestCase):
    def setUp(self):
        from java.lang import Object
        self.obj = Object()

    def test_method_binding(self):
        refcount1 = sys.gettotalrefcount()
        result = self.obj.hashCode
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_method_call(self):
        # First call to hashCode will cache info about the hashCode method
        self.obj.hashCode()
        refcount1 = sys.gettotalrefcount()
        result = self.obj.hashCode()
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_package_import(self):
        refcount1 = sys.gettotalrefcount()
        import java.lang as result
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_class_import(self):
        refcount1 = sys.gettotalrefcount()
        from java.lang import Object as result
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_construction(self):
        from java.lang import Object
        refcount1 = sys.gettotalrefcount()
        result = Object()
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_field_access(self):
        from java.lang import System
        # First time will cache info about the PrintStream class
        result = System.out
        del result
        refcount1 = sys.gettotalrefcount()
        result = System.out
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_array_creation(self):
        from jep import jarray
        from java.lang import Object
        refcount1 = sys.gettotalrefcount()
        result = jarray(1, Object)
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_array_assignment(self):
        from jep import jarray
        from java.lang import Object
        arr = jarray(1, Object)
        refcount1 = sys.gettotalrefcount()
        arr[0] = self.obj
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_array_access(self):
        from jep import jarray
        from java.lang import Object
        arr = jarray(1, Object)
        arr[0] = self.obj
        refcount1 = sys.gettotalrefcount()
        result = arr[0]
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_number_compare(self):
        x = 5
        from java.lang import Integer
        y = Integer(9)
        refcount1 = sys.gettotalrefcount()
        result = x < y
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_list_setslice(self):
        from java.util import ArrayList
        jlist = ArrayList()
        for i in range(5):
            jlist.add(i)
        refcount1 = sys.gettotalrefcount()
        jlist[2:4] = [7, 19]
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_dir_class(self):
        from java.util import ArrayList
        refcount1 = sys.gettotalrefcount()
        result = dir(ArrayList)
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)
 def hashCode(self):
     return Object.hashCode(self) + 1
Esempio n. 7
0
class TestPythonRefCounts(unittest.TestCase):

    def setUp(self):
        from java.lang import Object
        self.obj = Object()

    def test_method_binding(self):
        refcount1 = sys.gettotalrefcount()
        result = self.obj.hashCode
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_method_call(self):
        # First call to hashCode will cache info about the hashCode method
        self.obj.hashCode()
        refcount1 = sys.gettotalrefcount()
        result = self.obj.hashCode()
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_package_import(self):
        refcount1 = sys.gettotalrefcount()
        import java.lang as result
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_class_import(self):
        refcount1 = sys.gettotalrefcount()
        from java.lang import Object as result
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_construction(self):
        from java.lang import Object
        refcount1 = sys.gettotalrefcount()
        result = Object()
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_field_access(self):
        from java.lang import System
        # First time will cache info about the PrintStream class
        result = System.out
        del result
        refcount1 = sys.gettotalrefcount()
        result = System.out
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_array_creation(self):
        from jep import jarray
        from java.lang import Object
        refcount1 = sys.gettotalrefcount()
        result = jarray(1, Object)
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_array_assignment(self):
        from jep import jarray
        from java.lang import Object
        arr = jarray(1, Object)
        refcount1 = sys.gettotalrefcount()
        arr[0] = self.obj
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_array_access(self):
        from jep import jarray
        from java.lang import Object
        arr = jarray(1, Object)
        arr[0] = self.obj
        refcount1 = sys.gettotalrefcount()
        result = arr[0]
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_number_compare(self):
        x = 5
        from java.lang import Integer
        y = Integer(9)
        refcount1 = sys.gettotalrefcount()
        result = x < y
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_list_setslice(self):
        from java.util import ArrayList
        jlist = ArrayList()
        for i in range(5):
            jlist.add(i)
        refcount1 = sys.gettotalrefcount()
        jlist[2:4] = [7, 19]
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)

    def test_dir_class(self):
        from java.util import ArrayList
        refcount1 = sys.gettotalrefcount()
        result = dir(ArrayList)
        del result
        refcount2 = sys.gettotalrefcount()
        self.assertEquals(refcount1, refcount2 - 1)