Skip to content

Commit b77189c

Browse files
committed
Refactor progress
1 parent 26929b0 commit b77189c

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

common/allocation.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def data(self) -> bytes:
3838
"""
3939
data = b""
4040
for fragment in self._fragments:
41-
assert not fragment.is_returned_to_block
4241
data += fragment.data
4342
return data
4443

common/pool.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ def owner(self) -> Owner:
4545
"""
4646
return self._owner
4747

48+
@property
49+
def nr_used_bytes(self):
50+
"""
51+
Return the total number of used bytes in the pool.
52+
"""
53+
return sum(block.nr_used_bytes for block in self._blocks)
54+
4855
@property
4956
def nr_unused_bytes(self):
5057
"""
@@ -79,8 +86,8 @@ def get_block(self, block_uuid: UUID) -> Block:
7986
def allocate(self, size: PositiveInt, purpose: str) -> Allocation:
8087
"""
8188
Allocate an allocation from the pool. An allocation consists of one or more fragments.
82-
This either returns an Allocation object for the full requested `size` or None if there is
83-
not enough unallocated data left in the pool.
89+
This either returns an Allocation object for the full requested `size`. Raises exception
90+
OutOfPreSharedRandomDataError if not enough unused bytes are available in the pool.
8491
"""
8592
available = self.nr_unused_bytes
8693
if available < size:

common/tests/test_pool.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
from uuid import uuid4
66
from typing import List
7+
import pytest
78
from common.pool import Pool
89
from common.block import Block
10+
from common.exceptions import InvalidBlockUUIDError
911
from common.utils import bytes_to_str
1012

1113

@@ -45,13 +47,15 @@ def test_properties():
4547
assert pool.owner == Pool.Owner.LOCAL
4648

4749

48-
def test_nr_unused_bytes():
50+
def test_nr_used_and_unused_bytes():
4951
"""
5052
Number of unused bytes in the pool.
5153
"""
5254
pool, _blocks = _create_test_pool_and_block([10, 20, 30])
55+
assert pool.nr_used_bytes == 0
5356
assert pool.nr_unused_bytes == 60
5457
_allocation = pool.allocate(10, purpose="test")
58+
assert pool.nr_used_bytes == 10
5559
assert pool.nr_unused_bytes == 50
5660

5761

@@ -74,3 +78,34 @@ def test_to_mgmt():
7478
],
7579
"owner": str(pool.owner),
7680
}
81+
82+
83+
def test_get_block_success():
84+
"""
85+
Get a block by UUID (block exists).
86+
"""
87+
pool, blocks = _create_test_pool_and_block([10, 20])
88+
for block in blocks:
89+
retrieved_block = pool.get_block(block.uuid)
90+
assert retrieved_block == block
91+
92+
93+
def test_get_block_unknown_uuid():
94+
"""
95+
Get a block by UUID (no block with the given UUID).
96+
"""
97+
pool, _blocks = _create_test_pool_and_block([10, 20])
98+
uuid = uuid4()
99+
with pytest.raises(InvalidBlockUUIDError):
100+
pool.get_block(uuid)
101+
102+
103+
def test_allocate_success_empty_pool_first_block():
104+
"""
105+
Allocate an allocation from a pool. The pool is empty. The allocation fits in the first block.
106+
"""
107+
pool, _blocks = _create_test_pool_and_block([10, 20])
108+
allocation = pool.allocate(10, purpose="test")
109+
assert allocation is not None
110+
assert allocation.data == bytes.fromhex("00010203040506070809")
111+
assert pool.nr_used_bytes == 10

0 commit comments

Comments
 (0)