Skip to content

Commit 213e0f7

Browse files
committed
fixed flaky test by setting up an event loop to test Evaluator
1 parent 45e3452 commit 213e0f7

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

tests/test_artifacts.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Test suite for artifacts functionality
33
"""
44

5+
import asyncio
56
import os
67
import tempfile
78
import unittest
@@ -135,6 +136,13 @@ class TestEvaluatorArtifacts(unittest.TestCase):
135136
"""Test artifact handling in the evaluator"""
136137

137138
def setUp(self):
139+
# Set up event loop for async operations in tests
140+
try:
141+
self.loop = asyncio.get_event_loop()
142+
except RuntimeError:
143+
self.loop = asyncio.new_event_loop()
144+
asyncio.set_event_loop(self.loop)
145+
138146
# Create a mock evaluation file
139147
self.temp_eval_file = tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False)
140148
self.temp_eval_file.write(
@@ -150,10 +158,18 @@ def evaluate(program_path):
150158

151159
def tearDown(self):
152160
os.unlink(self.temp_eval_file.name)
161+
# Clean up event loop if we created one
162+
if hasattr(self, "loop") and self.loop and not self.loop.is_closed():
163+
# Cancel any pending tasks
164+
pending = asyncio.all_tasks(self.loop)
165+
for task in pending:
166+
task.cancel()
167+
# Run the loop briefly to let cancellations process
168+
if pending:
169+
self.loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
153170

154171
def test_evaluate_program_backward_compatibility(self):
155172
"""Test that old evaluators still work unchanged"""
156-
import asyncio
157173

158174
async def run_test():
159175
result = await self.evaluator.evaluate_program("print('test')", "test_id")
@@ -206,7 +222,7 @@ def test_render_artifacts_all_items(self):
206222
"""Test that all artifacts are included using .items() without prioritization"""
207223
artifacts = {
208224
"stderr": "error message",
209-
"stdout": "output message",
225+
"stdout": "output message",
210226
"traceback": "stack trace",
211227
"other": "other data",
212228
}
@@ -216,7 +232,7 @@ def test_render_artifacts_all_items(self):
216232
# All artifacts should be present (no prioritization)
217233
for key in artifacts.keys():
218234
self.assertIn(key, rendered)
219-
235+
220236
# Check that all content is included
221237
for value in artifacts.values():
222238
self.assertIn(value, rendered)

tests/test_artifacts_integration.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ class TestArtifactsIntegration(unittest.TestCase):
1919
"""Test full integration of artifacts feature"""
2020

2121
def setUp(self):
22+
# Set up event loop for async operations in tests
23+
try:
24+
self.loop = asyncio.get_event_loop()
25+
except RuntimeError:
26+
self.loop = asyncio.new_event_loop()
27+
asyncio.set_event_loop(self.loop)
28+
2229
# Create temporary directory for database
2330
self.temp_dir = tempfile.mkdtemp()
2431

@@ -86,6 +93,15 @@ def evaluate_stage1(program_path):
8693

8794
def tearDown(self):
8895
os.unlink(self.eval_file.name)
96+
# Clean up event loop if we created one
97+
if hasattr(self, "loop") and self.loop and not self.loop.is_closed():
98+
# Cancel any pending tasks
99+
pending = asyncio.all_tasks(self.loop)
100+
for task in pending:
101+
task.cancel()
102+
# Run the loop briefly to let cancellations process
103+
if pending:
104+
self.loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
89105

90106
def test_compile_failure_artifact_capture(self):
91107
"""Test that compilation failures are captured as artifacts"""
@@ -244,10 +260,28 @@ class TestArtifactsPersistence(unittest.TestCase):
244260
"""Test that artifacts persist correctly across save/load cycles"""
245261

246262
def setUp(self):
263+
# Set up event loop for async operations in tests
264+
try:
265+
self.loop = asyncio.get_event_loop()
266+
except RuntimeError:
267+
self.loop = asyncio.new_event_loop()
268+
asyncio.set_event_loop(self.loop)
269+
247270
self.temp_dir = tempfile.mkdtemp()
248271
config = DatabaseConfig(db_path=self.temp_dir)
249272
self.database = ProgramDatabase(config)
250273

274+
def tearDown(self):
275+
# Clean up event loop if we created one
276+
if hasattr(self, "loop") and self.loop and not self.loop.is_closed():
277+
# Cancel any pending tasks
278+
pending = asyncio.all_tasks(self.loop)
279+
for task in pending:
280+
task.cancel()
281+
# Run the loop briefly to let cancellations process
282+
if pending:
283+
self.loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
284+
251285
def test_save_load_artifacts(self):
252286
"""Test that artifacts survive database save/load cycle"""
253287
# Create program with artifacts

0 commit comments

Comments
 (0)