Skip to content

Commit

Permalink
Improve stopping logic
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz committed Oct 28, 2024
1 parent af73344 commit 585963e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/django_watchfiles/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def stop(self) -> None:

def __iter__(self) -> Generator[set[tuple[Change, str]]]:
while True:
if self.stop_event.is_set():
return
self.change_event.clear()
for changes in watch(
*self.roots,
Expand Down
16 changes: 14 additions & 2 deletions tests/test_django_watchfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
from pathlib import Path

import pytest
from django.utils import autoreload
from watchfiles import Change

Expand Down Expand Up @@ -31,9 +32,20 @@ def test_set_roots_changed(self):
assert self.watcher.change_event.is_set()

def test_stop(self):
assert not self.watcher.stop_event.is_set()
(self.temp_path / "test.txt").touch()
self.watcher.set_roots({self.temp_path})
iterator = iter(self.watcher)
# Flush initial events
next(iterator)

self.watcher.stop()
assert self.watcher.stop_event.is_set()

with pytest.raises(StopIteration):
next(iterator)

# Not possible to restart
with pytest.raises(StopIteration):
next(iter(self.watcher))

def test_iter_no_changes(self):
(self.temp_path / "test.txt").touch()
Expand Down

0 comments on commit 585963e

Please sign in to comment.