Skip to content

Commit 664df99

Browse files
authored
Merge pull request #1766 from codeflash-ai/fix-windows-trace-benchmark-test
fix: close SQLite connections in finally blocks for Windows compatibility
2 parents c9f506b + 77b705d commit 664df99

3 files changed

Lines changed: 17 additions & 20 deletions

File tree

tests/languages/javascript/test_support_dispatch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ def test_passes_loop_parameters(self, mock_vitest_runner: MagicMock, js_support:
182182

183183
call_kwargs = mock_vitest_runner.call_args.kwargs
184184
assert call_kwargs["min_loops"] == 10
185-
# JS/TS uses JS_BENCHMARKING_MAX_LOOPS (5_000) regardless of passed value
185+
# JS/TS uses JS_BENCHMARKING_MAX_LOOPS (1_000) regardless of passed value
186186
# Actual loop count is limited by target_duration, not max_loops
187-
assert call_kwargs["max_loops"] == 5_000
187+
assert call_kwargs["max_loops"] == 1_000
188188
assert call_kwargs["target_duration_ms"] == 5000
189189

190190

tests/test_languages/test_javascript_test_runner.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -907,15 +907,17 @@ def test_reporter_produces_valid_junit_xml(self):
907907

908908
# Create a Node.js script that exercises the reporter with mock data
909909
test_script = Path(tmpdir) / "test_reporter.js"
910+
reporter_path_js = reporter_path.as_posix()
911+
output_file_js = output_file.as_posix()
910912
test_script.write_text(f"""
911913
// Set env vars BEFORE requiring reporter (matches real Jest behavior)
912-
process.env.JEST_JUNIT_OUTPUT_FILE = '{output_file}';
914+
process.env.JEST_JUNIT_OUTPUT_FILE = '{output_file_js}';
913915
process.env.JEST_JUNIT_CLASSNAME = '{{filepath}}';
914916
process.env.JEST_JUNIT_SUITE_NAME = '{{filepath}}';
915917
process.env.JEST_JUNIT_ADD_FILE_ATTRIBUTE = 'true';
916918
process.env.JEST_JUNIT_INCLUDE_CONSOLE_OUTPUT = 'true';
917919
918-
const Reporter = require('{reporter_path}');
920+
const Reporter = require('{reporter_path_js}');
919921
920922
// Mock Jest globalConfig
921923
const globalConfig = {{ rootDir: '/tmp/project' }};
@@ -960,7 +962,7 @@ def test_reporter_produces_valid_junit_xml(self):
960962
reporter.onRunComplete([], results);
961963
962964
console.log('OK');
963-
""")
965+
""", encoding="utf-8")
964966

965967
result = subprocess.run(
966968
["node", str(test_script)],

tests/test_trace_benchmarks.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import shutil
22
import sqlite3
3-
import time
43
from pathlib import Path
54

65
import pytest
@@ -18,6 +17,7 @@ def test_trace_benchmarks() -> None:
1817
replay_tests_dir = benchmarks_root / "codeflash_replay_tests"
1918
tests_root = project_root / "tests"
2019
output_file = (benchmarks_root / Path("test_trace_benchmarks.trace")).resolve()
20+
conn: sqlite3.Connection | None = None
2121
trace_benchmarks_pytest(benchmarks_root, tests_root, project_root, output_file)
2222
assert output_file.exists()
2323
try:
@@ -121,8 +121,8 @@ def test_trace_benchmarks() -> None:
121121
assert actual[4] == expected[4], f"Mismatch at index {idx} for benchmark_function_name"
122122
assert actual[5] == expected[5], f"Mismatch at index {idx} for benchmark_module_path"
123123
assert actual[6] == expected[6], f"Mismatch at index {idx} for benchmark_line_number"
124-
# Close connection
125124
conn.close()
125+
conn = None
126126
generate_replay_test(output_file, replay_tests_dir)
127127
test_class_sort_path = replay_tests_dir / Path(
128128
"test_tests_pytest_benchmarks_test_test_benchmark_bubble_sort_example__replay_test_0.py"
@@ -217,7 +217,8 @@ def test_code_to_optimize_bubble_sort_codeflash_trace_sorter_test_no_func():
217217
"""
218218
assert test_sort_path.read_text("utf-8").strip() == test_sort_code.strip()
219219
finally:
220-
# cleanup
220+
if conn is not None:
221+
conn.close()
221222
output_file.unlink(missing_ok=True)
222223
shutil.rmtree(replay_tests_dir)
223224

@@ -231,6 +232,7 @@ def test_trace_multithreaded_benchmark() -> None:
231232
output_file = (benchmarks_root / Path("test_trace_benchmarks.trace")).resolve()
232233
trace_benchmarks_pytest(benchmarks_root, tests_root, project_root, output_file)
233234
assert output_file.exists()
235+
conn: sqlite3.Connection | None = None
234236
try:
235237
# check contents of trace file
236238
# connect to database
@@ -244,8 +246,6 @@ def test_trace_multithreaded_benchmark() -> None:
244246
)
245247
function_calls = cursor.fetchall()
246248

247-
conn.close()
248-
249249
# Assert the length of function calls
250250
assert len(function_calls) == 10, f"Expected 10 function calls, but got {len(function_calls)}"
251251
function_benchmark_timings = codeflash_benchmark_plugin.get_function_benchmark_timings(output_file)
@@ -281,11 +281,9 @@ def test_trace_multithreaded_benchmark() -> None:
281281
assert actual[4] == expected[4], f"Mismatch at index {idx} for benchmark_function_name"
282282
assert actual[5] == expected[5], f"Mismatch at index {idx} for benchmark_module_path"
283283
assert actual[6] == expected[6], f"Mismatch at index {idx} for benchmark_line_number"
284-
# Close connection
285-
conn.close()
286-
287284
finally:
288-
# cleanup
285+
if conn is not None:
286+
conn.close()
289287
output_file.unlink(missing_ok=True)
290288

291289

@@ -296,6 +294,7 @@ def test_trace_benchmark_decorator() -> None:
296294
output_file = (benchmarks_root / Path("test_trace_benchmarks.trace")).resolve()
297295
trace_benchmarks_pytest(benchmarks_root, tests_root, project_root, output_file)
298296
assert output_file.exists()
297+
conn: sqlite3.Connection | None = None
299298
try:
300299
# check contents of trace file
301300
# connect to database
@@ -352,11 +351,7 @@ def test_trace_benchmark_decorator() -> None:
352351
assert Path(actual[3]).name == Path(expected[3]).name, f"Mismatch at index {idx} for file_path"
353352
assert actual[4] == expected[4], f"Mismatch at index {idx} for benchmark_function_name"
354353
assert actual[5] == expected[5], f"Mismatch at index {idx} for benchmark_module_path"
355-
# Close connection
356-
cursor.close()
357-
conn.close()
358-
time.sleep(2)
359354
finally:
360-
# cleanup
355+
if conn is not None:
356+
conn.close()
361357
output_file.unlink(missing_ok=True)
362-
time.sleep(1)

0 commit comments

Comments
 (0)