added back drop_old_entries as well as a test case for it, only trigger it after a skip

This commit is contained in:
2025-10-17 17:14:07 +02:00
parent 2733be493b
commit 3424258ac6

View File

@@ -18,7 +18,9 @@ class Sorter:
def flush_ready(self):
res = self._collect_contiguous_IDs()
self._skip_over_gap()
skipped = self._skip_over_gap()
if skipped:
self._drop_old_entries()
return res
def _collect_contiguous_IDs(self):
@@ -30,7 +32,16 @@ class Sorter:
return res
def _skip_over_gap(self):
self.next_ID = max(self.next_ID, self.max_ID - self.window)
threshold = self.max_ID - self.window
skip = self.next_ID < threshold
if skip:
self.next_ID = threshold
return skip
def _drop_old_entries(self):
for i in list(self.buffer):
if i < self.next_ID:
del self.buffer[i]
@@ -63,5 +74,28 @@ if __name__ == "__main__":
exc = [(i, i) for i in exc]
assert exc == ready, f"{exc} != {ready}"
assert sorter.buffer == {}
sorter = Sorter(window=5)
data = [
0, # this is sent -- small gap coming
2, # large gap coming -- this goes stale
] + list(range(10, 17)) # this is sent
expected = [
[0],
[], [], [], [], [], [], []
] + [list(range(10, 17))]
for ID, exc in zip(data, expected):
sorter.add(ID, ID)
ready = sorter.flush_ready()
exc = [(i, i) for i in exc]
assert exc == ready, f"{exc} != {ready}"
assert sorter.buffer == {}