-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_thread_safety_main_fix.py
More file actions
250 lines (185 loc) Β· 9.41 KB
/
test_thread_safety_main_fix.py
File metadata and controls
250 lines (185 loc) Β· 9.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
"""
Comprehensive thread safety test for main.py global variables fix.
This test verifies that the contextvars-based fix properly isolates state
between concurrent agents/contexts, preventing race conditions and cross-contamination.
"""
import asyncio
import contextvars
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from typing import List, Dict
# Import the thread-safe globals
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src', 'praisonai-agents'))
from praisonaiagents.main import (
error_logs,
sync_display_callbacks,
async_display_callbacks,
approval_callback,
register_display_callback,
register_approval_callback
)
def test_error_logs_isolation():
"""Test that error logs are isolated between different contexts."""
results = {}
def worker(worker_id):
# Each worker runs in its own context - need to create a fresh context for each worker
def worker_task():
worker_errors = [f"Error {i} from worker {worker_id}" for i in range(10)]
for error in worker_errors:
error_logs.append(error)
# Verify only this worker's errors are visible
current_errors = list(error_logs)
return current_errors
# Run the task in a new context
ctx = contextvars.copy_context()
current_errors = ctx.run(worker_task)
results[worker_id] = current_errors
# Verify no contamination from other workers
for error in current_errors:
assert f"worker {worker_id}" in error, f"Cross-contamination detected: {error}"
# Run 10 concurrent workers
with ThreadPoolExecutor(max_workers=10) as executor:
futures = [executor.submit(worker, i) for i in range(10)]
for future in futures:
future.result()
# Verify each worker saw only its own errors
for worker_id, errors in results.items():
assert len(errors) == 10, f"Worker {worker_id} saw {len(errors)} errors, expected 10"
for error in errors:
assert f"worker {worker_id}" in error, f"Worker {worker_id} saw foreign error: {error}"
print("β
Error logs isolation test passed")
def test_callback_isolation():
"""Test that display callbacks are isolated between contexts."""
results = {}
def worker(worker_id):
def worker_task():
# Each worker registers its own callback
def my_callback(message=None, **kwargs):
return f"Worker {worker_id} processed: {message}"
register_display_callback('test_event', my_callback)
# Verify only this worker's callback is registered
callback = sync_display_callbacks.get('test_event')
assert callback is not None, f"Worker {worker_id} callback not found"
# Test the callback
result = callback(message=f"test message {worker_id}")
return result
# Run in fresh context
ctx = contextvars.copy_context()
result = ctx.run(worker_task)
results[worker_id] = result
# Verify isolation
assert f"Worker {worker_id}" in result, f"Callback isolation failed for worker {worker_id}"
# Run 5 concurrent workers
with ThreadPoolExecutor(max_workers=5) as executor:
futures = [executor.submit(worker, i) for i in range(5)]
for future in futures:
future.result()
# Verify each worker got its own callback result
for worker_id, result in results.items():
assert f"Worker {worker_id}" in result, f"Callback result contamination for worker {worker_id}"
assert f"test message {worker_id}" in result, f"Message processing failed for worker {worker_id}"
print("β
Callback isolation test passed")
def test_approval_callback_isolation():
"""Test that approval callbacks are isolated between contexts."""
results = {}
def worker(worker_id):
def worker_task():
# Each worker sets its own approval callback
def my_approval(func_name, args, risk_level):
return f"Worker {worker_id} approved {func_name}"
register_approval_callback(my_approval)
# Verify this worker's callback is set
current_callback = approval_callback.get()
assert current_callback is not None, f"Worker {worker_id} approval callback not set"
# Test the callback
result = current_callback("test_func", {}, "low")
return result
# Run in fresh context
ctx = contextvars.copy_context()
result = ctx.run(worker_task)
results[worker_id] = result
# Verify isolation
assert f"Worker {worker_id}" in result, f"Approval callback isolation failed for worker {worker_id}"
# Run 5 concurrent workers
with ThreadPoolExecutor(max_workers=5) as executor:
futures = [executor.submit(worker, i) for i in range(5)]
for future in futures:
future.result()
# Verify each worker got its own approval result
for worker_id, result in results.items():
assert f"Worker {worker_id}" in result, f"Approval callback result contamination for worker {worker_id}"
print("β
Approval callback isolation test passed")
async def test_async_callback_isolation():
"""Test that async callbacks are also properly isolated."""
results = {}
async def worker(worker_id):
# Create a task within the current context - asyncio automatically copies context
async def worker_task():
# Each worker registers its own async callback
async def my_async_callback(message=None, **kwargs):
await asyncio.sleep(0.01) # Small async operation
return f"Async Worker {worker_id} processed: {message}"
register_display_callback('async_test_event', my_async_callback, is_async=True)
# Verify only this worker's callback is registered
callback = async_display_callbacks.get('async_test_event')
assert callback is not None, f"Async Worker {worker_id} callback not found"
# Test the callback
result = await callback(message=f"async test message {worker_id}")
return result
result = await worker_task()
results[worker_id] = result
# Verify isolation
assert f"Async Worker {worker_id}" in result, f"Async callback isolation failed for worker {worker_id}"
# Run 5 concurrent async workers - each will run in its own context copy
tasks = [worker(i) for i in range(5)]
await asyncio.gather(*tasks)
# Verify each worker got its own callback result
for worker_id, result in results.items():
assert f"Async Worker {worker_id}" in result, f"Async callback result contamination for worker {worker_id}"
assert f"async test message {worker_id}" in result, f"Async message processing failed for worker {worker_id}"
print("β
Async callback isolation test passed")
def test_context_inheritance():
"""Test that context variables provide proper isolation between parent and child contexts."""
# Test that contexts start clean by default
initial_errors = list(error_logs)
assert len(initial_errors) == 0, f"Expected clean context, found {len(initial_errors)} errors"
# Add something in the current context
error_logs.append("Current context error")
current_errors = list(error_logs)
assert len(current_errors) == 1, "Current context should have 1 error"
# Test that a new context starts fresh (doesn't inherit from this context unless explicitly copied)
def new_context_task():
new_errors = list(error_logs)
return len(new_errors)
ctx = contextvars.Context() # Create truly empty context
new_context_error_count = ctx.run(new_context_task)
# The new context should start with empty state (our ContextVar has default=[])
assert new_context_error_count == 0, f"New context should start empty, but had {new_context_error_count} errors"
# Original context should still have its error
final_current_errors = list(error_logs)
assert len(final_current_errors) == 1, "Original context should still have 1 error"
print("β
Context inheritance test passed")
def run_all_tests():
"""Run all thread safety tests."""
print("π§ͺ Running thread safety tests for main.py globals fix...\n")
# Run each test in its own clean context
def run_test_in_clean_context(test_func):
ctx = contextvars.copy_context()
ctx.run(test_func)
# Run sync tests
run_test_in_clean_context(test_error_logs_isolation)
run_test_in_clean_context(test_callback_isolation)
run_test_in_clean_context(test_approval_callback_isolation)
run_test_in_clean_context(test_context_inheritance)
# Run async tests in clean context too
def run_async_test():
asyncio.run(test_async_callback_isolation())
run_test_in_clean_context(run_async_test)
print("\nπ All thread safety tests passed!")
print("β
main.py global variables are now thread-safe with proper context isolation")
if __name__ == "__main__":
run_all_tests()