gitoriaLog in with ident

mpackdb

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8788872587888725release 1.0.7caramboleyo87888725/tests/concurrent-lock.test.js

1.4 KB

  1. import MPackDB from '../src/MPackDB.js';
  2. import { rm } from 'fs/promises';
  3. import { spawn } from 'child_process';
  4. const TEST_DB_PATH = 'db/concurrent';
  5. console.log('--- Concurrent Lock Test ---');
  6. // Clean up
  7. await rm(TEST_DB_PATH, { recursive: true, force: true });
  8. // Test 1: Concurrent writes from same process
  9. console.log('\nTest 1: Concurrent writes (same process)');
  10. const db = new MPackDB(`${TEST_DB_PATH}/db`, { debug: true });
  11. const writes = [];
  12. const startTime = Date.now();
  13. // Fire 10 concurrent inserts
  14. for (let i = 0; i < 10; i++) {
  15. writes.push(db.insert({ id: i, data: `record-${i}` }));
  16. }
  17. await Promise.all(writes);
  18. const duration = Date.now() - startTime;
  19. console.log(`✓ Completed 10 concurrent inserts in ${duration}ms`);
  20. console.log(` Average: ${(duration / 10).toFixed(1)}ms per insert`);
  21. const all = await db.find();
  22. console.log(`✓ All ${all.length} records written correctly`);
  23. // Test 2: Verify serialization (they should have waited for each other)
  24. if (duration < 50) {
  25. console.log('⚠ Warning: Inserts were too fast - lock might not be working');
  26. } else {
  27. console.log('✓ Inserts were serialized (lock is working)');
  28. }
  29. // Test 3: Check no corruption
  30. const uniqueIds = new Set(all.map(r => r.id));
  31. if (uniqueIds.size === 10) {
  32. console.log('✓ No data corruption - all records unique');
  33. } else {
  34. throw new Error('❌ Data corruption detected!');
  35. }
  36. console.log('\n✓ Concurrent lock test passed!');

Branches

Latest commits

  • 87888725release 1.0.7caramboleyo
  • c4cdb9b6node: import prefixes (Deno compat) + pre-existing index-state WIPcaramboleyo
  • 0afb8f4bupdate now must be a callbackcaramboleyo
  • cde73eb4release 1.0.6caramboleyo
  • d01dda02add index hints, intersection, boundingBox; remove findByIndexcaramboleyo
  • b8ffc1a0release 1.0.5caramboleyo
  • d47876a1reimplemented lost features like indexed find and more testscaramboleyo
  • 7f08da9afixed insert ignoring model definitioncaramboleyo
  • 705774a9added flush before findcaramboleyo
  • b4db6391initial commitcaramboleyo