|
9 | 9 |
|
10 | 10 | import pytest |
11 | 11 |
|
12 | | -from frequenz.channels import select, selected_from |
| 12 | +from frequenz.channels import ReceiverStoppedError, select, selected_from |
13 | 13 | from frequenz.channels.file_watcher import Event, EventType, FileWatcher |
14 | 14 | from frequenz.channels.timer import SkipMissedAndDrift, Timer |
15 | 15 |
|
@@ -99,3 +99,36 @@ async def test_file_watcher_deletes(tmp_path: pathlib.Path) -> None: |
99 | 99 | # Can be more because the watcher could take some time to trigger |
100 | 100 | assert number_of_write >= 3 |
101 | 101 | assert number_of_events == 2 |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.integration |
| 105 | +async def test_file_watcher_exit_iterator(tmp_path: pathlib.Path) -> None: |
| 106 | + """Test breaking the file watcher iterator. |
| 107 | +
|
| 108 | + Args: |
| 109 | + tmp_path: A tmp directory to run the file watcher on. Created by pytest. |
| 110 | + """ |
| 111 | + filename = tmp_path / "test-file" |
| 112 | + |
| 113 | + number_of_writes = 0 |
| 114 | + expected_number_of_writes = 3 |
| 115 | + |
| 116 | + file_watcher = FileWatcher(paths=[str(tmp_path)]) |
| 117 | + timer = Timer(timedelta(seconds=0.1), SkipMissedAndDrift()) |
| 118 | + |
| 119 | + async for selected in select(file_watcher, timer): |
| 120 | + if selected_from(selected, timer): |
| 121 | + filename.write_text(f"{selected.message}") |
| 122 | + elif selected_from(selected, file_watcher): |
| 123 | + number_of_writes += 1 |
| 124 | + if number_of_writes == expected_number_of_writes: |
| 125 | + file_watcher._stop_event.set() # pylint: disable=protected-access |
| 126 | + break |
| 127 | + |
| 128 | + ready = await file_watcher.ready() |
| 129 | + assert ready is False |
| 130 | + |
| 131 | + with pytest.raises(ReceiverStoppedError): |
| 132 | + file_watcher.consume() |
| 133 | + |
| 134 | + assert number_of_writes == expected_number_of_writes |
0 commit comments