use super::*;
#[cfg(feature = "protocol-apikey")]
pub(crate) use super::glob_match;
pub use super::MockDao;
use crate::error::GarrisonError;
use parking_lot::Mutex;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
#[cfg(all(
feature = "anomalous-detector-dual",
any(feature = "cache-memory", feature = "cache-redis")
))]
mod oxcache_keys_tests {
use super::*;
#[tokio::test(flavor = "multi_thread")]
async fn test_oxcache_keys_empty() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
let keys = dao.keys("anomalous:login:*").await.unwrap();
assert!(keys.is_empty(), "无 key 时 keys() 应返回空 Vec");
}
#[tokio::test(flavor = "multi_thread")]
async fn test_oxcache_keys_pattern_match() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("anomalous:login:1:1", "v1", 3600).await.unwrap();
dao.set("anomalous:login:2:2", "v2", 3600).await.unwrap();
dao.set("other:key", "v3", 3600).await.unwrap();
let mut keys = dao.keys("anomalous:login:*").await.unwrap();
keys.sort();
assert_eq!(
keys,
vec![
"anomalous:login:1:1".to_string(),
"anomalous:login:2:2".to_string()
],
"keys() 应返回 2 个匹配 anomalous:login:* 的 key"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_oxcache_keys_clears_expired() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("anomalous:login:1:1", "v1", 1).await.unwrap();
tokio::time::sleep(Duration::from_secs(2)).await;
let keys = dao.keys("anomalous:login:*").await.unwrap();
assert!(
keys.is_empty(),
"TTL 过期后 keys() 应返回空 Vec(惰性清理)"
);
let keys2 = dao.keys("anomalous:login:*").await.unwrap();
assert!(keys2.is_empty(), "清理后再次 keys() 仍应返回空");
}
#[tokio::test(flavor = "multi_thread")]
async fn test_oxcache_keys_after_delete() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("anomalous:login:1:1", "v1", 3600).await.unwrap();
let keys = dao.keys("anomalous:login:*").await.unwrap();
assert_eq!(keys.len(), 1, "set 后应有 1 个 key");
dao.delete("anomalous:login:1:1").await.unwrap();
let keys = dao.keys("anomalous:login:*").await.unwrap();
assert!(keys.is_empty(), "delete 后 keys() 应返回空 Vec");
}
}
#[tokio::test]
async fn mock_set_get_pair() {
let dao = MockDao::new();
dao.set("key1", "value1", 3600).await.unwrap();
let got = dao.get("key1").await.unwrap();
assert_eq!(got, Some("value1".to_string()));
}
#[tokio::test]
async fn mock_expire_auto_delete() {
let dao = MockDao::new();
dao.set("key1", "value1", 1).await.unwrap();
tokio::time::sleep(Duration::from_secs(2)).await;
let got = dao.get("key1").await.unwrap();
assert!(got.is_none(), "过期后 get 应返回 None");
}
#[tokio::test]
async fn mock_delete_removes_key() {
let dao = MockDao::new();
dao.set("key1", "value1", 3600).await.unwrap();
dao.delete("key1").await.unwrap();
let got = dao.get("key1").await.unwrap();
assert!(got.is_none(), "delete 后 get 应返回 None");
}
#[tokio::test]
async fn mock_update_preserves_ttl() {
let dao = MockDao::new();
dao.set("key1", "value1", 2).await.unwrap();
dao.update("key1", "value2").await.unwrap();
let got = dao.get("key1").await.unwrap();
assert_eq!(got, Some("value2".to_string()));
tokio::time::sleep(Duration::from_secs(3)).await;
let got = dao.get("key1").await.unwrap();
assert!(
got.is_none(),
"update 不应重置 TTL,原 TTL 过期后应返回 None"
);
}
#[tokio::test]
async fn mock_update_missing_key_errors() {
let dao = MockDao::new();
let result = dao.update("missing", "value").await;
assert!(
matches!(result, Err(GarrisonError::Dao(_))),
"update 不存在的键应返回 Dao 错误"
);
}
#[tokio::test]
async fn mock_expire_resets_ttl() {
let dao = MockDao::new();
dao.set("key1", "value1", 1).await.unwrap();
dao.expire("key1", 3600).await.unwrap();
tokio::time::sleep(Duration::from_secs(2)).await;
let got = dao.get("key1").await.unwrap();
assert_eq!(got, Some("value1".to_string()));
}
#[tokio::test]
async fn mock_expire_missing_key_errors() {
let dao = MockDao::new();
let result = dao.expire("missing", 3600).await;
assert!(
matches!(result, Err(GarrisonError::Dao(_))),
"expire 不存在的键应返回 Dao 错误"
);
}
#[tokio::test]
async fn mock_set_zero_ttl_means_permanent() {
let dao = MockDao::new();
dao.set("perm", "value", 0).await.unwrap();
tokio::time::sleep(Duration::from_millis(10)).await;
let got = dao.get("perm").await.unwrap();
assert_eq!(got, Some("value".to_string()));
}
#[tokio::test]
async fn mock_get_missing_returns_none() {
let dao = MockDao::new();
let got = dao.get("never_set").await.unwrap();
assert!(got.is_none());
}
#[tokio::test]
async fn mock_dao_default_equals_new() {
let dao = MockDao::default();
dao.set("default_key", "default_value", 60).await.unwrap();
let got = dao.get("default_key").await.unwrap();
assert_eq!(got, Some("default_value".to_string()));
}
#[tokio::test]
async fn mock_expire_zero_seconds_means_permanent() {
let dao = MockDao::new();
dao.set("k", "v", 1).await.unwrap();
dao.expire("k", 0).await.unwrap();
tokio::time::sleep(Duration::from_secs(2)).await;
let got = dao.get("k").await.unwrap();
assert_eq!(got, Some("v".to_string()), "expire(0) 应改为永久驻留");
}
#[tokio::test]
async fn mock_set_permanent_persists_value() {
let dao = MockDao::new();
dao.set_permanent("perm_key", "perm_value").await.unwrap();
let got = dao.get("perm_key").await.unwrap();
assert_eq!(got, Some("perm_value".to_string()));
}
#[tokio::test]
async fn mock_set_permanent_does_not_expire_quickly() {
let dao = MockDao::new();
dao.set_permanent("perm_key", "perm_value").await.unwrap();
tokio::time::sleep(Duration::from_millis(50)).await;
let got = dao.get("perm_key").await.unwrap();
assert_eq!(got, Some("perm_value".to_string()), "永久键不应过期");
}
#[tokio::test]
async fn mock_get_timeout_returns_none_for_permanent_key() {
let dao = MockDao::new();
dao.set_permanent("perm", "v").await.unwrap();
let timeout = dao.get_timeout("perm").await.unwrap();
assert!(timeout.is_none(), "永久键应返回 None");
}
#[tokio::test]
async fn mock_get_timeout_returns_some_for_ttl_key() {
let dao = MockDao::new();
dao.set("ttl_key", "v", 3600).await.unwrap();
let timeout = dao.get_timeout("ttl_key").await.unwrap();
assert!(timeout.is_some(), "TTL 键应返回 Some");
let remaining = timeout.unwrap();
assert!(
remaining <= Duration::from_secs(3600),
"剩余时间应 ≤ 原 TTL"
);
}
#[tokio::test]
async fn mock_get_timeout_returns_none_for_missing_key() {
let dao = MockDao::new();
let timeout = dao.get_timeout("missing").await.unwrap();
assert!(timeout.is_none(), "不存在的键应返回 None");
}
#[tokio::test]
async fn mock_keys_returns_namespace_matches() {
let dao = MockDao::new();
dao.set("garrison:apikey:abc123", "v1", 3600).await.unwrap();
dao.set("garrison:apikey:def456", "v2", 3600).await.unwrap();
dao.set("garrison:session:xyz", "v3", 3600).await.unwrap();
let keys = dao.keys("garrison:apikey:*").await.unwrap();
assert_eq!(keys.len(), 2, "应匹配 2 个 apikey");
assert!(keys.contains(&"garrison:apikey:abc123".to_string()));
assert!(keys.contains(&"garrison:apikey:def456".to_string()));
}
#[tokio::test]
async fn mock_keys_star_returns_all() {
let dao = MockDao::new();
dao.set("k1", "v1", 3600).await.unwrap();
dao.set("k2", "v2", 3600).await.unwrap();
let keys = dao.keys("*").await.unwrap();
assert!(keys.len() >= 2, "应至少返回 2 个 key");
}
#[tokio::test]
async fn mock_keys_no_match_returns_empty() {
let dao = MockDao::new();
dao.set("k1", "v1", 3600).await.unwrap();
let keys = dao.keys("nonexistent:*").await.unwrap();
assert!(keys.is_empty(), "无匹配应返回空 Vec");
}
#[tokio::test]
async fn mock_keys_supports_question_mark() {
let dao = MockDao::new();
dao.set("key1", "v1", 3600).await.unwrap();
dao.set("key2", "v2", 3600).await.unwrap();
dao.set("key10", "v3", 3600).await.unwrap();
let keys = dao.keys("key?").await.unwrap();
assert_eq!(
keys.len(),
2,
"? 应匹配单个字符,key1/key2 匹配,key10 不匹配"
);
}
#[tokio::test]
async fn mock_rename_moves_key() {
let dao = MockDao::new();
dao.set("old_key", "value", 3600).await.unwrap();
dao.rename("old_key", "new_key").await.unwrap();
let old = dao.get("old_key").await.unwrap();
let new = dao.get("new_key").await.unwrap();
assert!(old.is_none(), "rename 后 old_key 应不存在");
assert_eq!(new, Some("value".to_string()), "rename 后 new_key 应有值");
}
#[tokio::test]
async fn mock_rename_missing_key_returns_invalid_param() {
let dao = MockDao::new();
let result = dao.rename("missing", "new").await;
assert!(
matches!(result, Err(GarrisonError::InvalidParam(_))),
"rename 不存在的键应返回 InvalidParam,实际: {:?}",
result
);
}
#[cfg(any(feature = "cache-memory", feature = "cache-redis"))]
mod oxcache_tests {
use super::*;
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_set_get_pair() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("oc_key1", "value1", 3600).await.unwrap();
let got = dao.get("oc_key1").await.unwrap();
assert_eq!(got, Some("value1".to_string()));
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_expire_auto_delete() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("oc_key2", "value1", 1).await.unwrap();
tokio::time::sleep(Duration::from_secs(2)).await;
let got = dao.get("oc_key2").await.unwrap();
assert!(got.is_none(), "过期后 get 应返回 None");
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_delete_removes_key() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("oc_key3", "value1", 3600).await.unwrap();
dao.delete("oc_key3").await.unwrap();
let got = dao.get("oc_key3").await.unwrap();
assert!(got.is_none(), "delete 后 get 应返回 None");
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_update_changes_value() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("oc_key4", "value1", 3600).await.unwrap();
dao.update("oc_key4", "value2").await.unwrap();
let got = dao.get("oc_key4").await.unwrap();
assert_eq!(got, Some("value2".to_string()));
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_update_missing_key_errors() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
let result = dao.update("oc_missing", "value").await;
assert!(
matches!(result, Err(GarrisonError::Dao(_))),
"update 不存在的键应返回 Dao 错误"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_expire_resets_ttl() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("oc_key5", "value1", 1).await.unwrap();
dao.expire("oc_key5", 3600).await.unwrap();
tokio::time::sleep(Duration::from_secs(2)).await;
let got = dao.get("oc_key5").await.unwrap();
assert_eq!(got, Some("value1".to_string()));
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_new_direct_construction() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("oc_init", "init_value", 60).await.unwrap();
let got = dao.get("oc_init").await.unwrap();
assert_eq!(got, Some("init_value".to_string()));
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_update_preserves_ttl() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("oc_ttl", "value1", 2).await.unwrap();
dao.update("oc_ttl", "value2").await.unwrap();
tokio::time::sleep(Duration::from_secs(3)).await;
let got = dao.get("oc_ttl").await.unwrap();
assert!(
got.is_none(),
"update 不应重置 TTL,原 TTL 过期后应返回 None"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_expire_zero_seconds_makes_permanent() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("oc_perm", "value1", 1).await.unwrap();
dao.expire("oc_perm", 0).await.unwrap();
tokio::time::sleep(Duration::from_secs(2)).await;
let got = dao.get("oc_perm").await.unwrap();
assert_eq!(
got,
Some("value1".to_string()),
"expire(0) 应将键改为永久驻留,不应过期"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_expire_zero_seconds_missing_key_errors() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
let result = dao.expire("oc_missing_perm", 0).await;
assert!(
matches!(result, Err(GarrisonError::Dao(_))),
"expire(0) 不存在的键应返回 Dao 错误"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_expire_missing_key_errors() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
let result = dao.expire("oc_missing_expire", 3600).await;
assert!(
matches!(result, Err(GarrisonError::Dao(ref msg)) if msg.contains("dao-key-missing")),
"expire 不存在的键应返回含 'dao-key-missing' 的 Dao 错误,实际: {:?}",
result
);
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_set_with_zero_ttl() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("oc_zero_ttl", "permanent_value", 0).await.unwrap();
tokio::time::sleep(Duration::from_secs(2)).await;
let got = dao.get("oc_zero_ttl").await.unwrap();
assert_eq!(
got,
Some("permanent_value".to_string()),
"set(ttl=0) 应写入永久驻留的键,2 秒后仍应存在"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_set_permanent_persists_without_ttl() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set_permanent("oc_perm", "perm_value").await.unwrap();
tokio::time::sleep(Duration::from_secs(2)).await;
let got = dao.get("oc_perm").await.unwrap();
assert_eq!(
got,
Some("perm_value".to_string()),
"set_permanent 应写入永久键,2 秒后仍应存在"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_get_timeout_returns_none_for_permanent_key() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set_permanent("oc_perm_ttl", "v").await.unwrap();
let timeout = dao.get_timeout("oc_perm_ttl").await.unwrap();
assert!(timeout.is_none(), "永久键应返回 None");
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_get_timeout_returns_some_for_ttl_key() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("oc_ttl_key", "v", 3600).await.unwrap();
let timeout = dao.get_timeout("oc_ttl_key").await.unwrap();
assert!(timeout.is_some(), "TTL 键应返回 Some");
let remaining = timeout.unwrap();
assert!(
remaining <= Duration::from_secs(3600),
"剩余时间应 ≤ 原 TTL"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_get_timeout_returns_none_for_missing_key() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
let timeout = dao.get_timeout("oc_missing_ttl").await.unwrap();
assert!(timeout.is_none(), "不存在的键应返回 None");
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_keys_behavior() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("oc_key1", "v1", 3600).await.unwrap();
let result = dao.keys("oc_*").await;
#[cfg(feature = "anomalous-detector-dual")]
{
let keys = result.expect("anomalous-detector-dual 启用时 keys() 应返回 key 列表");
assert!(
keys.iter().any(|k| k.contains("oc_key1")),
"keys 应包含 oc_key1, 实际: {:?}",
keys
);
}
#[cfg(not(feature = "anomalous-detector-dual"))]
{
assert!(
matches!(result, Err(GarrisonError::NotImplemented(_))),
"未启用 anomalous-detector-dual 时 keys() 应返回 NotImplemented, 实际: {:?}",
result
);
}
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_rename_moves_key() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("oc_old", "value", 3600).await.unwrap();
dao.rename("oc_old", "oc_new").await.unwrap();
let old = dao.get("oc_old").await.unwrap();
let new = dao.get("oc_new").await.unwrap();
assert!(old.is_none(), "rename 后 oc_old 应不存在");
assert_eq!(new, Some("value".to_string()), "rename 后 oc_new 应有值");
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_rename_missing_key_returns_invalid_param() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
let result = dao.rename("oc_missing_old", "oc_new").await;
assert!(
matches!(result, Err(GarrisonError::InvalidParam(_))),
"rename 不存在的键应返回 InvalidParam,实际: {:?}",
result
);
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_rename_preserves_ttl() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("oc_short_ttl", "value", 2).await.unwrap();
dao.rename("oc_short_ttl", "oc_renamed").await.unwrap();
let got = dao.get("oc_renamed").await.unwrap();
assert_eq!(got, Some("value".to_string()));
tokio::time::sleep(Duration::from_secs(3)).await;
let got = dao.get("oc_renamed").await.unwrap();
assert!(
got.is_none(),
"rename 应保留原 TTL,原 TTL 过期后应返回 None"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_get_and_delete_returns_value_and_removes_key() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("oc_atomic", "value", 3600).await.unwrap();
let got = dao.get_and_delete("oc_atomic").await.unwrap();
assert_eq!(got, Some("value".to_string()));
let after = dao.get("oc_atomic").await.unwrap();
assert!(after.is_none(), "get_and_delete 后 key 应不存在");
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_get_and_delete_missing_returns_none() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
let got = dao.get_and_delete("oc_missing").await.unwrap();
assert!(got.is_none());
}
#[tokio::test(flavor = "multi_thread")]
async fn oxcache_get_and_delete_concurrent_only_one_succeeds() {
let dao = Arc::new(GarrisonDaoOxcache::new().await.unwrap());
dao.set("oc_concurrent", "value", 3600).await.unwrap();
let mut handles = Vec::new();
for _ in 0..10 {
let d = dao.clone();
handles.push(tokio::spawn(async move {
d.get_and_delete("oc_concurrent").await
}));
}
let mut success = 0;
let mut none_count = 0;
for handle in handles {
let result = handle.await.unwrap();
match result {
Ok(Some(_)) => success += 1,
Ok(None) => none_count += 1,
Err(e) => panic!("get_and_delete 不应返回错误: {:?}", e),
}
}
assert_eq!(success, 1, "并发调用仅一个返回 Some");
assert_eq!(none_count, 9, "其他 9 个返回 None");
}
#[cfg(feature = "tenant-isolation")]
#[tokio::test(flavor = "multi_thread")]
async fn dao_key_prefixed_with_tenant_when_isolation_enabled() {
use crate::context::tenant::{TenantContext, TenantSource, TENANT};
let dao = GarrisonDaoOxcache::new().await.unwrap();
let ctx_42 = TenantContext {
tenant_id: 42,
resolved_from: TenantSource::Header,
};
TENANT
.scope(ctx_42.clone(), async {
dao.set("shared_key", "tenant_42_value", 3600)
.await
.unwrap();
let got = dao.get("shared_key").await.unwrap();
assert_eq!(
got,
Some("tenant_42_value".to_string()),
"同租户 get 应命中 set 写入的值(前缀一致)"
);
})
.await;
let ctx_1 = TenantContext {
tenant_id: 1,
resolved_from: TenantSource::Header,
};
TENANT
.scope(ctx_1, async {
let got = dao.get("shared_key").await.unwrap();
assert!(
got.is_none(),
"跨租户 get 应返回 None(隔离失败),实际: {:?}",
got
);
dao.set("shared_key", "tenant_1_value", 3600).await.unwrap();
let got_self = dao.get("shared_key").await.unwrap();
assert_eq!(
got_self,
Some("tenant_1_value".to_string()),
"tenant 1 应读到自己的值"
);
})
.await;
TENANT
.scope(ctx_42.clone(), async {
let got = dao.get("shared_key").await.unwrap();
assert_eq!(
got,
Some("tenant_42_value".to_string()),
"tenant 42 的值不应被 tenant 1 覆盖(隔离失败)"
);
})
.await;
}
#[cfg(feature = "tenant-isolation")]
#[tokio::test(flavor = "multi_thread")]
async fn dao_key_unchanged_when_tenant_context_absent() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
dao.set("no_ctx_key", "value", 3600).await.unwrap();
let got = dao.get("no_ctx_key").await.unwrap();
assert_eq!(
got,
Some("value".to_string()),
"TENANT 上下文不存在时 key 应保持原样(无前缀)"
);
let prefixed = dao.get("tenant:0:no_ctx_key").await.unwrap();
assert!(
prefixed.is_none(),
"TENANT 上下文不存在时不应有带前缀的 key"
);
}
#[cfg(feature = "tenant-isolation")]
#[tokio::test(flavor = "multi_thread")]
async fn dao_delete_uses_prefixed_key_in_tenant_context() {
use crate::context::tenant::{TenantContext, TenantSource, TENANT};
let dao = GarrisonDaoOxcache::new().await.unwrap();
let ctx = TenantContext {
tenant_id: 42,
resolved_from: TenantSource::Header,
};
TENANT
.scope(ctx, async {
dao.set("del_key", "value", 3600).await.unwrap();
assert_eq!(
dao.get("del_key").await.unwrap(),
Some("value".to_string()),
"set 后同租户 get 应命中"
);
dao.delete("del_key").await.unwrap();
let after = dao.get("del_key").await.unwrap();
assert!(
after.is_none(),
"delete 后同租户 get 应返回 None(delete 也加了前缀)"
);
})
.await;
}
}
#[tokio::test]
async fn mock_get_and_delete_returns_value_and_removes_key() {
let dao = MockDao::new();
dao.set("atomic_key", "value", 3600).await.unwrap();
let got = dao.get_and_delete("atomic_key").await.unwrap();
assert_eq!(got, Some("value".to_string()));
let after = dao.get("atomic_key").await.unwrap();
assert!(after.is_none(), "get_and_delete 后 key 应不存在");
}
#[tokio::test]
async fn mock_get_and_delete_missing_returns_none() {
let dao = MockDao::new();
let got = dao.get_and_delete("missing").await.unwrap();
assert!(got.is_none(), "不存在的 key 应返回 None");
}
#[tokio::test(flavor = "multi_thread")]
async fn mock_get_and_delete_concurrent_only_one_succeeds() {
let dao = Arc::new(MockDao::new());
dao.set("concurrent_key", "value", 3600).await.unwrap();
let mut handles = Vec::new();
for _ in 0..10 {
let d = dao.clone();
handles.push(tokio::spawn(async move {
d.get_and_delete("concurrent_key").await
}));
}
let mut success = 0;
let mut none_count = 0;
for handle in handles {
let result = handle.await.unwrap();
match result {
Ok(Some(_)) => success += 1,
Ok(None) => none_count += 1,
Err(e) => panic!("get_and_delete 不应返回错误: {:?}", e),
}
}
assert_eq!(success, 1, "并发调用仅一个返回 Some");
assert_eq!(none_count, 9, "其他 9 个返回 None");
}
pub struct MinimalDao {
store: Mutex<HashMap<String, String>>,
}
impl Default for MinimalDao {
fn default() -> Self {
Self::new()
}
}
impl MinimalDao {
pub fn new() -> Self {
Self {
store: Mutex::new(HashMap::new()),
}
}
}
#[async_trait]
impl GarrisonDao for MinimalDao {
async fn get(&self, key: &str) -> GarrisonResult<Option<String>> {
Ok(self.store.lock().get(key).cloned())
}
async fn set(&self, key: &str, value: &str, _ttl_seconds: u64) -> GarrisonResult<()> {
self.store.lock().insert(key.to_string(), value.to_string());
Ok(())
}
async fn update(&self, key: &str, value: &str) -> GarrisonResult<()> {
match self.store.lock().get_mut(key) {
Some(existing) => {
*existing = value.to_string();
Ok(())
},
None => Err(GarrisonError::Dao(format!("dao-key-not-found::{}", key))),
}
}
async fn expire(&self, _key: &str, _seconds: u64) -> GarrisonResult<()> {
Ok(()) }
async fn delete(&self, key: &str) -> GarrisonResult<()> {
self.store.lock().remove(key);
Ok(())
}
}
#[tokio::test]
async fn default_set_permanent_delegates_to_set_with_ttl_zero() {
let dao = MinimalDao::new();
dao.set_permanent("perm_key", "perm_value").await.unwrap();
let val = dao.get("perm_key").await.unwrap();
assert_eq!(val.as_deref(), Some("perm_value"));
}
#[tokio::test]
async fn default_get_timeout_returns_not_implemented() {
let dao = MinimalDao::new();
dao.set("key", "value", 3600).await.unwrap();
let result = dao.get_timeout("key").await;
assert!(matches!(result, Err(GarrisonError::NotImplemented(_))));
}
#[tokio::test]
async fn default_keys_returns_not_implemented() {
let dao = MinimalDao::new();
dao.set("key1", "v1", 0).await.unwrap();
let result = dao.keys("*").await;
assert!(matches!(result, Err(GarrisonError::NotImplemented(_))));
}
#[tokio::test]
async fn default_rename_get_set_permanent_delete() {
let dao = MinimalDao::new();
dao.set("old_key", "old_value", 0).await.unwrap();
dao.rename("old_key", "new_key").await.unwrap();
assert!(dao.get("old_key").await.unwrap().is_none());
assert_eq!(
dao.get("new_key").await.unwrap().as_deref(),
Some("old_value")
);
}
#[tokio::test]
async fn default_rename_missing_key_returns_invalid_param() {
let dao = MinimalDao::new();
let result = dao.rename("nonexistent", "new_key").await;
assert!(matches!(result, Err(GarrisonError::InvalidParam(_))));
}
#[tokio::test]
async fn default_find_social_binding_returns_not_implemented() {
let dao = MinimalDao::new();
let result = dao.find_social_binding(0, "wechat", "wx_openid").await;
assert!(
matches!(result, Err(GarrisonError::NotImplemented(ref msg)) if msg.contains("find_social_binding")),
"find_social_binding 默认实现应返回 NotImplemented,实际: {:?}",
result
);
}
#[tokio::test]
async fn default_insert_social_binding_returns_not_implemented() {
let dao = MinimalDao::new();
let result = dao
.insert_social_binding(0, 1001, "wechat", "wx_openid", None, 1700000000)
.await;
assert!(
matches!(result, Err(GarrisonError::NotImplemented(ref msg)) if msg.contains("insert_social_binding")),
"insert_social_binding 默认实现应返回 NotImplemented,实际: {:?}",
result
);
}
#[tokio::test]
async fn default_compare_and_update_if_greater_returns_not_implemented() {
let dao = MinimalDao::new();
let result = dao.compare_and_update_if_greater("key", 1, 60).await;
assert!(
matches!(result, Err(GarrisonError::NotImplemented(ref msg)) if msg.contains("compare_and_update_if_greater")),
"compare_and_update_if_greater 默认实现应返回 NotImplemented,实际: {:?}",
result
);
}
#[tokio::test]
async fn default_decr_returns_not_implemented() {
let dao = MinimalDao::new();
dao.set("counter", "5", 60).await.unwrap();
let result = dao.decr("counter").await;
assert!(
matches!(result, Err(GarrisonError::NotImplemented(ref msg)) if msg.contains("decr") && msg.contains("原子 decr")),
"decr 默认实现应返回 NotImplemented(含 '原子 decr' 提示),实际: {:?}",
result
);
let after = dao.get("counter").await.unwrap();
assert_eq!(
after.as_deref(),
Some("5"),
"decr 默认实现 short-circuit 后 key 不应被修改"
);
}
#[tokio::test]
async fn default_get_and_delete_returns_value_and_removes_key() {
let dao = MinimalDao::new();
dao.set("k1", "v1", 60).await.unwrap();
let val = dao.get_and_delete("k1").await.unwrap();
assert_eq!(val, Some("v1".to_string()));
assert!(dao.get("k1").await.unwrap().is_none());
}
#[tokio::test]
async fn default_get_and_delete_missing_key_returns_none() {
let dao = MinimalDao::new();
let val = dao.get_and_delete("nope").await.unwrap();
assert!(val.is_none());
}
#[test]
fn redis_config_default_returns_single_mode() {
let config = RedisConfig::default();
assert_eq!(
config.mode,
RedisDeploymentMode::Single {
url: "redis://127.0.0.1:6379".to_string()
}
);
assert_eq!(config.password, None);
assert_eq!(config.db, 0);
assert_eq!(config.connection_timeout_secs, 5);
assert_eq!(config.pool_size, 10);
}
#[test]
fn redis_config_serde_roundtrip() {
let config = RedisConfig {
mode: RedisDeploymentMode::Cluster {
urls: vec![
"redis://10.0.0.1:6379".to_string(),
"redis://10.0.0.2:6379".to_string(),
"redis://10.0.0.3:6379".to_string(),
],
},
password: Some("secret".to_string()),
db: 1,
connection_timeout_secs: 10,
pool_size: 20,
};
let json = serde_json::to_string(&config).unwrap();
let deserialized: RedisConfig = serde_json::from_str(&json).unwrap();
assert_eq!(config.mode, deserialized.mode);
assert_eq!(config.password, deserialized.password);
assert_eq!(config.db, deserialized.db);
assert_eq!(
config.connection_timeout_secs,
deserialized.connection_timeout_secs
);
assert_eq!(config.pool_size, deserialized.pool_size);
}
#[test]
fn redis_config_serde_partial_override() {
let json = r#"{"mode":{"mode":"cluster","urls":["redis://10.0.0.1:6379"]}}"#;
let config: RedisConfig = serde_json::from_str(json).unwrap();
match config.mode {
RedisDeploymentMode::Cluster { urls } => {
assert_eq!(urls, vec!["redis://10.0.0.1:6379".to_string()]);
},
_ => panic!("期望 Cluster 模式"),
}
assert_eq!(config.password, None);
assert_eq!(config.db, 0);
assert_eq!(config.connection_timeout_secs, 5);
assert_eq!(config.pool_size, 10);
}
#[test]
fn redis_deployment_mode_display() {
let single = RedisDeploymentMode::Single {
url: "redis://127.0.0.1:6379".to_string(),
};
assert!(format!("{}", single).contains("single"));
assert!(format!("{}", single).contains("redis://127.0.0.1:6379"));
let sentinel = RedisDeploymentMode::Sentinel {
master_name: "mymaster".to_string(),
urls: vec!["redis://s1:26379".to_string()],
};
let s = format!("{}", sentinel);
assert!(s.contains("sentinel"));
assert!(s.contains("mymaster"));
let cluster = RedisDeploymentMode::Cluster {
urls: vec!["redis://c1:6379".to_string(), "redis://c2:6379".to_string()],
};
let c = format!("{}", cluster);
assert!(c.contains("cluster"));
assert!(c.contains("2 nodes"));
let ms = RedisDeploymentMode::MasterSlave {
master_url: "redis://master:6379".to_string(),
slave_urls: vec!["redis://slave1:6379".to_string()],
};
let m = format!("{}", ms);
assert!(m.contains("master-slave"));
assert!(m.contains("master:6379"));
assert!(m.contains("1 slaves"));
}
#[test]
fn redis_deployment_mode_eq() {
let a = RedisDeploymentMode::Single {
url: "redis://127.0.0.1:6379".to_string(),
};
let b = RedisDeploymentMode::Single {
url: "redis://127.0.0.1:6379".to_string(),
};
let c = RedisDeploymentMode::Single {
url: "redis://10.0.0.1:6379".to_string(),
};
assert_eq!(a, b);
assert_ne!(a, c);
}
#[cfg(feature = "cache-redis")]
#[tokio::test(flavor = "multi_thread")]
async fn with_redis_config_stores_config() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
assert!(
dao.redis_config().is_none(),
"新建实例的 redis_config 应为 None"
);
let config = RedisConfig {
mode: RedisDeploymentMode::Sentinel {
master_name: "mymaster".to_string(),
urls: vec![
"redis://s1:26379".to_string(),
"redis://s2:26379".to_string(),
"redis://s3:26379".to_string(),
],
},
password: Some("pass123".to_string()),
db: 2,
connection_timeout_secs: 15,
pool_size: 50,
};
let dao = dao.with_redis_config(config);
let stored = dao.redis_config().expect("with_redis_config 后应有配置");
assert!(matches!(
&stored.mode,
RedisDeploymentMode::Sentinel { master_name, urls }
if master_name == "mymaster" && urls.len() == 3
));
assert_eq!(stored.password, Some("pass123".to_string()));
assert_eq!(stored.db, 2);
assert_eq!(stored.connection_timeout_secs, 15);
assert_eq!(stored.pool_size, 50);
}
#[cfg(feature = "cache-redis")]
#[tokio::test(flavor = "multi_thread")]
async fn without_redis_config_returns_none() {
let dao = GarrisonDaoOxcache::new().await.unwrap();
assert!(
dao.redis_config().is_none(),
"未调用 with_redis_config 时 redis_config 应为 None"
);
}
#[tokio::test]
async fn default_incr_initializes_new_key() {
let dao = MinimalDao::new();
let result = dao.incr("counter", 3600).await.unwrap();
assert_eq!(result, 1, "新键应初始化为 1");
let val = dao.get("counter").await.unwrap();
assert_eq!(val.as_deref(), Some("1"));
}
#[tokio::test]
async fn default_incr_increments_existing_key() {
let dao = MinimalDao::new();
dao.set("counter", "5", 3600).await.unwrap();
let result = dao.incr("counter", 3600).await.unwrap();
assert_eq!(result, 6, "已存在键 5 应递增为 6");
let result = dao.incr("counter", 3600).await.unwrap();
assert_eq!(result, 7, "已存在键 6 应递增为 7");
}
#[tokio::test]
async fn default_incr_handles_non_numeric_value() {
let dao = MinimalDao::new();
dao.set("bad_counter", "not_a_number", 3600).await.unwrap();
let result = dao.incr("bad_counter", 3600).await;
assert!(result.is_err(), "非数字值应返回错误而非静默回退到 0");
match result {
Err(GarrisonError::Dao(msg)) => {
assert!(
msg.contains("incr: 现存值非 u64"),
"错误消息应指明 parse 失败,实际: {}",
msg
);
},
other => panic!("期望 Dao 错误(非数字值 parse 失败),实际: {:?}", other),
}
}
#[tokio::test]
async fn default_eval_lua_returns_not_implemented() {
let dao = MinimalDao::new();
let result = dao
.eval_lua("return 1", vec!["k".to_string()], vec!["a".to_string()])
.await;
assert!(
matches!(result, Err(GarrisonError::NotImplemented(ref msg)) if msg.contains("eval_lua")),
"eval_lua 默认实现应返回 NotImplemented,实际: {:?}",
result
);
}
#[tokio::test]
async fn minimal_dao_default_equals_new() {
let dao = MinimalDao::default();
dao.set("k", "v", 60).await.unwrap();
let got = dao.get("k").await.unwrap();
assert_eq!(got.as_deref(), Some("v"));
}