|
14 | 14 | limitations under the License.
|
15 | 15 | """
|
16 | 16 |
|
17 |
| -from accuracy_checker.utils import concat_lists, contains_all, contains_any, overrides, zipped_transform |
| 17 | +import os |
| 18 | +import threading |
| 19 | +from accuracy_checker.utils import concat_lists, contains_all, contains_any, overrides, zipped_transform, AtomicWriteFileHandle |
18 | 20 |
|
19 | 21 |
|
20 | 22 | def test_concat_lists():
|
@@ -125,3 +127,35 @@ class C:
|
125 | 127 |
|
126 | 128 | assert overrides(B, 'foo', A)
|
127 | 129 | assert not overrides(C, 'foo', A)
|
| 130 | + |
| 131 | + |
| 132 | +def thread_write_to_file(file_path, data, thread_id): |
| 133 | + with AtomicWriteFileHandle(file_path, 'wt') as file: |
| 134 | + file.write(f"Thread {thread_id}: {data}\n") |
| 135 | + |
| 136 | + |
| 137 | +class TestAtomicWriteFileHandle: |
| 138 | + |
| 139 | + def test_multithreaded_atomic_file_write(self): |
| 140 | + target_file_path = "test_atomic_file.txt" |
| 141 | + threads = [] |
| 142 | + num_threads = 8 |
| 143 | + data_chunks = [f"Data chunk {i}" for i in range(num_threads)] |
| 144 | + |
| 145 | + if os.path.exists(target_file_path): |
| 146 | + os.remove(target_file_path) |
| 147 | + |
| 148 | + for i in range(num_threads): |
| 149 | + thread = threading.Thread(target=thread_write_to_file, args=(target_file_path, data_chunks[i], i)) |
| 150 | + threads.append(thread) |
| 151 | + thread.start() |
| 152 | + |
| 153 | + for thread in threads: |
| 154 | + thread.join() |
| 155 | + |
| 156 | + with open(target_file_path, 'r') as file: |
| 157 | + lines = file.readlines() |
| 158 | + |
| 159 | + os.remove(target_file_path) |
| 160 | + |
| 161 | + assert any(data_chunk in line for line in lines for data_chunk in data_chunks), f"data_chunks data not found in the file" |
0 commit comments