Skip to content

Commit ac89c48

Browse files
committed
Refactor progress
1 parent 40539f9 commit ac89c48

5 files changed

Lines changed: 64 additions & 14 deletions

File tree

client/peer_hub.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,12 @@ def start_request_psrd_task_if_needed(self) -> None:
159159
Start request PSRD task(s) if needed.
160160
"""
161161
if self._local_pool_request_psrd_task is None:
162-
if self._local_pool.bytes_available < START_REQUEST_PSRD_THRESHOLD:
162+
if self._local_pool.nr_unused_bytes < START_REQUEST_PSRD_THRESHOLD:
163163
self._local_pool_request_psrd_task = asyncio.create_task(
164164
self.request_psrd_task(self._local_pool)
165165
)
166166
if self._peer_pool_request_psrd_task is None:
167-
if self._peer_pool.bytes_available < START_REQUEST_PSRD_THRESHOLD:
167+
if self._peer_pool.nr_unused_bytes < START_REQUEST_PSRD_THRESHOLD:
168168
self._peer_pool_request_psrd_task = asyncio.create_task(
169169
self.request_psrd_task(self._peer_pool)
170170
)
@@ -176,7 +176,7 @@ async def request_psrd_task(self, pool: Pool) -> None:
176176
task_name = f"request PSRD task for peer hub {self._hub_name} and pool owner {pool.owner}"
177177
LOGGER.info(f"Begin {task_name}")
178178
try:
179-
while pool.bytes_available < STOP_REQUEST_PSRD_THRESHOLD:
179+
while pool.nr_unused_bytes < STOP_REQUEST_PSRD_THRESHOLD:
180180
if not await self.attempt_request_psrd(pool):
181181
await asyncio.sleep(_GET_PSRD_RETRY_DELAY)
182182
except asyncio.CancelledError:

common/pool.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
from .exceptions import OutOfPreSharedRandomDataError, InvalidBlockUUIDError
1212

1313

14-
# TODO: Unit tests for newly added _owner attribute.
15-
16-
1714
class Pool:
1815
"""
1916
A pool of blocks.
@@ -49,9 +46,9 @@ def owner(self) -> Owner:
4946
return self._owner
5047

5148
@property
52-
def bytes_available(self):
49+
def nr_unused_bytes(self):
5350
"""
54-
Return the number of bytes available for allocation in the pool.
51+
Return the total number of unused bytes in the pool.
5552
"""
5653
return sum(block.nr_unused_bytes for block in self._blocks)
5754

@@ -85,7 +82,7 @@ def allocate(self, size: PositiveInt, purpose: str) -> Allocation:
8582
This either returns an Allocation object for the full requested `size` or None if there is
8683
not enough unallocated data left in the pool.
8784
"""
88-
available = self.bytes_available
85+
available = self.nr_unused_bytes
8986
if available < size:
9087
LOGGER.error(
9188
f"PSRD allocation failed: pool={self._name} owner={self._owner} purpose={purpose} "

common/tests/test_block.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def _create_test_block(size):
2525
return block
2626

2727

28-
def test_block_init():
28+
def test_init():
2929
"""
3030
Initialize a block.
3131
"""
@@ -35,7 +35,7 @@ def test_block_init():
3535
_block = Block(uuid, data)
3636

3737

38-
def test_block_properties():
38+
def test_properties():
3939
""" "
4040
Test the properties of a block.
4141
"""
@@ -47,7 +47,7 @@ def test_block_properties():
4747
assert block.nr_unused_bytes == size
4848

4949

50-
def test_block_to_mgmt():
50+
def test_to_mgmt():
5151
"""
5252
Get the management status.
5353
"""

common/tests/test_fragment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _create_test_pool_and_block(block_size):
2929
return (pool, block)
3030

3131

32-
def test_fragment_init():
32+
def test_init():
3333
"""
3434
Initialize a fragment.
3535
"""
@@ -73,7 +73,7 @@ def test_give_back_success():
7373
assert block._data == bytes.fromhex("000102030400000008090a0b0c0d0e0f")
7474

7575

76-
def test_fragment_to_mgmt():
76+
def test_to_mgmt():
7777
"""
7878
Get the management status of a fragment.
7979
"""

common/tests/test_pool.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Unit tests for the Fragment class.
3+
"""
4+
5+
from uuid import uuid4
6+
from common.pool import Pool
7+
from common.block import Block
8+
9+
10+
def _bytes_test_pattern(size):
11+
return bytes([i % 255 for i in range(size)])
12+
13+
14+
def _create_test_block(size):
15+
uuid = uuid4()
16+
data = _bytes_test_pattern(size)
17+
block = Block(uuid, data)
18+
return block
19+
20+
21+
def _create_test_pool_and_block(block_sizes: [int]):
22+
pool = Pool(name="test_pool", owner=Pool.Owner.LOCAL)
23+
blocks = []
24+
for block_size in block_sizes:
25+
block = _create_test_block(block_size)
26+
pool.add_block(block)
27+
blocks.append(block)
28+
return (pool, blocks)
29+
30+
31+
def test_init():
32+
"""
33+
Initialize a pool.
34+
"""
35+
_pool = Pool(name="test_pool", owner=Pool.Owner.LOCAL)
36+
37+
38+
def test_properties():
39+
"""
40+
Properties of the pool.
41+
"""
42+
pool = Pool(name="test_pool", owner=Pool.Owner.LOCAL)
43+
assert pool.owner == Pool.Owner.LOCAL
44+
45+
46+
def test_nr_unused_bytes():
47+
"""
48+
Number of unused bytes in the pool.
49+
"""
50+
pool, _blocks = _create_test_pool_and_block([10, 20, 30])
51+
assert pool.nr_unused_bytes == 60
52+
_allocation = pool.allocate(10, purpose="test")
53+
assert pool.nr_unused_bytes == 50

0 commit comments

Comments
 (0)