Skip to content

Commit 7329f3f

Browse files
committed
Refactor progress
1 parent e4f6e18 commit 7329f3f

6 files changed

Lines changed: 119 additions & 90 deletions

File tree

common/allocation.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ def data(self) -> bytes:
4545
data += fragment.data
4646
return data
4747

48+
def give_back(self) -> None:
49+
"""
50+
Give the allocation back to the pool it was taken from.
51+
"""
52+
for fragment in self._fragments:
53+
fragment.give_back()
54+
self._fragments = []
55+
4856
def to_mgmt(self) -> dict:
4957
"""
5058
Get the management status.
@@ -53,14 +61,6 @@ def to_mgmt(self) -> dict:
5361
"fragments": [fragment.to_mgmt() for fragment in self._fragments],
5462
}
5563

56-
def return_to_pool(self) -> None:
57-
"""
58-
Return the allocation to the pool.
59-
"""
60-
for fragment in self._fragments:
61-
fragment.return_to_block()
62-
self._fragments = []
63-
6464
@classmethod
6565
def from_api(
6666
cls,

common/fragment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def data(self):
6868

6969
def give_back(self):
7070
"""
71-
Return the fragment to the block it was taken from.
71+
Give the fragment back to the block it was taken from.
7272
"""
7373
assert self._data is not None, "Attempt to return fragment twice"
7474
self._block.give_back_data(self._start, self._data)

common/tests/test_allocation.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,42 @@
22
Unit tests for the Allocation class.
33
"""
44

5-
from uuid import uuid4
65
from common.allocation import Allocation
7-
from common.block import Block
86
from common.fragment import Fragment
7+
from .unit_test_common import create_test_block, create_test_pool_and_blocks
98

109

11-
def _bytes_test_pattern(size):
12-
return bytes([i % 255 for i in range(size)])
13-
14-
15-
def _create_test_block(size):
16-
uuid = uuid4()
17-
data = _bytes_test_pattern(size)
18-
block = Block(uuid, data)
19-
return block
20-
21-
22-
def test_init():
10+
def test_init_and_properties():
2311
"""
2412
Initialize an allocation.
2513
"""
26-
block = _create_test_block(100)
14+
block = create_test_block(100)
2715
fragment = Fragment(
2816
block=block,
2917
start=0,
3018
size=10,
3119
data=bytes.fromhex("00010203040506070809"),
3220
)
33-
_allocation = Allocation(fragments=[fragment])
21+
allocation = Allocation(fragments=[fragment])
22+
assert allocation.fragments == [fragment]
23+
assert allocation.data == bytes.fromhex("00010203040506070809")
24+
25+
26+
def test_give_back():
27+
"""
28+
Give back an allocation.
29+
"""
30+
# pylint: disable=protected-access
31+
pool, blocks = create_test_pool_and_blocks([10, 6])
32+
assert blocks[0]._data == bytes.fromhex("00010203040506070809")
33+
assert blocks[1]._data == bytes.fromhex("000102030405")
34+
allocation = pool.allocate(5, purpose="test")
35+
assert allocation is not None
36+
assert allocation.data == bytes.fromhex("0001020304")
37+
assert pool.nr_used_bytes == 5
38+
assert blocks[0]._data == bytes.fromhex("00000000000506070809")
39+
assert blocks[1]._data == bytes.fromhex("000102030405")
40+
allocation.give_back()
41+
assert pool.nr_used_bytes == 0
42+
assert blocks[0]._data == bytes.fromhex("00010203040506070809")
43+
assert blocks[1]._data == bytes.fromhex("000102030405")

common/tests/test_block.py

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,7 @@
1212
InvalidPSRDIndex,
1313
PSRDDataAlreadyUsedError,
1414
)
15-
16-
17-
def _bytes_test_pattern(size):
18-
return bytes([i % 255 for i in range(size)])
19-
20-
21-
def _create_test_block(size):
22-
uuid = uuid4()
23-
data = _bytes_test_pattern(size)
24-
block = Block(uuid, data)
25-
return block
15+
from .unit_test_common import bytes_test_pattern, create_test_block
2616

2717

