import { beforeEach, describe, expect, it, } from "vitest"; import { createTestDb, type TestDbContext, } from "../db/test-db-factory.js"; import { ImmutableTree, } from "../immutable-tree.js"; import { MutableTree, } from "../mutable-tree.js"; import { stringToBytes, } from "../utils/bytes.js"; import { NodeDB, } from "./node-db.js"; /** * Tests for async pruning functionality * Based on Go IAVL v1.3.5 nodedb.go async pruning */ describe("Async Pruning", () => { let ctx: TestDbContext; let ndb: NodeDB; let tree: MutableTree; beforeEach(async () => { ctx = await createTestDb(); ndb = new NodeDB(ctx.db, 1000, { asyncPruning: true, // Enable async pruning }); await ndb.initialize(); const workingTree = new ImmutableTree(null, ndb, 0n); tree = new MutableTree(ndb, workingTree, true); }); afterEach(async () => { await ctx.cleanup(); }); // Helper to wait for async pruning to complete async function waitForPruning(expectedFirstVersion: bigint, maxWaitMs = 2000): Promise { const startTime = Date.now(); while (Date.now() - startTime < maxWaitMs) { await new Promise(resolve => setTimeout(resolve, 50)); // Check if firstVersion has been updated to expected value const firstVersion = ndb.getFirstVersion(); if (firstVersion >= expectedFirstVersion) { // Wait a bit more to ensure completion await new Promise(resolve => setTimeout(resolve, 100)); return; } } // Timeout - may or may not be an error, depending on the test } describe("async deleteVersionsTo", () => { it("should queue deletion for async processing", async () => { const ctx = await createTestDb(); ndb = new NodeDB(ctx.db, 1000, { asyncPruning: true, // Enable async pruning }); await ndb.initialize(); const workingTree = new ImmutableTree(null, ndb, 0n); tree = new MutableTree(ndb, workingTree, true); // Create multiple versions await tree.set(stringToBytes("key1"), stringToBytes("value1")); await tree.saveVersion(); await tree.set(stringToBytes("key2"), stringToBytes("value2")); await tree.saveVersion(); await tree.set(stringToBytes("key3"), stringToBytes("value3")); await tree.saveVersion(); expect(ndb.getFirstVersion()).toBe(1n); expect(ndb.getLatestVersion()).toBe(3n); // Request deletion up to and including version 2 await ndb.deleteVersionsTo(2n); // With async pruning, deletion happens in background // Wait for async pruning to complete await waitForPruning(2n); // After pruning, versions 1-2 should be deleted, firstVersion becomes 3 expect(ndb.getFirstVersion()).toBe(3n); expect(await ndb.versionExists(1n)).toBe(false); expect(await ndb.versionExists(2n)).toBe(false); expect(await ndb.versionExists(3n)).toBe(true); await ctx.cleanup(); }); it("should handle multiple pruning requests", async () => { // Create 5 versions for (let i = 1; i <= 5; i++) { await tree.set(stringToBytes(`key${i}`), stringToBytes(`value${i}`)); await tree.saveVersion(); } expect(ndb.getFirstVersion()).toBe(1n); expect(ndb.getLatestVersion()).toBe(5n); // Queue deletion up to and including version 2 await ndb.deleteVersionsTo(2n); // Queue another deletion up to and including version 4 (should replace the first) await ndb.deleteVersionsTo(4n); // Wait for async pruning (deletes 1-4 inclusive, firstVersion becomes 5) await waitForPruning(5n); // Should have deleted up to and including version 4 expect(ndb.getFirstVersion()).toBe(5n); expect(await ndb.versionExists(1n)).toBe(false); expect(await ndb.versionExists(2n)).toBe(false); expect(await ndb.versionExists(3n)).toBe(false); expect(await ndb.versionExists(4n)).toBe(false); expect(await ndb.versionExists(5n)).toBe(true); }); it("should not interfere with commits", async () => { // Create multiple versions for (let i = 1; i <= 10; i++) { await tree.set(stringToBytes(`key${i}`), stringToBytes(`value${i}`)); await tree.saveVersion(); } // Queue pruning (deletes 1-5 inclusive, firstVersion becomes 6) await ndb.deleteVersionsTo(5n); // Perform more commits while pruning is happening await tree.set(stringToBytes("key11"), stringToBytes("value11")); await tree.saveVersion(); await tree.set(stringToBytes("key12"), stringToBytes("value12")); await tree.saveVersion(); // Wait for pruning await waitForPruning(5n); // Verify results (firstVersion should be 6 after deleting 1-5) expect(ndb.getFirstVersion()).toBe(6n); expect(ndb.getLatestVersion()).toBe(12n); // Verify data integrity const val11 = await tree.get(stringToBytes("key11")); const val12 = await tree.get(stringToBytes("key12")); expect(val11).toEqual(stringToBytes("value11")); expect(val12).toEqual(stringToBytes("value12")); }); }); describe("committing flag", () => { it("should set and unset committing flag", () => { expect(ndb.isCommitting()).toBe(false); ndb.setCommitting(); expect(ndb.isCommitting()).toBe(true); ndb.unsetCommitting(); expect(ndb.isCommitting()).toBe(false); }); it("should prevent pruning during commit", async () => { // Create several versions so we have something to prune for (let i = 1; i <= 5; i++) { await tree.set(stringToBytes(`key${i}`), stringToBytes(`value${i}`)); await tree.saveVersion(); } // Set committing flag manually ndb.setCommitting(); expect(ndb.isCommitting()).toBe(true); // Queue pruning (deletes 1-2 inclusive) - should wait for commit await ndb.deleteVersionsTo(2n); // Wait a reasonable amount - pruning should NOT have completed because commit flag is set await new Promise(resolve => setTimeout(resolve, 500)); // firstVersion should still be 1 (pruning waiting for commit) expect(ndb.getFirstVersion()).toBe(1n); // Unset committing flag - this will signal the worker to proceed ndb.unsetCommitting(); expect(ndb.isCommitting()).toBe(false); // Now wait for pruning (deletes 1-2 inclusive, firstVersion becomes 3) // Give it plenty of time since the worker runs in a loop with sleeps await waitForPruning(3n, 10000); // Pruning should complete (versions 1-2 deleted, firstVersion becomes 3) expect(ndb.getFirstVersion()).toBe(3n); expect(await ndb.versionExists(1n)).toBe(false); expect(await ndb.versionExists(2n)).toBe(false); expect(await ndb.versionExists(3n)).toBe(true); }); }); describe("stop pruning worker", () => { it("should stop pruning worker on command", async () => { // Create versions await tree.set(stringToBytes("key1"), stringToBytes("value1")); await tree.saveVersion(); await tree.set(stringToBytes("key2"), stringToBytes("value2")); await tree.saveVersion(); // Stop the pruning worker ndb.stopPruningWorker(); // Queue pruning - should not be processed since worker is stopped await ndb.deleteVersionsTo(2n); // Wait a bit await new Promise(resolve => setTimeout(resolve, 300)); // Pruning should not have happened expect(ndb.getFirstVersion()).toBe(1n); }); }); });