Skip to content

Commit 3a201fc

Browse files
authored
Test AtomicWriteFileHandle with test_utils.py
1 parent b34be8e commit 3a201fc

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

tools/accuracy_checker/tests/test_utils.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
limitations under the License.
1515
"""
1616

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
1820

1921

2022
def test_concat_lists():
@@ -125,3 +127,35 @@ class C:
125127

126128
assert overrides(B, 'foo', A)
127129
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

Comments
 (0)