|
| 1 | +import secrets |
| 2 | +import time |
| 3 | + |
| 4 | +from gradio_client import Client |
| 5 | + |
| 6 | +import trackio |
| 7 | +from trackio.sqlite_storage import SQLiteStorage |
| 8 | + |
| 9 | + |
| 10 | +def test_data_not_lost_on_transient_network_error( |
| 11 | + test_space_id, temp_dir, wait_for_client |
| 12 | +): |
| 13 | + """ |
| 14 | + When predict() fails once due to a transient network error and then |
| 15 | + recovers, the failed batch should be retried and all data should |
| 16 | + eventually reach the Space. |
| 17 | + """ |
| 18 | + project_name = f"test_transient_{secrets.token_urlsafe(8)}" |
| 19 | + run_name = "test_run" |
| 20 | + |
| 21 | + run = trackio.init(project=project_name, name=run_name, space_id=test_space_id) |
| 22 | + wait_for_client(run) |
| 23 | + |
| 24 | + original_predict = run._client.predict |
| 25 | + call_count = [0] |
| 26 | + |
| 27 | + def wrapped_predict(*args, **kwargs): |
| 28 | + call_count[0] += 1 |
| 29 | + if kwargs.get("api_name") == "/bulk_log" and call_count[0] <= 1: |
| 30 | + raise Exception("ReadTimeout: The read operation timed out") |
| 31 | + return original_predict(*args, **kwargs) |
| 32 | + |
| 33 | + run._client.predict = wrapped_predict |
| 34 | + |
| 35 | + trackio.log({"loss": 0.5}) |
| 36 | + trackio.log({"loss": 0.3}) |
| 37 | + time.sleep(5) |
| 38 | + trackio.finish() |
| 39 | + |
| 40 | + verify_client = Client(test_space_id) |
| 41 | + summary = verify_client.predict( |
| 42 | + project=project_name, run=run_name, api_name="/get_run_summary" |
| 43 | + ) |
| 44 | + assert summary["num_logs"] == 2 |
| 45 | + |
| 46 | + |
| 47 | +def test_failed_data_persisted_locally(test_space_id, temp_dir, wait_for_client): |
| 48 | + """ |
| 49 | + When predict() permanently fails (Space unreachable), data should be |
| 50 | + persisted to the local SQLite database as a fallback buffer so it is |
| 51 | + not lost. |
| 52 | + """ |
| 53 | + project_name = f"test_persist_{secrets.token_urlsafe(8)}" |
| 54 | + run_name = "test_run" |
| 55 | + |
| 56 | + run = trackio.init(project=project_name, name=run_name, space_id=test_space_id) |
| 57 | + wait_for_client(run) |
| 58 | + |
| 59 | + original_predict = run._client.predict |
| 60 | + |
| 61 | + def always_fail_writes(*args, **kwargs): |
| 62 | + if kwargs.get("api_name") in ("/bulk_log", "/bulk_log_system"): |
| 63 | + raise Exception("Connection refused") |
| 64 | + return original_predict(*args, **kwargs) |
| 65 | + |
| 66 | + run._client.predict = always_fail_writes |
| 67 | + |
| 68 | + trackio.log({"loss": 0.5}) |
| 69 | + trackio.log({"loss": 0.3}) |
| 70 | + time.sleep(3) |
| 71 | + trackio.finish() |
| 72 | + |
| 73 | + local_logs = SQLiteStorage.get_logs(project=project_name, run=run_name) |
| 74 | + assert len(local_logs) >= 2, ( |
| 75 | + f"Expected at least 2 logs persisted in local SQLite, got {len(local_logs)}" |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +def test_data_delivered_after_batch_sender_crash( |
| 80 | + test_space_id, temp_dir, wait_for_client |
| 81 | +): |
| 82 | + """ |
| 83 | + After a network error crashes the batch sender, subsequently-logged |
| 84 | + data should still be delivered to the Space (either by retrying within |
| 85 | + the same thread or by restarting the sender). |
| 86 | + """ |
| 87 | + project_name = f"test_crash_{secrets.token_urlsafe(8)}" |
| 88 | + run_name = "test_run" |
| 89 | + |
| 90 | + run = trackio.init(project=project_name, name=run_name, space_id=test_space_id) |
| 91 | + wait_for_client(run) |
| 92 | + |
| 93 | + original_predict = run._client.predict |
| 94 | + call_count = [0] |
| 95 | + |
| 96 | + def wrapped_predict(*args, **kwargs): |
| 97 | + call_count[0] += 1 |
| 98 | + if kwargs.get("api_name") == "/bulk_log" and call_count[0] == 1: |
| 99 | + raise Exception("ReadTimeout: The read operation timed out") |
| 100 | + return original_predict(*args, **kwargs) |
| 101 | + |
| 102 | + run._client.predict = wrapped_predict |
| 103 | + |
| 104 | + trackio.log({"loss": 0.5}) |
| 105 | + time.sleep(3) |
| 106 | + |
| 107 | + trackio.log({"loss": 0.3}) |
| 108 | + trackio.log({"loss": 0.1}) |
| 109 | + time.sleep(5) |
| 110 | + trackio.finish() |
| 111 | + |
| 112 | + verify_client = Client(test_space_id) |
| 113 | + summary = verify_client.predict( |
| 114 | + project=project_name, run=run_name, api_name="/get_run_summary" |
| 115 | + ) |
| 116 | + assert summary["num_logs"] >= 2, ( |
| 117 | + f"Expected at least 2 logs on Space after recovery, got {summary['num_logs']}" |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +def test_local_buffer_flushed_after_recovery(test_space_id, temp_dir, wait_for_client): |
| 122 | + """ |
| 123 | + When the connection recovers after several failures, data that was |
| 124 | + persisted in the local SQLite fallback buffer should be flushed to the |
| 125 | + Space and cleaned up from the local database. |
| 126 | + """ |
| 127 | + project_name = f"test_flush_{secrets.token_urlsafe(8)}" |
| 128 | + run_name = "test_run" |
| 129 | + |
| 130 | + run = trackio.init(project=project_name, name=run_name, space_id=test_space_id) |
| 131 | + wait_for_client(run) |
| 132 | + |
| 133 | + original_predict = run._client.predict |
| 134 | + call_count = [0] |
| 135 | + |
| 136 | + def wrapped_predict(*args, **kwargs): |
| 137 | + call_count[0] += 1 |
| 138 | + if kwargs.get("api_name") == "/bulk_log" and call_count[0] <= 3: |
| 139 | + raise Exception("Connection refused") |
| 140 | + return original_predict(*args, **kwargs) |
| 141 | + |
| 142 | + run._client.predict = wrapped_predict |
| 143 | + |
| 144 | + trackio.log({"loss": 0.5, "epoch": 1}) |
| 145 | + trackio.log({"loss": 0.3, "epoch": 2}) |
| 146 | + time.sleep(2) |
| 147 | + trackio.log({"loss": 0.1, "epoch": 3}) |
| 148 | + time.sleep(10) |
| 149 | + trackio.finish() |
| 150 | + |
| 151 | + verify_client = Client(test_space_id) |
| 152 | + summary = verify_client.predict( |
| 153 | + project=project_name, run=run_name, api_name="/get_run_summary" |
| 154 | + ) |
| 155 | + assert summary["num_logs"] == 3, ( |
| 156 | + f"Expected all 3 logs on Space after recovery, got {summary['num_logs']}" |
| 157 | + ) |
| 158 | + |
| 159 | + local_logs = SQLiteStorage.get_logs(project=project_name, run=run_name) |
| 160 | + assert len(local_logs) == 0, ( |
| 161 | + f"Expected local buffer to be empty after flush, but found {len(local_logs)} rows" |
| 162 | + ) |
0 commit comments