mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-03 01:40:21 +08:00
fix(gateway): 修复 balance 刷新去重键冲突、usage 状态回退与测试竞态
- balance_cache: 引入实例级 refresh key 防止多实例共享进程级 HashSet 冲突 - InMemoryUsageRepo: 阻止 pending/streaming 状态覆盖已终结(completed/failed/cancelled)记录 - usage 同步测试: 等待条件从 is_some() 改为检查 status=="completed" 避免竞态 - wallet 测试: 增加轮询等待 wallet 扣款完成 - 整理 import 语句与 tests 模块位置
This commit is contained in:
@@ -111,7 +111,10 @@ async fn gateway_records_usage_for_execution_runtime_sync_when_runtime_enabled()
|
||||
.find_by_request_id("req-usage-sync-123")
|
||||
.await
|
||||
.expect("usage lookup should succeed");
|
||||
if stored.is_some() {
|
||||
if stored
|
||||
.as_ref()
|
||||
.is_some_and(|usage| usage.status == "completed")
|
||||
{
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
|
||||
|
||||
@@ -309,7 +309,10 @@ async fn gateway_truncates_deep_request_echo_for_local_openai_chat_sync_usage()
|
||||
.find_by_request_id("trace-openai-chat-local-report-sync-deep-123")
|
||||
.await
|
||||
.expect("usage lookup should succeed");
|
||||
if stored_usage.is_some() {
|
||||
if stored_usage
|
||||
.as_ref()
|
||||
.is_some_and(|usage| usage.status == "completed")
|
||||
{
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
|
||||
|
||||
@@ -145,7 +145,10 @@ async fn gateway_settles_wallet_for_completed_execution_runtime_sync_usage() {
|
||||
.find_by_request_id("req-usage-wallet-sync-123")
|
||||
.await
|
||||
.expect("usage lookup should succeed");
|
||||
if stored.is_some() {
|
||||
if stored
|
||||
.as_ref()
|
||||
.is_some_and(|usage| usage.status == "completed")
|
||||
{
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
|
||||
@@ -154,11 +157,20 @@ async fn gateway_settles_wallet_for_completed_execution_runtime_sync_usage() {
|
||||
assert_eq!(stored.status, "completed");
|
||||
assert_eq!(stored.total_tokens, 1500);
|
||||
|
||||
let wallet = wallet_repository
|
||||
.find(WalletLookupKey::UserId("user-usage-sync-123"))
|
||||
.await
|
||||
.expect("wallet lookup should succeed")
|
||||
.expect("wallet should exist");
|
||||
let mut wallet = None;
|
||||
for _ in 0..50 {
|
||||
wallet = wallet_repository
|
||||
.find(WalletLookupKey::UserId("user-usage-sync-123"))
|
||||
.await
|
||||
.expect("wallet lookup should succeed");
|
||||
if wallet.as_ref().is_some_and(|wallet| {
|
||||
wallet.balance < 10.0 || wallet.gift_balance < 2.0 || wallet.total_consumed > 0.0
|
||||
}) {
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
|
||||
}
|
||||
let wallet = wallet.expect("wallet should exist");
|
||||
assert!(wallet.balance < 10.0 || wallet.gift_balance < 2.0);
|
||||
assert!(wallet.total_consumed > 0.0);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user