Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

test(redis): cover standalone collection pagination scoping

弈秋bf9bb3fc47c4
diff --git a/crates/dbx-driver-redis/src/lib.rs b/crates/dbx-driver-redis/src/lib.rs
index 3f335abc0f..c4b5ec090f 100644
--- a/crates/dbx-driver-redis/src/lib.rs
+++ b/crates/dbx-driver-redis/src/lib.rs
@@ -5960,6 +5960,86 @@ mod tests {
assert!(expired.commands.is_empty());
}
+ /// Replays the standalone-Redis pagination sequence from issue #12640: a large
+ /// hash is read once through the scoped value API, then continued through the
+ /// scoped load-more API with the same scope (the pairing the desktop app uses).
+ /// Every field must eventually arrive exactly once, and a continuation under a
+ /// different (unseoped) scope must fail instead of restarting the walk.
+ #[tokio::test]
+ async fn scoped_hash_pagination_walks_large_collections_without_repeating_the_first_page() {
+ const FIELDS: usize = 4000;
+ const PAGE: usize = 200;
+ let scope = super::RedisOperationScope { connection_id: "issue-12640-standalone".to_string(), db: 0 };
+
+ // Real HSCAN cursors: one batch of `PAGE` fields per server round trip.
+ let mut responses = vec![bulk("hash"), RedisRawValue::Int(-1), RedisRawValue::Int(FIELDS as i64)];
+ let pages = FIELDS.div_ceil(PAGE);
+ for page in 0..pages {
+ let cursor = if page + 1 == pages { "0".to_string() } else { (page + 1).to_string() };
+ let pairs = (page * PAGE..((page + 1) * PAGE).min(FIELDS))
+ .map(|index| (format!("field:{index}"), format!("value:{index}")))
+ .collect();
+ responses.push(hscan_response_owned(&cursor, pairs));
+ responses.push(httl_response(PAGE, -1));
+ }
+ let mut con = FakeRedisConnection::new(responses);
+
+ let value = super::get_value_scoped(&mut con, b"issue-12640-hash", &scope).await.unwrap();
+ let RedisValueData::Hash { items: mut seen, scan_cursor, total } = value.data else {
+ panic!("expected hash");
+ };
+ assert_eq!(total, FIELDS as u64);
+ assert_eq!(seen.len(), PAGE, "the first page defaults to the configured page size");
+ let mut cursor = scan_cursor.expect("a 4000-field hash must offer a continuation");
+ assert!(cursor >= super::COLLECTION_OVERFLOW_CURSOR_START, "the continuation is an overflow session marker");
+
+ // The old desktop path continued under the default (unseoped) connection,
+ // which rejected the marker and stopped pagination at the first page.
+ let mut mismatched = FakeRedisConnection::new(vec![]);
+ let error = super::load_more_collection(&mut mismatched, b"issue-12640-hash", "hash", cursor, PAGE, None, None)
+ .await
+ .unwrap_err();
+ assert!(error.contains("invalid, expired"), "{error}");
+ assert!(mismatched.commands.is_empty(), "a foreign cursor must not reach the server");
+
+ let mut pages_seen = 1;
+ while cursor != 0 {
+ let page = super::load_more_collection_scoped(
+ &mut con,
+ b"issue-12640-hash",
+ "hash",
+ cursor,
+ PAGE,
+ None,
+ None,
+ &scope,
+ )
+ .await
+ .unwrap();
+ let RedisCollectionPage::Hash { items, scan_cursor: next } = page else {
+ panic!("expected hash");
+ };
+ seen.extend(items);
+ pages_seen += 1;
+ assert!(pages_seen <= pages, "pagination must terminate");
+ match next {
+ Some(next) => {
+ assert_ne!(next, cursor, "each page must continue with a fresh marker");
+ cursor = next;
+ }
+ None => break,
+ }
+ }
+
+ assert_eq!(pages_seen, pages);
+ assert_eq!(seen.len(), FIELDS);
+ let unique: std::collections::HashSet<_> = seen.iter().map(|item| item.field.raw_base64.clone()).collect();
+ assert_eq!(unique.len(), FIELDS, "every field arrives exactly once");
+ // Every page came from a fresh HSCAN round trip — no page was re-served from
+ // the start of the hash, which is what a foreign or replayed cursor would do.
+ assert_eq!(con.command_count("HSCAN"), pages);
+ }
+
#[tokio::test]
async fn set_overflow_rejects_unknown_and_foreign_tokens_without_server_scan() {
let scope = super::RedisOperationScope { connection_id: "set-scope-test".to_string(), db: 1 };
diff --git a/crates/dbx-driver-redis/tests/issue_12640_standalone_pagination.rs b/crates/dbx-driver-redis/tests/issue_12640_standalone_pagination.rs
new file mode 100644
index 0000000000..dab48fdd9a
--- /dev/null
+++ b/crates/dbx-driver-redis/tests/issue_12640_standalone_pagination.rs
@@ -0,0 +1,188 @@
+//! Real-environment verification for issue #12640: pagination of large hash and
+//! set collections on a standalone Redis.
+//!
+//! Replays the exact call sequence the desktop app performs after the fix —
+//! `get_value_scoped_with_preview_limit` mints the continuation cursor under the
+//! connection/database scope, and every `load_more_collection_scoped` reuses the
+//! same scope — and also pins the pre-fix pairing (an unscoped continuation),
+//! which a real server rejects.
+//!
+//! Run against a standalone Redis:
+//! DBX_REDIS_STANDALONE_URL=redis://127.0.0.1:6379 cargo test -p dbx-driver-redis \
+//! --test issue_12640_standalone_pagination -- --ignored
+//!
+//! The test owns two keys in database 0 and deletes them before and after the
+//! walk, so it is safe to point at a shared development server.
+
+use std::collections::HashSet;
+
+use base64::engine::general_purpose::STANDARD as BASE64;
+use base64::Engine as _;
+use dbx_driver_redis::{
+ get_value_scoped_with_preview_limit, load_more_collection, load_more_collection_scoped, select_db,
+ RedisCollectionPage, RedisOperationScope, RedisValueData, STRING_PREVIEW_MAX_BYTES,
+};
+
+const FIELDS: usize = 4000;
+const PAGE: usize = 200;
+/// Fields written per HSET/SADD command. Seeding 4000 single-command writes in
+/// one pipeline overruns the client's default 500 ms response timeout as soon
+/// as the server is not on localhost.
+const SEED_BATCH: usize = 200;
+
+async fn connect() -> redis::aio::MultiplexedConnection {
+ let url = std::env::var("DBX_REDIS_STANDALONE_URL").unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
+ let client = redis::Client::open(url).expect("open redis url");
+ let config = redis::AsyncConnectionConfig::new()
+ .set_connection_timeout(Some(std::time::Duration::from_secs(10)))
+ .set_response_timeout(Some(std::time::Duration::from_secs(60)));
+ let mut con = client.get_multiplexed_async_connection_with_config(&config).await.expect("connect redis");
+ select_db(&mut con, 0).await.expect("select db");
+ con
+}
+
+async fn del_keys(con: &mut redis::aio::MultiplexedConnection) {
+ let _: i64 =
+ redis::cmd("DEL").arg("issue-12640-hash").arg("issue-12640-set").query_async(con).await.expect("clean keys");
+}
+
+async fn seed_hash(con: &mut redis::aio::MultiplexedConnection, key: &str) {
+ for start in (0..FIELDS).step_by(SEED_BATCH) {
+ let end = (start + SEED_BATCH).min(FIELDS);
+ let mut cmd = redis::cmd("HSET");
+ cmd.arg(key);
+ for index in start..end {
+ cmd.arg(format!("field:{index}")).arg(format!("value:{index}"));
+ }
+ let _: i64 = cmd.query_async(con).await.expect("seed hash batch");
+ }
+}
+
+async fn seed_set(con: &mut redis::aio::MultiplexedConnection, key: &str) {
+ for start in (0..FIELDS).step_by(SEED_BATCH) {
+ let end = (start + SEED_BATCH).min(FIELDS);
+ let mut cmd = redis::cmd("SADD");
+ cmd.arg(key);
+ for index in start..end {
+ cmd.arg(format!("member:{index}"));
+ }
+ let _: i64 = cmd.query_async(con).await.expect("seed set batch");
+ }
+}
+
+fn decoded(item: &impl AsRef<str>) -> String {
+ String::from_utf8(BASE64.decode(item.as_ref()).expect("base64 payload")).expect("utf8 payload")
+}
+
+/// Walks a hash from its first scoped read to the last page, asserting every
+/// field arrives exactly once and the walk terminates.
+async fn walk_hash(con: &mut redis::aio::MultiplexedConnection, key: &str, scope: &RedisOperationScope) {
+ let value = get_value_scoped_with_preview_limit(con, key.as_bytes(), scope, STRING_PREVIEW_MAX_BYTES)
+ .await
+ .expect("first hash page");
+ let RedisValueData::Hash { items, scan_cursor, total } = value.data else {
+ panic!("expected hash value");
+ };
+ assert_eq!(total, FIELDS as u64);
+ assert!(!items.is_empty(), "the first page must carry fields");
+
+ let mut seen: HashSet<String> = items.iter().map(|item| decoded(&item.field.raw_base64)).collect();
+ let Some(mut cursor) = scan_cursor else {
+ panic!("a 4000-field hash must offer a continuation");
+ };
+
+ // Pre-fix pairing: the desktop app continued standalone collections under the
+ // default scope, which a real server rejects without issuing any scan.
+ let unscoped = load_more_collection(con, key.as_bytes(), "hash", cursor, PAGE, None, None).await;
+ assert!(
+ unscoped.is_err(),
+ "an unscoped continuation must be rejected, got {:?}",
+ unscoped.ok().map(|page| match page {
+ RedisCollectionPage::Hash { items, .. } => items.len(),
+ _ => 0,
+ })
+ );
+
+ let mut rounds = 0usize;
+ while cursor != 0 {
+ let page = load_more_collection_scoped(con, key.as_bytes(), "hash", cursor, PAGE, None, None, scope)
+ .await
+ .expect("scoped hash continuation");
+ let RedisCollectionPage::Hash { items, scan_cursor } = page else {
+ panic!("expected hash page");
+ };
+ let before = seen.len();
+ seen.extend(items.iter().map(|item| decoded(&item.field.raw_base64)));
+ assert_eq!(seen.len() - before, items.len(), "no page may re-serve a field");
+ match scan_cursor {
+ // The marker is an opaque session handle: it stays the same while
+ // the session still buffers a page and only switches to the server
+ // scan cursor once the buffer drains.
+ Some(next) => cursor = next,
+ None => break,
+ }
+ rounds += 1;
+ assert!(rounds <= 5 * FIELDS / PAGE + 8, "pagination must terminate");
+ }
+
+ assert_eq!(seen.len(), FIELDS, "every field must arrive exactly once");
+ for index in 0..FIELDS {
+ assert!(seen.contains(&format!("field:{index}")), "missing field:{index}");
+ }
+}
+
+/// Same walk for sets: the sibling collection that shares the overflow session
+/// mechanism and the same pre-fix scope mismatch.
+async fn walk_set(con: &mut redis::aio::MultiplexedConnection, key: &str, scope: &RedisOperationScope) {
+ let value = get_value_scoped_with_preview_limit(con, key.as_bytes(), scope, STRING_PREVIEW_MAX_BYTES)
+ .await
+ .expect("first set page");
+ let RedisValueData::Set { items, scan_cursor, total } = value.data else {
+ panic!("expected set value");
+ };
+ assert_eq!(total, FIELDS as u64);
+
+ let mut seen: HashSet<String> = items.iter().map(|item| decoded(&item.member.raw_base64)).collect();
+ let Some(mut cursor) = scan_cursor else {
+ panic!("a 4000-member set must offer a continuation");
+ };
+ let unscoped = load_more_collection(con, key.as_bytes(), "set", cursor, PAGE, None, None).await;
+ assert!(unscoped.is_err(), "an unscoped set continuation must be rejected");
+
+ let mut rounds = 0usize;
+ while cursor != 0 {
+ let page = load_more_collection_scoped(con, key.as_bytes(), "set", cursor, PAGE, None, None, scope)
+ .await
+ .expect("scoped set continuation");
+ let RedisCollectionPage::Set { items, scan_cursor } = page else {
+ panic!("expected set page");
+ };
+ let before = seen.len();
+ seen.extend(items.iter().map(|item| decoded(&item.member.raw_base64)));
+ assert_eq!(seen.len() - before, items.len(), "no page may re-serve a member");
+ match scan_cursor {
+ Some(next) => cursor = next,
+ None => break,
+ }
+ rounds += 1;
+ assert!(rounds <= 5 * FIELDS / PAGE + 8, "pagination must terminate");
+ }
+
+ assert_eq!(seen.len(), FIELDS, "every member must arrive exactly once");
+}
+
+#[tokio::test]
+#[ignore = "requires a standalone Redis at DBX_REDIS_STANDALONE_URL (default redis://127.0.0.1:6379)"]
+async fn standalone_hash_and_set_pagination_walk_large_collections_under_one_scope() {
+ let mut con = connect().await;
+ let scope = RedisOperationScope { connection_id: "issue-12640-verify".to_string(), db: 0 };
+
+ del_keys(&mut con).await;
+ seed_hash(&mut con, "issue-12640-hash").await;
+ seed_set(&mut con, "issue-12640-set").await;
+
+ walk_hash(&mut con, "issue-12640-hash", &scope).await;
+ walk_set(&mut con, "issue-12640-set", &scope).await;
+
+ del_keys(&mut con).await;
+}