-
Notifications
You must be signed in to change notification settings - Fork 15
fix(native): thread config.db.busyTimeoutMs into NativeDatabase open factories #2021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
carlos-alm
wants to merge
1
commit into
fix/issue-1881-loadconfig-resolves-from-process-cwd-instead
Choose a base branch
from
fix/issue-1882-thread-config-db-busytimeoutms-into-the-rust
base: fix/issue-1881-loadconfig-resolves-from-process-cwd-instead
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /** | ||
| * Regression tests for issue #1882: the JS call sites that open a | ||
| * `NativeDatabase` must pass the resolved `config.db.busyTimeoutMs` through | ||
| * as the factory's `busyTimeoutMs` argument, instead of silently relying on | ||
| * the Rust-side hardcoded default. | ||
| * | ||
| * Mocks `infrastructure/native.js` (the same pattern as | ||
| * `tests/unit/openRepo-busy.test.ts`) so the assertions are about *what | ||
| * argument each call site passes*, independent of whether a native addon is | ||
| * actually built for the current platform. | ||
| */ | ||
| import fs from 'node:fs'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const CUSTOM_BUSY_TIMEOUT_MS = 424242; | ||
|
|
||
| const openReadonlyCalls: Array<[string, number | undefined]> = []; | ||
| const openReadWriteCalls: Array<[string, number | undefined]> = []; | ||
|
|
||
| function makeFakeNativeDb() { | ||
| return { | ||
| getBuildMeta: () => null, | ||
| close: () => {}, | ||
| initSchema: () => {}, | ||
| exec: () => {}, | ||
| }; | ||
| } | ||
|
|
||
| vi.mock('../../src/infrastructure/native.js', () => ({ | ||
| isNativeAvailable: () => true, | ||
| getNative: () => ({ | ||
| NativeDatabase: { | ||
| openReadonly: (dbPath: string, busyTimeoutMs?: number) => { | ||
| openReadonlyCalls.push([dbPath, busyTimeoutMs]); | ||
| return makeFakeNativeDb(); | ||
| }, | ||
| openReadWrite: (dbPath: string, busyTimeoutMs?: number) => { | ||
| openReadWriteCalls.push([dbPath, busyTimeoutMs]); | ||
| return makeFakeNativeDb(); | ||
| }, | ||
| }, | ||
| }), | ||
| loadNative: () => ({ | ||
| NativeDatabase: { | ||
| openReadonly: (dbPath: string, busyTimeoutMs?: number) => { | ||
| openReadonlyCalls.push([dbPath, busyTimeoutMs]); | ||
| return makeFakeNativeDb(); | ||
| }, | ||
| openReadWrite: (dbPath: string, busyTimeoutMs?: number) => { | ||
| openReadWriteCalls.push([dbPath, busyTimeoutMs]); | ||
| return makeFakeNativeDb(); | ||
| }, | ||
| }, | ||
| }), | ||
| })); | ||
|
|
||
| import { | ||
| closeDb, | ||
| initSchema, | ||
| openDb, | ||
| openReadonlyWithNative, | ||
| openRepo, | ||
| } from '../../src/db/index.js'; | ||
| import { PipelineContext } from '../../src/domain/graph/builder/context.js'; | ||
| import { reopenNativeDb } from '../../src/domain/graph/builder/stages/native-db-lifecycle.js'; | ||
|
|
||
| let tmpDir: string; | ||
| let dbPath: string; | ||
|
|
||
| beforeAll(() => { | ||
| tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-native-busy-threading-')); | ||
| dbPath = path.join(tmpDir, '.codegraph', 'graph.db'); | ||
| const db = openDb(dbPath); | ||
| initSchema(db); | ||
| closeDb(db); | ||
| fs.writeFileSync( | ||
| path.join(tmpDir, '.codegraphrc.json'), | ||
| JSON.stringify({ db: { busyTimeoutMs: CUSTOM_BUSY_TIMEOUT_MS } }), | ||
| ); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe('openRepo threads busyTimeoutMs into NativeDatabase.openReadonly', () => { | ||
| it('passes the configured busyTimeoutMs to the native factory', () => { | ||
| openReadonlyCalls.length = 0; | ||
| const { close } = openRepo(dbPath); | ||
| close(); | ||
| expect(openReadonlyCalls).toHaveLength(1); | ||
| expect(openReadonlyCalls[0]?.[1]).toBe(CUSTOM_BUSY_TIMEOUT_MS); | ||
| }); | ||
| }); | ||
|
|
||
| describe('openReadonlyWithNative threads busyTimeoutMs into NativeDatabase.openReadonly', () => { | ||
| it('passes the configured busyTimeoutMs to the native factory', () => { | ||
| openReadonlyCalls.length = 0; | ||
| const { close } = openReadonlyWithNative(dbPath); | ||
| close(); | ||
| expect(openReadonlyCalls).toHaveLength(1); | ||
| expect(openReadonlyCalls[0]?.[1]).toBe(CUSTOM_BUSY_TIMEOUT_MS); | ||
| }); | ||
| }); | ||
|
|
||
| describe('reopenNativeDb (build pipeline) threads ctx.config.db.busyTimeoutMs into NativeDatabase.openReadWrite', () => { | ||
| it('passes ctx.config.db.busyTimeoutMs to the native factory', () => { | ||
| openReadWriteCalls.length = 0; | ||
| const ctx = new PipelineContext(); | ||
| ctx.dbPath = dbPath; | ||
| ctx.opts = { engine: 'native' }; | ||
| ctx.config = { db: { busyTimeoutMs: CUSTOM_BUSY_TIMEOUT_MS } } as PipelineContext['config']; | ||
|
|
||
| reopenNativeDb(ctx, 'test'); | ||
|
|
||
| expect(openReadWriteCalls).toHaveLength(1); | ||
| expect(openReadWriteCalls[0]?.[1]).toBe(CUSTOM_BUSY_TIMEOUT_MS); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * Regression tests for issue #1882: `config.db.busyTimeoutMs` must reach the | ||
| * Rust native DB layer (`NativeDatabase::open_readonly` / `open_read_write`), | ||
| * not just the TS-side better-sqlite3 pragma threaded by #1763. | ||
| * | ||
| * Verifies the applied `busy_timeout` via `queryGet('PRAGMA busy_timeout', [])` | ||
| * rather than the `pragma()` helper — `pragma()` only supports TEXT-affinity | ||
| * results (see #2019) and throws for INTEGER-returning pragmas like | ||
| * `busy_timeout`. | ||
| */ | ||
| import fs from 'node:fs'; | ||
| import os from 'node:os'; | ||
| import path from 'node:path'; | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
| import { DEFAULTS } from '../../src/infrastructure/config.js'; | ||
| import { getNative, isNativeAvailable } from '../../src/infrastructure/native.js'; | ||
| import type { NativeDatabase } from '../../src/types.js'; | ||
|
|
||
| const hasNativeDb = | ||
| isNativeAvailable() && typeof getNative().NativeDatabase?.openReadWrite === 'function'; | ||
|
|
||
| /** Read the effective `busy_timeout` pragma value via queryGet (avoids the pragma() TEXT-only bug, #2019). */ | ||
| function readBusyTimeout(ndb: NativeDatabase): number { | ||
| const row = ndb.queryGet('PRAGMA busy_timeout', []) as { timeout: number } | null; | ||
| return row?.timeout as number; | ||
| } | ||
|
|
||
| describe.skipIf(!hasNativeDb)('NativeDatabase busy_timeout_ms threading (Rust layer)', () => { | ||
| let tmpDir: string; | ||
| let dbPath: string; | ||
| let nativeDb: NativeDatabase | undefined; | ||
|
|
||
| beforeEach(() => { | ||
| tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-native-busy-')); | ||
| dbPath = path.join(tmpDir, 'test.db'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| try { | ||
| nativeDb?.close(); | ||
| } catch { | ||
| /* already closed */ | ||
| } | ||
| nativeDb = undefined; | ||
| fs.rmSync(tmpDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('openReadWrite applies a configured busyTimeoutMs', () => { | ||
| const NativeDB = getNative().NativeDatabase; | ||
| nativeDb = NativeDB.openReadWrite(dbPath, 424242); | ||
| expect(readBusyTimeout(nativeDb)).toBe(424242); | ||
| }); | ||
|
|
||
| it('openReadWrite defaults to DEFAULTS.db.busyTimeoutMs when omitted', () => { | ||
| const NativeDB = getNative().NativeDatabase; | ||
| nativeDb = NativeDB.openReadWrite(dbPath); | ||
| expect(readBusyTimeout(nativeDb)).toBe(DEFAULTS.db.busyTimeoutMs); | ||
| }); | ||
|
|
||
| it('openReadonly applies a configured busyTimeoutMs', () => { | ||
| const NativeDB = getNative().NativeDatabase; | ||
| // openReadonly requires the file to already exist. | ||
| NativeDB.openReadWrite(dbPath).close(); | ||
| nativeDb = NativeDB.openReadonly(dbPath, 99999); | ||
| expect(readBusyTimeout(nativeDb)).toBe(99999); | ||
| }); | ||
|
|
||
| it('openReadonly defaults to DEFAULTS.db.busyTimeoutMs when omitted', () => { | ||
| const NativeDB = getNative().NativeDatabase; | ||
| NativeDB.openReadWrite(dbPath).close(); | ||
| nativeDb = NativeDB.openReadonly(dbPath); | ||
| expect(readBusyTimeout(nativeDb)).toBe(DEFAULTS.db.busyTimeoutMs); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The threading test asserts that
openRepo,openReadonlyWithNative, andreopenNativeDball forwardbusyTimeoutMs. However, three changed call sites are not exercised here:openNativeDatabase(~line 1955 innative-orchestrator.ts),runPostNativeAnalysis(~line 579 innative-orchestrator.ts), andopenNativeDbForFanMetricsinbranch-compare.ts. A future regression in those paths would go unnoticed by this test suite.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!