|
5 | 5 |
|
6 | 6 | from __future__ import annotations |
7 | 7 |
|
8 | | -import os |
9 | 8 | import pathlib |
10 | 9 | from collections.abc import AsyncGenerator, Iterator, Sequence |
11 | | -from datetime import timedelta |
12 | 10 | from typing import Any |
13 | 11 | from unittest import mock |
14 | 12 |
|
15 | 13 | import pytest |
16 | 14 | from watchfiles import Change |
17 | 15 | from watchfiles.main import FileChange |
18 | 16 |
|
19 | | -from frequenz.channels.util import FileWatcher, Select, Timer |
| 17 | +from frequenz.channels.util import FileWatcher |
20 | 18 |
|
21 | 19 |
|
22 | 20 | class _FakeAwatch: |
@@ -72,67 +70,3 @@ async def test_file_watcher_receive_updates( |
72 | 70 | for change in changes: |
73 | 71 | recv_changes = await file_watcher.receive() |
74 | 72 | assert recv_changes == pathlib.Path(change[1]) |
75 | | - |
76 | | - |
77 | | -async def test_file_watcher(tmp_path: pathlib.Path) -> None: |
78 | | - """Ensure file watcher is returning paths on file events. |
79 | | -
|
80 | | - Args: |
81 | | - tmp_path (pathlib.Path): A tmp directory to run the file watcher on. |
82 | | - Created by pytest. |
83 | | - """ |
84 | | - filename = tmp_path / "test-file" |
85 | | - file_watcher = FileWatcher(paths=[str(tmp_path)]) |
86 | | - |
87 | | - number_of_writes = 0 |
88 | | - expected_number_of_writes = 3 |
89 | | - |
90 | | - select = Select( |
91 | | - timer=Timer.timeout(timedelta(seconds=0.1)), |
92 | | - file_watcher=file_watcher, |
93 | | - ) |
94 | | - while await select.ready(): |
95 | | - if msg := select.timer: |
96 | | - filename.write_text(f"{msg.inner}") |
97 | | - elif msg := select.file_watcher: |
98 | | - assert msg.inner == filename |
99 | | - number_of_writes += 1 |
100 | | - # After receiving a write 3 times, unsubscribe from the writes channel |
101 | | - if number_of_writes == expected_number_of_writes: |
102 | | - break |
103 | | - |
104 | | - assert number_of_writes == expected_number_of_writes |
105 | | - |
106 | | - |
107 | | -async def test_file_watcher_change_types(tmp_path: pathlib.Path) -> None: |
108 | | - """Ensure file watcher is returning paths only on the DELETE change. |
109 | | -
|
110 | | - Args: |
111 | | - tmp_path (pathlib.Path): A tmp directory to run the file watcher on. |
112 | | - Created by pytest. |
113 | | - """ |
114 | | - filename = tmp_path / "test-file" |
115 | | - file_watcher = FileWatcher( |
116 | | - paths=[str(tmp_path)], event_types={FileWatcher.EventType.DELETE} |
117 | | - ) |
118 | | - |
119 | | - select = Select( |
120 | | - write_timer=Timer.timeout(timedelta(seconds=0.1)), |
121 | | - deletion_timer=Timer.timeout(timedelta(seconds=0.25)), |
122 | | - watcher=file_watcher, |
123 | | - ) |
124 | | - number_of_deletes = 0 |
125 | | - number_of_write = 0 |
126 | | - while await select.ready(): |
127 | | - if msg := select.write_timer: |
128 | | - filename.write_text(f"{msg.inner}") |
129 | | - number_of_write += 1 |
130 | | - elif _ := select.deletion_timer: |
131 | | - os.remove(filename) |
132 | | - elif _ := select.watcher: |
133 | | - number_of_deletes += 1 |
134 | | - break |
135 | | - |
136 | | - assert number_of_deletes == 1 |
137 | | - # Can be more because the watcher could take some time to trigger |
138 | | - assert number_of_write >= 2 |
0 commit comments