Skip to content

Commit d7fd9b4

Browse files
committed
Refactor progress
1 parent ddf79cd commit d7fd9b4

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

common/pool.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,17 @@ def allocate(self, size: PositiveInt, purpose: str) -> Allocation:
116116
remaining_size == 0
117117
) # We checked availability at the top of the method.
118118
return Allocation(fragments)
119-
except Exception as exc:
119+
# As far as we know because of the check at the top, there is currently no way to reach
120+
# this. This is just defensive programming.
121+
except Exception as exc: # pragma: no cover
120122
# Allocation failed halfway; give back any fragments that were taken.
121123
for fragment in fragments:
122124
fragment.give_back()
123125
raise exc
124126

125-
def mark_allocation_used(self, allocation: Allocation):
126-
"""
127-
Mark an allocation as used in the pool. This is needed when the allocation was received
128-
from the other side instead of being allocated locally.
129-
"""
130-
# TODO: $$$ Is this the right way?
131-
for fragment in allocation.fragments:
132-
fragment.block.mark_fragment_used(fragment)
133-
134127
def delete_fully_used_blocks(self):
135128
"""
136129
Delete fully used PSRD blocks from the pool.
137130
"""
138-
self._blocks = [block for block in self._blocks if not block.is_fully_used()]
131+
new_blocks = [block for block in self._blocks if not block.is_fully_used()]
132+
self._blocks = new_blocks

common/tests/test_pool.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def test_get_block_unknown_uuid():
100100
pool.get_block(uuid)
101101

102102

103-
def test_allocate_success_empty_pool_first_block():
103+
def test_allocate_success_empty_pool_first_block_full():
104104
"""
105105
Allocate an allocation from a pool. The pool is empty. The allocation fits in the first block.
106106
"""
@@ -111,7 +111,7 @@ def test_allocate_success_empty_pool_first_block():
111111
assert pool.nr_used_bytes == 10
112112

113113

114-
def test_allocate_success_empty_pool_two_blocks():
114+
def test_allocate_success_empty_pool_first_block_full_second_block_partial():
115115
"""
116116
Allocate an allocation from a pool. The pool is empty. The allocation is spread over two blocks.
117117
"""
@@ -122,6 +122,20 @@ def test_allocate_success_empty_pool_two_blocks():
122122
assert pool.nr_used_bytes == 8
123123

124124

125+
def test_allocate_success_empty_pool_first_block_full_second_block_full():
126+
"""
127+
Allocate an allocation from a pool. The pool is empty. The allocation is spread over two blocks.
128+
"""
129+
pool, _blocks = _create_test_pool_and_block([5, 4])
130+
allocation = pool.allocate(9, purpose="test")
131+
assert allocation is not None
132+
assert allocation.data == bytes.fromhex("000102030400010203")
133+
assert pool.nr_used_bytes == 9
134+
135+
136+
# TODO: Also test re-allocation after giving back
137+
138+
125139
def test_allocate_failure_insufficient_space():
126140
"""
127141
Attempt to allocate an allocation from a pool. There is not enough unused data in the pool.
@@ -130,3 +144,17 @@ def test_allocate_failure_insufficient_space():
130144
with pytest.raises(OutOfPreSharedRandomDataError):
131145
pool.allocate(20, purpose="test")
132146
assert pool.nr_used_bytes == 0
147+
148+
149+
def test_delete_fully_used_blocks():
150+
"""
151+
Delete fully used PSRD blocks from the pool.
152+
"""
153+
pool, _blocks = _create_test_pool_and_block([10, 11])
154+
# Allocate all of the first block and part of the second block.
155+
_allocation = pool.allocate(15, purpose="test1")
156+
assert pool.nr_used_bytes == 15
157+
assert pool.nr_unused_bytes == 6
158+
pool.delete_fully_used_blocks()
159+
assert pool.nr_used_bytes == 5
160+
assert pool.nr_unused_bytes == 6

0 commit comments

Comments
 (0)