|
| 1 | +import os |
| 2 | +import random |
| 3 | +import sys |
| 4 | +import threading |
| 5 | +import time |
| 6 | + |
| 7 | +from aikido_zen.background_process.routes import Routes |
| 8 | +from aikido_zen.helpers.logging import logger |
| 9 | + |
| 10 | + |
| 11 | +def add_routes(routes, num_routes, error_count): |
| 12 | + """Function to add routes to the Routes object.""" |
| 13 | + try: |
| 14 | + for _ in range(num_routes): |
| 15 | + route_metadata = { |
| 16 | + "method": random.choice(["GET", "POST", "PUT", "DELETE"]), |
| 17 | + "route": f"/api/test/{random.randint(1, 10000)}", |
| 18 | + } |
| 19 | + routes.increment_route(route_metadata) |
| 20 | + except Exception: |
| 21 | + error_count["errors_add"] += 1 |
| 22 | + |
| 23 | + |
| 24 | +def export_routes(routes, error_count): |
| 25 | + """Function to export routes with hits.""" |
| 26 | + while True: |
| 27 | + try: |
| 28 | + routes.get_routes_with_hits() |
| 29 | + except Exception: |
| 30 | + error_count["errors_export"] += 1 |
| 31 | + |
| 32 | + |
| 33 | +def runtime_error_without_data_lock(): |
| 34 | + """Test function to check for runtime errors without data locking.""" |
| 35 | + routes = Routes(max_size=5000) |
| 36 | + |
| 37 | + # Create threads for adding routes and exporting routes |
| 38 | + num_add_threads = 30 |
| 39 | + num_routes_per_thread = 3000 |
| 40 | + num_export_threads = 20 |
| 41 | + error_count = { |
| 42 | + "errors_add": 0, |
| 43 | + "errors_export": 0, |
| 44 | + } |
| 45 | + |
| 46 | + add_threads = [] |
| 47 | + for i in range(num_add_threads): |
| 48 | + print(f"Adding thread {i} started") |
| 49 | + thread = threading.Thread( |
| 50 | + target=add_routes, |
| 51 | + args=(routes, num_routes_per_thread, error_count), |
| 52 | + daemon=True, |
| 53 | + ) |
| 54 | + add_threads.append(thread) |
| 55 | + thread.start() |
| 56 | + |
| 57 | + # Start the export thread |
| 58 | + export_threads = [] |
| 59 | + for i in range(num_export_threads): |
| 60 | + print(f"Export thread {i} started") |
| 61 | + thread = threading.Thread( |
| 62 | + target=export_routes, args=(routes, error_count), daemon=True |
| 63 | + ) |
| 64 | + export_threads.append(thread) |
| 65 | + thread.start() |
| 66 | + |
| 67 | + # Wait for all add threads to finish |
| 68 | + time.sleep(1) |
| 69 | + |
| 70 | + for thread in add_threads: |
| 71 | + print("Closing add thread: " + thread.name) |
| 72 | + thread.join(timeout=20 / 1000) |
| 73 | + |
| 74 | + # Stop the export thread after adding routes |
| 75 | + for thread in export_threads: |
| 76 | + print("Closing export thread: " + thread.name) |
| 77 | + thread.join(timeout=20 / 1000) |
| 78 | + |
| 79 | + print(error_count) |
| 80 | + assert error_count["errors_add"] == 0 |
| 81 | + assert error_count["errors_export"] == 0 |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + runtime_error_without_data_lock() |
0 commit comments