Skip to content

Commit

Permalink
mock id for _block.store testing
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed May 8, 2023
1 parent 675563d commit 0e65d76
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
16 changes: 3 additions & 13 deletions asdf/_tests/_block/test_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,10 @@ def test_is_valid():
assert not bk.is_valid()


def test_memory_reuse():
def test_same_class():
f = Foo()
bk = Key(f)
fid = id(f)
del f
objs = []
for _ in range(100):
f = Foo()
objs.append(f)
if fid == id(f):
break
else:
raise AssertionError("Failed to find reused memory address")

assert fid == id(f)
f2 = Foo()
assert not bk.is_valid()
assert not bk.matches(f)
assert not bk.matches(f2)
38 changes: 22 additions & 16 deletions asdf/_tests/_block/test_store.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest.mock import patch

import pytest

from asdf._block.key import Key
Expand Down Expand Up @@ -99,13 +101,15 @@ def test_get_memory_reused():
s.assign_object(f, v)
fid = id(f)
del f
for _ in range(1000):
f = Foo()
if id(f) == fid:
break
else:
raise AssertionError("Failed to trigger memory reuse")
assert s.lookup_by_object(f) is None
f2 = Foo()

def mock_id(obj):
if obj is f2:
return fid
return id(obj)

with patch("asdf._block.store.id", mock_id):
assert s.lookup_by_object(f2) is None


def test_set_memory_reused():
Expand All @@ -115,15 +119,17 @@ def test_set_memory_reused():
s.assign_object(f, v)
fid = id(f)
del f
for _ in range(1000):
f = Foo()
if id(f) == fid:
break
else:
raise AssertionError("Failed to trigger memory reuse")
nv = 26
s.assign_object(f, nv)
assert s.lookup_by_object(f) is nv
f2 = Foo()

def mock_id(obj):
if obj is f2:
return fid
return id(obj)

with patch("asdf._block.store.id", mock_id):
nv = 26
s.assign_object(f2, nv)
assert s.lookup_by_object(f2) is nv


def test_cleanup():
Expand Down

0 comments on commit 0e65d76

Please sign in to comment.