Skip to content

Commit

Permalink
add gc.collect after del in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Sep 19, 2024
1 parent 9ef61de commit aa40657
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions asdf/_tests/_block/test_callback.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import gc

import pytest

from asdf._block.callback import DataCallback
Expand Down Expand Up @@ -35,6 +37,7 @@ def __init__(self, value):
blks = ReadBlocks([Data("a"), Data("b")])
cb = DataCallback(0, blks)
del blks
gc.collect(2)

with pytest.raises(OSError, match="Attempt to read block data from missing block"):
cb()
Expand Down
5 changes: 5 additions & 0 deletions asdf/_tests/_block/test_key.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import gc

from asdf._block.key import Key

Expand Down Expand Up @@ -41,13 +42,15 @@ def test_is_valid():
bk = Key(f)
assert bk._is_valid()
del f
gc.collect(2)
assert not bk._is_valid()


def test_same_class():
f = Foo()
bk = Key(f)
del f
gc.collect(2)
f2 = Foo()
assert not bk._is_valid()
assert not bk._matches_object(f2)
Expand Down Expand Up @@ -92,6 +95,7 @@ def test_deleted_object_not_equal():
k1 = Key(f, key_value)
k2 = Key(f, key_value)
del f
gc.collect(2)
assert k1 != k2


Expand All @@ -113,4 +117,5 @@ def test_copy_deleted_object_not_equal():
k1 = Key(f)
k2 = copy.copy(k1)
del f
gc.collect(2)
assert k1 != k2
3 changes: 3 additions & 0 deletions asdf/_tests/_block/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import gc

import numpy as np
import pytest

Expand All @@ -14,6 +16,7 @@ def test_set_streamed_block_via_options():
with pytest.raises(ValueError, match=r"Can not add second streaming block"):
options.set_options(arr2, Options("streamed"))
del arr1
gc.collect(2)
options.set_options(arr2, Options("streamed"))


Expand Down
5 changes: 5 additions & 0 deletions asdf/_tests/_block/test_store.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gc
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -108,6 +109,7 @@ def test_get_memory_reused():
s.assign_object(f, v)
fid = id(f)
del f
gc.collect(2)
f2 = Foo()

def mock_id(obj):
Expand All @@ -126,6 +128,7 @@ def test_set_memory_reused():
s.assign_object(f, v)
fid = id(f)
del f
gc.collect(2)
f2 = Foo()

def mock_id(obj):
Expand All @@ -146,6 +149,7 @@ def test_cleanup():
s.assign_object(s, 42)
s.assign_object(k, 26)
del f
gc.collect(2)
s._cleanup()
assert s.lookup_by_object(k, None) is None

Expand All @@ -172,3 +176,4 @@ def test_keys_for_value():
returned_objects.add(obj)
assert objs == returned_objects
del returned_objects, objs
gc.collect(2)
2 changes: 2 additions & 0 deletions asdf/_tests/test_block_converter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import gc

import numpy as np
from numpy.testing import assert_array_equal
Expand Down Expand Up @@ -290,6 +291,7 @@ def test_shared_block_obj_removal(tmp_path):
with asdf.open(fn, mode="rw") as af:
af["b"] = None
del b
gc.collect(2)
af.update()
with asdf.open(fn) as af:
np.testing.assert_array_equal(af["a"].data, arr1)
Expand Down
9 changes: 7 additions & 2 deletions asdf/_tests/test_lazy_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def test_access_after_del(tmp_path):
d = af["a"]

del af
gc.collect(2)

with pytest.raises(asdf.exceptions.AsdfLazyReferenceError, match="Failed to resolve"):
d["b"]
Expand Down Expand Up @@ -215,10 +216,14 @@ def test_lazy_tree_option(tmp_path):
def test_resolve_af_ref():
with pytest.raises(asdf.exceptions.AsdfLazyReferenceError, match="Failed to resolve"):
_resolve_af_ref(None)

af = asdf.AsdfFile()
af_ref = weakref.ref(af)
assert _resolve_af_ref(af_ref) is af

del af
gc.collect(2)

with pytest.raises(asdf.exceptions.AsdfLazyReferenceError, match="Failed to resolve"):
_resolve_af_ref(af_ref)

Expand Down Expand Up @@ -285,7 +290,7 @@ def test_cache_frees_deleted_object(cache_test_tree_path):
# now delete all references to the list (including the one in the tree)
del l0, af.tree["a"]
# trigger garbage collection
gc.collect()
gc.collect(2)
# check that the weakref fails to resolve (so the list was freed)
assert lref() is None
# and we can no longer access 'a'
Expand All @@ -303,7 +308,7 @@ def test_cache_non_weakref():
obj = complex(1, 1)
cache_item = asdf.lazy_nodes._TaggedObjectCacheItem(tagged_node, obj)
del obj
gc.collect()
gc.collect(2)
assert cache_item.custom_object == complex(1, 1)


Expand Down

0 comments on commit aa40657

Please sign in to comment.