Skip to content

Commit 8faacf3

Browse files
committed
Progress refactor
1 parent 4c19450 commit 8faacf3

4 files changed

Lines changed: 70 additions & 19 deletions

File tree

common/block.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ def take_data(self, start: int, size: int) -> bytes:
126126
self._data = self._data[:start] + b"\x00" * size + self._data[end:]
127127
return data
128128

129-
def return_data(self, start: int, data: bytes):
129+
def give_back_data(self, start: int, data: bytes):
130130
"""
131-
Return previously taken data to the block.
131+
Give back previously taken data to the block.
132132
"""
133-
# Returning data is only used internally; the parameters are decided by the outside world.
133+
# Giving back data is only used internally; the parameters are decided by the outside world.
134134
# Thus, if there is a problem with the parameters it is a bug: we assert rather than raise.
135135
size = len(data)
136136
assert size > 0

common/fragment.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ def allocate(block: Block, desired_size: int) -> Union[None, "Fragment"]:
8282
data=data,
8383
)
8484

85+
def give_back(self):
86+
"""
87+
Return the fragment to the block it was taken from.
88+
"""
89+
assert self._data is not None, "Attempt to return fragment twice"
90+
self._block.give_back_data(self._start, self._data)
91+
self._data = None
92+
8593
def to_mgmt(self) -> dict:
8694
"""
8795
Get the management status.
@@ -93,12 +101,6 @@ def to_mgmt(self) -> dict:
93101
"data": utils.bytes_to_str(self._data, truncate=True),
94102
}
95103

96-
def mark_as_returned_to_block(self):
97-
"""
98-
Mark the fragment as returned to the pool.
99-
"""
100-
self._data = None
101-
102104
def to_api(self) -> APIFragment:
103105
"""
104106
Create an APIFragment from a Fragment.

common/tests/test_block.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
)
1515

1616

17-
# pylint: disable=missing-function-docstring
18-
19-
2017
def _bytes_test_pattern(size):
2118
return bytes([i % 255 for i in range(size)])
2219

@@ -29,6 +26,9 @@ def _create_test_block(size):
2926

3027

3128
def test_block_init():
29+
"""
30+
Initialize a block.
31+
"""
3232
size = 1000
3333
uuid = uuid4()
3434
data = _bytes_test_pattern(size)
@@ -45,6 +45,9 @@ def test_block_properties():
4545

4646

4747
def test_block_to_mgmt():
48+
"""
49+
Get the management status.
50+
"""
4851
size = 20
4952
uuid = uuid4()
5053
data = _bytes_test_pattern(size)
@@ -60,6 +63,9 @@ def test_block_to_mgmt():
6063

6164

6265
def test_create_random_block():
66+
"""
67+
Create a block with random data.
68+
"""
6369
size = 100
6470
_block = Block.new_with_random_data(size)
6571

@@ -214,7 +220,7 @@ def test_return_data():
214220
assert block._data == bytes.fromhex("00000000000000000009")
215221
assert block.nr_used_bytes == 9
216222
# Return the middle 3 bytes
217-
block.return_data(3, bytes.fromhex("030405"))
223+
block.give_back_data(3, bytes.fromhex("030405"))
218224
assert block._data == bytes.fromhex("00000003040500000009")
219225
assert block.nr_used_bytes == 6
220226

common/tests/test_fragment.py

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
from common.utils import bytes_to_str
99

1010

11-
# pylint: disable=missing-function-docstring
12-
13-
1411
def _bytes_test_pattern(size):
1512
return bytes([i % 255 for i in range(size)])
1613

@@ -23,6 +20,9 @@ def _create_test_block(size):
2320

2421

2522
def test_fragment_init():
23+
"""
24+
Initialize a fragment.
25+
"""
2626
block = _create_test_block(100)
2727
_fragment = Fragment(
2828
block=block,
@@ -32,7 +32,10 @@ def test_fragment_init():
3232
)
3333

3434

35-
def test_fragment_allocate_full():
35+
def test_allocate_full():
36+
"""
37+
Allocate a fragment. Requested allocation is fully available.
38+
"""
3639
block = _create_test_block(100)
3740
fragment = Fragment.allocate(block, 10)
3841
assert fragment.block == block
@@ -41,7 +44,10 @@ def test_fragment_allocate_full():
4144
assert fragment.data == bytes.fromhex("00010203040506070809")
4245

4346

44-
def test_fragment_allocate_partial():
47+
def test_allocate_partial():
48+
"""
49+
Allocate a fragment. Requested allocation is partially available.
50+
"""
4551
block = _create_test_block(5)
4652
fragment = Fragment.allocate(block, 10)
4753
assert fragment.block == block
@@ -50,7 +56,10 @@ def test_fragment_allocate_partial():
5056
assert fragment.data == bytes.fromhex("0001020304")
5157

5258

53-
def test_fragment_allocate_none():
59+
def test_allocate_none():
60+
"""
61+
Allocate a fragment. No allocation is available.
62+
"""
5463
block = _create_test_block(5)
5564
fragment = Fragment.allocate(block, 5)
5665
assert fragment.block == block
@@ -60,7 +69,41 @@ def test_fragment_allocate_none():
6069
assert Fragment.allocate(block, 5) is None
6170

6271

72+
def test_give_back_success():
73+
"""
74+
Give a fragment back to the block.
75+
"""
76+
# pylint: disable=protected-access
77+
block = _create_test_block(16)
78+
assert block.nr_used_bytes == 0
79+
# Allocate 5 bytes for fragment A
80+
assert block._data == bytes.fromhex("000102030405060708090a0b0c0d0e0f")
81+
fragment_a = Fragment.allocate(block, 5)
82+
assert fragment_a.block == block
83+
assert fragment_a.start == 0
84+
assert fragment_a.size == 5
85+
assert fragment_a.data == bytes.fromhex("0001020304")
86+
assert block.nr_used_bytes == 5
87+
assert block._data == bytes.fromhex("000000000005060708090a0b0c0d0e0f")
88+
# Allocate 3 bytes for fragment B
89+
fragment_b = Fragment.allocate(block, 3)
90+
assert fragment_b.block == block
91+
assert fragment_b.start == 5
92+
assert fragment_b.size == 3
93+
assert fragment_b.data == bytes.fromhex("050607")
94+
assert block.nr_used_bytes == 8
95+
assert block._data == bytes.fromhex("000000000000000008090a0b0c0d0e0f")
96+
# Give fragment A back
97+
fragment_a.give_back()
98+
assert fragment_a.data is None
99+
assert block.nr_used_bytes == 3
100+
assert block._data == bytes.fromhex("000102030400000008090a0b0c0d0e0f")
101+
102+
63103
def test_fragment_to_mgmt():
104+
"""
105+
Get the management status of a fragment.
106+
"""
64107
block = _create_test_block(5)
65108
fragment = Fragment.allocate(block, 5)
66109
fragment_mgmt = fragment.to_mgmt()

0 commit comments

Comments
 (0)