<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals , absolute_import <EOL> import gc <EOL> import unittest <EOL> from weakref import ref <EOL> from py4j . finalizer import ThreadSafeFinalizer , Finalizer , clear_finalizers <EOL> def deleted ( accumulator , id ) : <EOL> print ( id ) <EOL> accumulator . acc += <NUM_LIT:1> <EOL> class Accumulator ( object ) : <EOL> def __init__ ( self ) : <EOL> self . acc = <NUM_LIT:0> <EOL> class AClass ( object ) : <EOL> def __init__ ( self , id , acc ) : <EOL> self . id = id <EOL> self . acc = acc <EOL> ThreadSafeFinalizer . add_finalizer ( <EOL> id , <EOL> ref ( self , lambda wr , i = self . id , a = self . acc : deleted ( a , i ) ) ) <EOL> class AClass2 ( object ) : <EOL> def __init__ ( self , id , acc ) : <EOL> self . id = id <EOL> self . acc = acc <EOL> Finalizer . add_finalizer ( <EOL> id , <EOL> ref ( self , lambda wr , i = self . id , a = self . acc : deleted ( a , i ) ) ) <EOL> class JavaObjecTest ( object ) : <EOL> def __init__ ( self , id , acc ) : <EOL> self . id = id <EOL> self . acc = acc <EOL> self . methods = [ ] <EOL> ThreadSafeFinalizer . add_finalizer ( <EOL> id , <EOL> ref ( self , lambda wr , i = self . id , a = self . acc : deleted ( a , i ) ) ) <EOL> class JavaMemberTest ( object ) : <EOL> def __init__ ( self , name , container ) : <EOL> self . name = name <EOL> self . container = container <EOL> class TestThreadSafeFinalizer ( unittest . TestCase ) : <EOL> def tearDown ( self ) : <EOL> clear_finalizers ( True ) <EOL> def work1 ( self , acc ) : <EOL> a1 = AClass ( <NUM_LIT:1> , acc ) <EOL> a2 = AClass ( <NUM_LIT:2> , acc ) <EOL> def work1b ( self , acc ) : <EOL> a1 = AClass ( <NUM_LIT:1> , acc ) <EOL> a2 = AClass ( <NUM_LIT:2> , acc ) <EOL> a1 . foo = a2 <EOL> a2 . foo = a1 <EOL> def work2 ( self , acc ) : <EOL> a1 = AClass ( <NUM_LIT:1> , acc ) <EOL> a2 = AClass ( <NUM_LIT:2> , acc ) <EOL> return a1 <EOL> def testFinalizer ( self ) : <EOL> acc = Accumulator ( ) <EOL> self . work1 ( acc ) <EOL> self . assertEqual ( <NUM_LIT:2> , acc . acc ) <EOL> self . work2 ( acc ) <EOL> self . assertEqual ( <NUM_LIT:4> , acc . acc ) <EOL> def work_circ ( self , acc ) : <EOL> jobj = JavaObjecTest ( <NUM_LIT:1> , acc ) <EOL> jmem1 = JavaMemberTest ( "<STR_LIT>" , jobj ) <EOL> jobj . methods . append ( jmem1 ) <EOL> def testCircularReference2 ( self ) : <EOL> acc = Accumulator ( ) <EOL> self . work_circ ( acc ) <EOL> gc . collect ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , acc . acc ) <EOL> def testCleanUp ( self ) : <EOL> acc = Accumulator ( ) <EOL> a1 = self . work2 ( acc ) <EOL> self . assertEqual ( <NUM_LIT:1> , acc . acc ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( ThreadSafeFinalizer . finalizers ) ) <EOL> clear_finalizers ( False ) <EOL> self . assertEqual ( <NUM_LIT:1> , acc . acc ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( ThreadSafeFinalizer . finalizers ) ) <EOL> a1 . foo = "<STR_LIT:hello>" <EOL> del ( a1 ) <EOL> self . assertEqual ( <NUM_LIT:2> , acc . acc ) <EOL> clear_finalizers ( False ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( ThreadSafeFinalizer . finalizers ) ) <EOL> def testCleanUpAll ( self ) : <EOL> acc = Accumulator ( ) <EOL> a1 = self . work2 ( acc ) <EOL> self . assertEqual ( <NUM_LIT:1> , acc . acc ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( ThreadSafeFinalizer . finalizers ) ) <EOL> clear_finalizers ( True ) <EOL> self . assertEqual ( <NUM_LIT:1> , acc . acc ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( ThreadSafeFinalizer . finalizers ) ) <EOL> a1 . foo = "<STR_LIT:hello>" <EOL> del ( a1 ) <EOL> self . assertEqual ( <NUM_LIT:1> , acc . acc ) <EOL> class TestFinalizer ( unittest . TestCase ) : <EOL> def tearDown ( self ) : <EOL> clear_finalizers ( True ) <EOL> def work1 ( self , acc ) : <EOL> a1 = AClass2 ( <NUM_LIT:1> , acc ) <EOL> a2 = AClass2 ( <NUM_LIT:2> , acc ) <EOL> def work2 ( self , acc ) : <EOL> a1 = AClass2 ( <NUM_LIT:1> , acc ) <EOL> a2 = AClass2 ( <NUM_LIT:2> , acc ) <EOL> return a1 <EOL> def testFinalizer ( self ) : <EOL> acc = Accumulator ( ) <EOL> self . work1 ( acc ) <EOL> self . assertEqual ( <NUM_LIT:2> , acc . acc ) <EOL> def testCleanUp ( self ) : <EOL> acc = Accumulator ( ) <EOL> a1 = self . work2 ( acc ) <EOL> self . assertEqual ( <NUM_LIT:1> , acc . acc ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( Finalizer . finalizers ) ) <EOL> clear_finalizers ( False ) <EOL> self . assertEqual ( <NUM_LIT:1> , acc . acc ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( Finalizer . finalizers ) ) <EOL> a1 . foo = "<STR_LIT:hello>" <EOL> del ( a1 ) <EOL> self . assertEqual ( <NUM_LIT:2> , acc . acc ) <EOL> clear_finalizers ( False ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( Finalizer . finalizers ) ) <EOL> def testCleanUpAll ( self ) : <EOL> acc = Accumulator ( ) <EOL> a1 = self . work2 ( acc ) <EOL> self . assertEqual ( <NUM_LIT:1> , acc . acc ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( Finalizer . finalizers ) ) <EOL> clear_finalizers ( True ) <EOL> self . assertEqual ( <NUM_LIT:1> , acc . acc ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( Finalizer . finalizers ) ) <EOL> a1 . foo = "<STR_LIT:hello>" <EOL> del ( a1 ) <EOL> self . assertEqual ( <NUM_LIT:1> , acc . acc ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
