Skip to content

Commit a0b896f

Browse files
committed
Even more function.
1 parent 9f1c3b1 commit a0b896f

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

src/priority/priority.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ def remove_child(self, child):
8282

8383
self.child_queue = new_queue
8484

85+
for new_child in child.children:
86+
self.add_child(new_child)
87+
8588
def schedule(self):
8689
"""
8790
Returns the stream ID of the next child to schedule. Potentially
@@ -217,9 +220,9 @@ def remove_stream(self, stream_id):
217220
"""
218221
Removes a stream from the priority tree.
219222
"""
220-
# TODO: At some point we should actually prune streams we no longer
221-
# need. For now, just mark it permanently blocked.
222-
self._streams[stream_id].active = False
223+
child = self._streams.pop(stream_id)
224+
parent = child.parent
225+
parent.remove_child(child)
223226

224227
def block(self, stream_id):
225228
"""

test/test_priority.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
)
4545

4646

47-
4847
def readme_tree():
4948
"""
5049
Provide a tree configured as the one in the readme.
@@ -113,6 +112,38 @@ def test_stream_repr(self):
113112
s = priority.Stream(stream_id=80, weight=16)
114113
assert repr(s) == "Stream<id=80, weight=16>"
115114

115+
@given(STREAMS_AND_WEIGHTS)
116+
def test_streams_are_well_ordered(self, streams_and_weights):
117+
"""
118+
Streams are ordered by their stream ID.
119+
"""
120+
stream_list = [
121+
priority.Stream(stream_id=s, weight=w)
122+
for s, w in streams_and_weights
123+
]
124+
stream_list = sorted(stream_list)
125+
streams_by_id = [stream.stream_id for stream in stream_list]
126+
assert sorted(streams_by_id) == streams_by_id
127+
128+
@given(
129+
integers(min_value=1, max_value=2**24),
130+
integers(min_value=1, max_value=2**24)
131+
)
132+
def test_stream_ordering(self, a, b):
133+
"""
134+
Two streams are well ordered based on their stream ID.
135+
"""
136+
s1 = priority.Stream(stream_id=a, weight=16)
137+
s2 = priority.Stream(stream_id=b, weight=32)
138+
139+
assert (s1 < s2) == (a < b)
140+
assert (s1 <= s2) == (a <= b)
141+
assert (s1 > s2) == (a > b)
142+
assert (s1 >= s2) == (a >= b)
143+
assert (s1 == s2) == (a == b)
144+
assert (s1 != s2) == (a != b)
145+
146+
116147

117148
class TestPriorityTreeManual(object):
118149
"""
@@ -161,6 +192,24 @@ def test_priority_tree_blocking_is_isomorphic(self,
161192
result = [next(tree) for _ in range(len(expected))]
162193
assert expected == result
163194

195+
@given(BLOCKED_AND_ACTIVE)
196+
def test_removing_items_behaves_similarly_to_blocking(self,
197+
blocked_expected):
198+
"""
199+
From the perspective of iterating over items, removing streams should
200+
have the same effect as blocking them, except that the ordering
201+
changes. Because the ordering is not important, don't test for it.
202+
"""
203+
tree = readme_tree()
204+
blocked = blocked_expected[0]
205+
expected = set(blocked_expected[1])
206+
207+
for stream_id in blocked:
208+
tree.remove_stream(stream_id)
209+
210+
result = set(next(tree) for _ in range(len(expected)))
211+
assert expected == result
212+
164213
def test_priority_tree_raises_deadlock_error_if_all_blocked(self):
165214
"""
166215
Assuming all streams are blocked and none can progress, asking for the

0 commit comments

Comments
 (0)