Skip to content

Commit 422d2d3

Browse files
committed
Refactor progress
1 parent 6a09fd8 commit 422d2d3

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

common/allocation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def from_api(
8585
fragments.append(fragment)
8686
except Exception as exc:
8787
for fragment in fragments:
88-
fragment.return_to_block()
88+
fragment.give_back()
8989
raise exc
9090
return Allocation(fragments=fragments)
9191

common/tests/test_allocation.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66
from common.allocation import Allocation, APIAllocation
7-
from common.exceptions import InvalidBlockUUIDError
7+
from common.exceptions import InvalidBlockUUIDError, InvalidPSRDIndex
88
from common.fragment import APIFragment, Fragment
99
from .unit_test_common import create_test_block, create_test_pool_and_blocks
1010

@@ -102,11 +102,28 @@ def test_from_api_success():
102102

103103
def test_from_api_bad_fragment():
104104
"""
105-
Attempt to create an Allocation from a valid APIAllocation: one of the fragments has an invalid
105+
Attempt to create an Allocation from a bad APIAllocation: one of the fragments has an invalid
106106
block UUID.
107107
"""
108108
pool, _blocks = create_test_pool_and_blocks([10])
109109
api_fragment = APIFragment(block_uuid="not-a-uuid", start=0, size=5)
110110
_api_allocation = APIAllocation(fragments=[api_fragment])
111111
with pytest.raises(InvalidBlockUUIDError):
112112
_allocation = Allocation.from_api(_api_allocation, pool)
113+
114+
115+
def test_from_api_second_fragment_is_bad():
116+
"""
117+
Attempt to create an Allocation from bad APIAllocations. The first APIFragment is valid. The
118+
second has an invalid block size. Make sure the first fragment is properly given back to the
119+
pool (all-or-nothing behavior).
120+
"""
121+
# pylint: disable=protected-access
122+
pool, blocks = create_test_pool_and_blocks([10])
123+
api_fragment_1 = APIFragment(block_uuid=str(blocks[0].uuid), start=0, size=5)
124+
api_fragment_2 = APIFragment(block_uuid=str(blocks[0].uuid), start=5, size=99999)
125+
_api_allocation = APIAllocation(fragments=[api_fragment_1, api_fragment_2])
126+
with pytest.raises(InvalidPSRDIndex):
127+
_allocation = Allocation.from_api(_api_allocation, pool)
128+
assert pool.nr_used_bytes == 0
129+
assert blocks[0]._data == bytes.fromhex("00010203040506070809")

0 commit comments

Comments
 (0)