mirror of
https://github.com/cnlimiter/codex-register.git
synced 2026-05-11 18:10:53 +08:00
fix(logic): isolate atomic batch counters and token sync fields
This commit is contained in:
72
tests/test_account_token_sync_status.py
Normal file
72
tests/test_account_token_sync_status.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from src.database import crud
|
||||
from src.database.session import DatabaseSessionManager
|
||||
|
||||
|
||||
def test_create_account_marks_token_sync_pending_when_tokens_persist(tmp_path):
|
||||
manager = DatabaseSessionManager(f"sqlite:///{tmp_path}/test.db")
|
||||
manager.create_tables()
|
||||
manager.migrate_tables()
|
||||
|
||||
with manager.session_scope() as session:
|
||||
account = crud.create_account(
|
||||
session,
|
||||
email="sync@example.com",
|
||||
email_service="tempmail",
|
||||
access_token="access-token",
|
||||
refresh_token="refresh-token",
|
||||
)
|
||||
|
||||
assert account.token_sync_status == "pending"
|
||||
assert account.token_sync_updated_at is not None
|
||||
|
||||
|
||||
def test_update_account_marks_token_sync_pending_when_tokens_change(tmp_path):
|
||||
manager = DatabaseSessionManager(f"sqlite:///{tmp_path}/test.db")
|
||||
manager.create_tables()
|
||||
manager.migrate_tables()
|
||||
|
||||
with manager.session_scope() as session:
|
||||
account = crud.create_account(
|
||||
session,
|
||||
email="nosync@example.com",
|
||||
email_service="tempmail",
|
||||
)
|
||||
|
||||
assert account.token_sync_status == "not_ready"
|
||||
|
||||
updated = crud.update_account(
|
||||
session,
|
||||
account.id,
|
||||
access_token="new-access-token",
|
||||
)
|
||||
|
||||
assert updated is not None
|
||||
assert updated.token_sync_status == "pending"
|
||||
assert updated.token_sync_updated_at is not None
|
||||
|
||||
|
||||
def test_update_account_preserves_pending_status_when_other_tokens_remain(tmp_path):
|
||||
manager = DatabaseSessionManager(f"sqlite:///{tmp_path}/test.db")
|
||||
manager.create_tables()
|
||||
manager.migrate_tables()
|
||||
|
||||
with manager.session_scope() as session:
|
||||
account = crud.create_account(
|
||||
session,
|
||||
email="partial-sync@example.com",
|
||||
email_service="tempmail",
|
||||
access_token="access-token",
|
||||
refresh_token="refresh-token",
|
||||
)
|
||||
|
||||
updated = crud.update_account(
|
||||
session,
|
||||
account.id,
|
||||
refresh_token="",
|
||||
)
|
||||
|
||||
assert updated is not None
|
||||
assert updated.access_token == "access-token"
|
||||
assert updated.refresh_token == ""
|
||||
assert updated.token_sync_status == "pending"
|
||||
assert updated.token_sync_updated_at is not None
|
||||
21
tests/test_batch_task_manager.py
Normal file
21
tests/test_batch_task_manager.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
from src.web.task_manager import task_manager
|
||||
|
||||
|
||||
def test_record_batch_task_result_is_atomic_under_threads():
|
||||
batch_id = "batch-atomic-test"
|
||||
task_manager.init_batch(batch_id, 100)
|
||||
|
||||
statuses = ["completed"] * 60 + ["failed"] * 40
|
||||
|
||||
with ThreadPoolExecutor(max_workers=16) as executor:
|
||||
list(executor.map(lambda status: task_manager.record_batch_task_result(batch_id, status), statuses))
|
||||
|
||||
snapshot = task_manager.get_batch_status(batch_id)
|
||||
|
||||
assert snapshot is not None
|
||||
assert snapshot["completed"] == 100
|
||||
assert snapshot["success"] == 60
|
||||
assert snapshot["failed"] == 40
|
||||
assert snapshot["skipped"] == 0
|
||||
Reference in New Issue
Block a user