Skip to content

Commit

Permalink
Align SkipRepeatsQueue with Eventlet's Queue implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-vanderheijden committed Sep 17, 2024
1 parent 7bca6d9 commit 68cbcdf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
13 changes: 6 additions & 7 deletions src/watchdog/utils/bricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,13 @@ def _init(self, maxsize: int) -> None:
super()._init(maxsize)
self._last_item = None

def _put(self, item: Any) -> None:
def put(self, item: Any, block: bool = True, timeout: float | None = None) -> None:
if self._last_item is None or item != self._last_item:
super()._put(item)
self._last_item = item
else:
# `put` increments `unfinished_tasks` even if we did not put
# anything into the queue here
self.unfinished_tasks -= 1
super().put(item, block, timeout)

def _put(self, item: Any) -> None:
super()._put(item)
self._last_item = item

def _get(self) -> Any:
item = super()._get()
Expand Down
32 changes: 21 additions & 11 deletions tests/isolated/eventlet_skip_repeat_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@

from watchdog.utils.bricks import SkipRepeatsQueue

# same as test_basic_queue() inside test_skip_repeats_queue.py
q = SkipRepeatsQueue(10)
q.put('A')
q.put('A')
q.put('A')
q.put('A')
q.put('B')
q.put('A')

q = SkipRepeatsQueue()
value = q.get()
assert value == 'A'
q.task_done()

e1 = (2, "fred")
e2 = (2, "george")
e3 = (4, "sally")
assert q.unfinished_tasks == 2

q.put(e1)
q.put(e2)
q.put(e3)
value = q.get()
assert value == 'B'
q.task_done()

assert q.unfinished_tasks == 1

value = q.get()
assert value == 'A'
q.task_done()

assert e1 == q.get()
assert e2 == q.get()
assert e3 == q.get()
assert q.empty()
assert q.unfinished_tasks == 0

0 comments on commit 68cbcdf

Please sign in to comment.