2818
def test_init():
@@ -31,7 +21,7 @@ def test_init():
3121
"""
3222
size = 1000
3323
uuid = uuid4()
34-
data = _bytes_test_pattern(size)
24+
data = bytes_test_pattern(size)
3525
_block = Block(uuid, data)
3626

3727

@@ -41,7 +31,7 @@ def test_properties():
4131
"""
4232
size = 1000
4333
uuid = uuid4()
44-
data = _bytes_test_pattern(size)
34+
data = bytes_test_pattern(size)
4535
block = Block(uuid, data)
4636
assert block.uuid == uuid
4737
assert block.size == size
@@ -55,7 +45,7 @@ def test_to_mgmt():
5545
"""
5646
size = 20
5747
uuid = uuid4()
58-
data = _bytes_test_pattern(size)
48+
data = bytes_test_pattern(size)
5949
block = Block(uuid, data)
6050
block_mgmt = block.to_mgmt()
6151
assert block_mgmt == {
@@ -79,7 +69,7 @@ def test_allocate_data_full():
7969
"""
8070
Allocate data from block: full requested allocation is fully available.
8171
"""
82-
block = _create_test_block(100)
72+
block = create_test_block(100)
8373
(start, size, data) = block.allocate_data(10)
8474
assert start == 0
8575
assert size == 10
@@ -90,7 +80,7 @@ def test_allocate_data_partial():
9080
"""
9181
Allocate data from block: full requested allocation is partially available.
9282
"""
93-
block = _create_test_block(9)
83+
block = create_test_block(9)
9484
(start, size, data) = block.allocate_data(90)
9585
assert start == 0
9686
assert size == 9
@@ -102,7 +92,7 @@ def test_allocate_data_bytes_zeroed():
10292
Allocate data from block: bytes in the block are zeroed after allocation.
10393
"""
10494
# pylint: disable=protected-access
105-
block = _create_test_block(5)
95+
block = create_test_block(5)
10696
assert block._data == bytes.fromhex("0001020304")
10797
(start, size, data) = block.allocate_data(3)
10898
assert start == 0
@@ -116,7 +106,7 @@ def test_allocate_data_full_full():
116106
Allocate data twice from a block: first requested allocation is fully available, second
117107
requested allocation is also fully available.
118108
"""
119-
block = _create_test_block(100)
109+
block = create_test_block(100)
120110
(start, size, data) = block.allocate_data(10)
121111
assert start == 0
122112
assert size == 10
@@ -132,7 +122,7 @@ def test_allocate_data_full_partial():
132122
Allocate data twice from a block: first requested allocation is fully available, second
133123
requested allocation is also fully available.
134124
"""
135-
block = _create_test_block(12)
125+
block = create_test_block(12)
136126
(start, size, data) = block.allocate_data(10)
137127
assert start == 0
138128
assert size == 10
@@ -148,7 +138,7 @@ def test_allocate_data_full_none():
148138
Allocate data twice from a block: first requested allocation is fully available, second
149139
requested allocation is also fully available.
150140
"""
151-
block = _create_test_block(10)
141+
block = create_test_block(10)
152142
(start, size, data) = block.allocate_data(10)
153143
assert start == 0
154144
assert size == 10
@@ -160,7 +150,7 @@ def test_take_data_all_free():
160150
"""
161151
Take data from block: all requested data is free.
162152
"""
163-
block = _create_test_block(10)
153+
block = create_test_block(10)
164154
data = block.take_data(2, 5)
165155
assert data == bytes.fromhex("0203040506")
166156
assert block.nr_unused_bytes == 5
@@ -170,7 +160,7 @@ def test_take_data_invalid_start_index():
170160
"""
171161
Take data from block: invalid start index.
172162
"""
173-
block = _create_test_block(10)
163+
block = create_test_block(10)
174164
with pytest.raises(InvalidPSRDIndex):
175165
_data = block.take_data(-1, 5)
176166
with pytest.raises(InvalidPSRDIndex):
@@ -181,7 +171,7 @@ def test_take_data_already_in_use():
181171
"""
182172
Take data from block: already in use.
183173
"""
184-
block = _create_test_block(10)
174+
block = create_test_block(10)
185175
(start, size, data) = block.allocate_data(5)
186176
assert start == 0
187177
assert size == 5
@@ -200,7 +190,7 @@ def test_return_data():
200190
"""
201191
# pylint: disable=protected-access
202192
# Create a block
203-
block = _create_test_block(10)
193+
block = create_test_block(10)
204194
assert block._data == bytes.fromhex("00010203040506070809")
205195
assert block.nr_used_bytes == 0
206196
# Allocate 3 bytes
@@ -234,7 +224,7 @@ def test_allocate_fragment_full():
234224
"""
235225
Allocate a fragment. Requested allocation is fully available.
236226
"""
237-
block = _create_test_block(100)
227+
block = create_test_block(100)
238228
fragment = block.allocate_fragment(10)
239229
assert fragment.block == block
240230
assert fragment.start == 0
@@ -246,7 +236,7 @@ def test_allocate_fragment_partial():
246236
"""
247237
Allocate a fragment. Requested allocation is partially available.
248238
"""
249-
block = _create_test_block(5)
239+
block = create_test_block(5)
250240
fragment = block.allocate_fragment(10)
251241
assert fragment.block == block
252242
assert fragment.start == 0
@@ -258,7 +248,7 @@ def test_allocate_fragment_none():
258248
"""
259249
Allocate a fragment. No allocation is available.
260250
"""
261-
block = _create_test_block(5)
251+
block = create_test_block(5)
262252
fragment = block.allocate_fragment(5)
263253
assert fragment.block == block
264254
assert fragment.start == 0
@@ -271,7 +261,7 @@ def test_is_fully_used():
271261
"""
272262
Check if all bytes in the block have been used.
273263
"""
274-
block = _create_test_block(10)
264+
block = create_test_block(10)
275265
assert not block.is_fully_used()
276266
(start, size, data) = block.allocate_data(10)
277267
assert start == 0
@@ -284,18 +274,18 @@ def test_to_api():
284274
"""
285275
Create an APIBlock for a Block.
286276
"""
287-
block = _create_test_block(10)
277+
block = create_test_block(10)
288278
api_block = block.to_api()
289279
assert api_block.block_uuid == str(block.uuid)
290-
assert api_block.data == bytes_to_str(_bytes_test_pattern(10))
280+
assert api_block.data == bytes_to_str(bytes_test_pattern(10))
291281

292282

293283
def test_from_api_success():
294284
"""
295285
Create a Block from a valid APIBlock.
296286
"""
297287
uuid = uuid4()
298-
data = _bytes_test_pattern(10)
288+
data = bytes_test_pattern(10)
299289
api_block = APIBlock(block_uuid=str(uuid), data=bytes_to_str(data))
300290
block = Block.from_api(api_block)
301291
assert block.uuid == uuid
@@ -306,7 +296,7 @@ def test_from_api_bad_uuid():
306296
"""
307297
Attempt to create a Block from a bad APIBlock (invalid block UUID).
308298
"""
309-
data = _bytes_test_pattern(10)
299+
data = bytes_test_pattern(10)
310300
api_block = APIBlock(block_uuid="bad-uuid", data=bytes_to_str(data))
311301
with pytest.raises(InvalidBlockUUIDError):
312302
_block = Block.from_api(api_block)

0 commit comments

Comments
 (0)