Skip to content

Commit 035b29b

Browse files
fangererqunaibit
authored andcommitted
Add another corner case to test_gc
1 parent 1999fb2 commit 035b29b

File tree

1 file changed

+44
-2
lines changed
  • graalpython/com.oracle.graal.python.test/src/tests/cpyext

1 file changed

+44
-2
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_gc.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ def test_native_class(self):
193193
ID_OBJ11 = 9
194194
ID_OBJ12 = 10
195195
ID_OBJ13 = 11
196+
ID_OBJ14 = 12
196197

197198
# don't rely on deterministic Java GC behavior by default on GraalPy
198199
RELY_ON_GC = os.environ.get("RELY_ON_GC", not GRAALPY)
@@ -337,12 +338,16 @@ def assert_is_freed(id): pass
337338
assert_is_alive(ID_OBJ1)
338339

339340
del obj1
341+
####################################### GC #######################################
340342
self._trigger_gc()
343+
##################################################################################
341344
assert_is_alive(ID_OBJ0)
342345
assert_is_alive(ID_OBJ1)
343346

344347
del obj0
348+
####################################### GC #######################################
345349
self._trigger_gc()
350+
##################################################################################
346351
assert_is_freed(ID_OBJ0)
347352
assert_is_freed(ID_OBJ1)
348353

@@ -356,6 +361,7 @@ def assert_is_freed(id): pass
356361
obj9 = TestCycle0(ID_OBJ9)
357362
obj10 = TestCycle0(ID_OBJ10)
358363
obj11 = TestCycle0(ID_OBJ11)
364+
obj14 = TestCycle0(ID_OBJ14)
359365

360366
# Legend
361367
# '=>'
@@ -385,6 +391,8 @@ def assert_is_freed(id): pass
385391
# update_refs: 10 1 11
386392
# subtract_refs: 10 0 10
387393
# move_unreachable: 10 0 10
394+
# update_refs: 10 11 11
395+
# subtract_refs: 10 10 10
388396
# commit_weak_cand: obj2 => obj3 =ht-> l => obj2
389397
obj2.set_obj(obj3)
390398
l = [obj2]
@@ -400,7 +408,9 @@ def assert_is_freed(id): pass
400408
# establish cycle: obj4 => obj5 =ht=> l1 => obj4
401409
# update_refs: 10 1 11
402410
# subtract_refs: 10 0 10
403-
# move_unreachable: 10 10 10
411+
# move_unreachable: 10 1 10
412+
# update_refs: 10 11 11
413+
# subtract_refs: 10 10 10
404414
# commit_weak_cand: obj4 => obj5 =ht-> l1 => obj4
405415
obj4.set_obj(obj5)
406416
l1 = [obj4]
@@ -422,6 +432,8 @@ def assert_is_freed(id): pass
422432
# update_refs: 1 11
423433
# subtract_refs: 1 10
424434
# move_unreachable: 1 10
435+
# update_refs: 1 11
436+
# subtract_refs: 1 10
425437
# commit_weak_cand: N => obj10 =ht=> l2
426438
l2 = ["hello"]
427439
obj10.set_obj(l2)
@@ -433,14 +445,28 @@ def assert_is_freed(id): pass
433445
# update_refs: 11 11
434446
# subtract_refs: 11 10
435447
# move_unreachable: 11 10
436-
# commit_weak_cand: N => obj11 =ht=> l3
448+
# update_refs: 11 11
449+
# subtract_refs: 11 10
450+
# commit_weak_cand: J/N => obj11 =ht=> l3
437451
l3 = ["hello"]
438452
obj11.set_obj(l3)
439453
TestCycle0.set_global_obj(1, obj11)
440454
htid_l3 = get_handle_table_id(l3)
441455
del l3
442456
# difference to previous situation: obj11 is still reachable from Java
443457

458+
# J => obj14 =ht=> l3
459+
# update_refs: 10 11
460+
# subtract_refs: 10 10
461+
# move_unreachable: 10 10
462+
# update_refs: 10 11
463+
# subtract_refs: 10 10
464+
# commit_weak_cand: J/N => obj11 =ht=> l3
465+
l4 = ["world"]
466+
obj14.set_obj(l4)
467+
htid_l4 = get_handle_table_id(l4)
468+
del l4
469+
444470
# everything should still be alive
445471
assert_is_alive(ID_OBJ2)
446472
assert_is_alive(ID_OBJ3)
@@ -452,17 +478,21 @@ def assert_is_freed(id): pass
452478
assert_is_alive(ID_OBJ9)
453479
assert_is_alive(ID_OBJ10)
454480
assert_is_alive(ID_OBJ11)
481+
assert_is_alive(ID_OBJ14)
455482
assert is_strong_handle_table_ref(htid_l)
456483
assert is_strong_handle_table_ref(htid_l1)
457484
assert is_strong_handle_table_ref(htid_l2)
458485
assert is_strong_handle_table_ref(htid_l3)
486+
assert is_strong_handle_table_ref(htid_l4)
459487
assert is_strong_handle_table_ref(htid_d0)
460488

461489
del obj2, l, obj3
462490
del obj4, obj5
463491
del obj6, d0, obj7
464492

493+
####################################### GC #######################################
465494
self._trigger_gc()
495+
##################################################################################
466496

467497
# Delete Java ref after GC. This will provoke the situation where 'PythonAbstractNativeObject' of obj11 will
468498
# die after references where potentially replicated. This tests if dangling pointers appear for the managed
@@ -479,19 +509,29 @@ def assert_is_freed(id): pass
479509
assert_is_alive(ID_OBJ8)
480510
assert_is_alive(ID_OBJ9)
481511
assert_is_alive(ID_OBJ10)
512+
assert_is_alive(ID_OBJ14)
482513
assert is_strong_handle_table_ref(htid_l2)
483514
assert is_strong_handle_table_ref(htid_l3)
484515
assert not is_strong_handle_table_ref(htid_l)
485516
assert not is_strong_handle_table_ref(htid_l1)
517+
assert not is_strong_handle_table_ref(htid_l4)
486518
assert not is_strong_handle_table_ref(htid_d0)
487519

488520
rescued_obj4 = l1[0]
489521
del l1
522+
523+
__graalpython__.tdebug("uff")
524+
TestCycle0.set_global_obj(2, obj14)
525+
del obj14
526+
####################################### GC #######################################
490527
self._trigger_gc()
528+
##################################################################################
491529
# still reachable
492530
assert_is_alive(ID_OBJ4)
493531
assert_is_alive(ID_OBJ5)
532+
assert_is_alive(ID_OBJ14)
494533
assert rescued_obj4.get_obj().get_obj()[0] is rescued_obj4
534+
assert is_strong_handle_table_ref(htid_l4)
495535

496536
del rescued_obj4
497537

@@ -516,7 +556,9 @@ def assert_is_freed(id): pass
516556

517557
del obj12, obj13, l2, l3
518558

559+
####################################### GC #######################################
519560
self._trigger_gc()
561+
##################################################################################
520562

521563
assert_is_freed(ID_OBJ4)
522564
assert_is_freed(ID_OBJ5)

0 commit comments

Comments
 (0)